variable
The concept of variables:
A storage area in memory
This area has its own name (variable name) and type (data type)
Every variable in Java must be declared first and then used
The data in this area can be constantly changing within the same type range
Note on using variables:
Variable scope: valid between a pair of {}
Initial value
Define the format of the variable: data type variable name = initialization value
Variables access this area by using variable names
For each type of data, a clear specific data type is defined, and different sizes of memory space are allocated in the memory.
All variables are divided into member variables and local invariants.
Member variables include instance variables and class variables.
Local variables have formal parameters, method local variables, and code block local variables
. The similarities and differences in initialization values are :
Same: Both have life cycles.
Different: Local variables need to be initialized explicitly except for formal parameters.
Automatic type conversion
Byte can be converted to short, int,, long, float and double;
short can be converted to int, long, float and double;
char can be converted to int, long, float and double;
int can be converted to long, float and double;
Long can be converted to float and double;
float can be converted to double;
Forced type conversion
The inverse process of automatic type conversion converts a data type with a large capacity to a data type with a small capacity. It is necessary to add the coercion symbol () when using it, but it may cause the precision to decrease or overflow, so be careful.
Generally, a string cannot be directly converted to a basic type, but it can be converted to a basic type through a wrapper class corresponding to the basic type.
String a = "35"; int i = Integer.parseInt(a);
boolean type cannot convert data
constant
Constant declaration
Basic format: final data type constant identifier = value;
The java language recommends that all constant identifiers be expressed in uppercase letters.
E.g:
final int MAX=10;
final float PI=3.14;
Each integer type in Java has a fixed table number range and field length, which is not affected by the specific OS to ensure the portability of java programs.
Java integer constants are of type int by default. The declaration of long type constants must be followed by'l' or'L'.
Integer types: byte, short, int, long
Types of | Take up storage space | Table number range |
---|---|---|
byte | 1 byte = 8bit | -128 ~ 127 |
short | 2 bytes | -215 ~215-1 |
int | 4 bytes | -231 ~ 231-1 |
long | 8 bytes | -263 ~ 263-1 |
Floating point types: float, double
Types of | Take up storage space | Table number range |
---|---|---|
Single precision float | 4 bytes | -3.403E38 ~ 3.403E38 |
Double precision | 8 bytes | -1.798E308 ~ 1.798E308 |
The char type data is used to express the use of "character" (2 bytes)
escape characters in the usual sense.
Boolean type: boolean,
null constant
String: string
Operator
Operator is a special symbol used to represent data operation, assignment and comparison.
Arithmetic operators
Assignment operators
Comparison operators (relational operators)
Logical operators
Bitwise operators
Ternary operators