Chapter 2 variables, data types and operators, and data type operators
1. variables: a basic unit for storing data. Different variables are independent of each other.
2. Variable declaration and value assignment syntax
1) declare and assign values: data type variable name = value; example: int price = 10;
2) separate declaration and Value assignment: data type variable name; for example, int price;
Variable name = value; for example, price = 10;
3. perform the following steps to use a variable:
1) Declare the variable, that is, apply for a space in the memory based on the data type, and take an alias for the space; for example: money.
2) assign a value to a variable, also called an initialization variable; for example, money = 100;
3) Call the variable. For example, System. out. println (money );
Note: variables must be declared and the initial values must be assigned to them before they can be used.
4. variable naming rules
1) The variable must start with a letter, underscore, or $.
2) variables can contain numbers, but cannot start with numbers.
3) except the "_" or "$" symbol, the variable name cannot contain any special characters.
4) You cannot use keywords in the Java language, such as int class.
5. Data Type
Eight basic data types: Numeric: byte short int long float double
Non-numeric type: char boolean
Reference data type: Class interface array String
6. Data Type Conversion
Conditions for automatic type conversion
1) compatible with types (int and double are both numeric)
2) The target type is greater than the source type (double is greater than int)
Forced type conversion
(Data Type) variable name;
(Data Type) value;
For example, double num = 5.5;
Int result = (int) num;
7. Operators
Arithmetic Operator: +-*/% + + --
Assignment operator: = + =-= * =/= % =
Relational operators:> <>=<==! =
Logical OPERATOR :! & |
Bitwise OPERATOR :~ ^ >><>>>
String concatenation operator: +