Java first acquaintance 1-variables and operators.

In C language, we learned how to create variables. Is creating variables in Java the same as in C language? Let's review it together.
1. Variables

1. The
general format of integer variables is int variable name = assignment; int type occupies 4 bytes, but in Java, regardless of whether the operating system is 32-bit or 64-bit, as long as it is an integer, four bytes are allocated. The data range represented by 4 bytes is -2^31~ 2^31-1, which is about -2 billion to +2.1 billion.
(The following figure illustrates)
Insert picture description here
Insert picture description here
2. Long integer variable The
general format is long variable name=initial value; for example (long a=10; or long a=10L;)
long occupies 8 bytes, a total of -2^63 ~ 2 ^63-1, the range is far beyond the int type, it is enough in most cases.
Insert picture description here
Insert picture description here
3. The
basic syntax format of double -precision floating-point type is; double variable name=initial value;
Insert picture description here

Insert picture description here
Although the double in Java is also 8 bytes, the memory layout of floating-point numbers is very different from that of integers, and the data range cannot be expressed simply in the form of 2 ^ n.
The memory layout of Java's double type complies with the IEEE 754 standard (and C The language is the same), try to use a limited memory space to represent possibly infinite decimals, and there is bound to be a certain accuracy error.
4. Single-precision floating-point
basic syntax format float f=12.5f; (or 12.5F is also possible). Float is 4 bytes, the
range is relatively small, so the double type is generally preferred.
Insert picture description here
Insert picture description here
5. Character type The
basic syntax format is: char variable name=initial value;
note:

  1. In Java, single quotation marks + single letters are used to represent character literals.
  2. A character in a computer is essentially an integer. In C language, ASCII is used to represent characters, while Java uses Unicode to represent characters. Therefore, a character occupies two bytes, and there are more types of characters, including Chinese.
    Insert picture description here

Insert picture description here
6. Byte type The
basic syntax format is: byte variable name=initial value;
byte type also represents an integer, which only occupies one byte, and the range is small (-128~+127).
Insert picture description here
Insert picture description here

7. The
basic syntax format of short integer variable is: short variable name=initial value;
short is 2 bytes, which means the range is small, which is -32767~+32767.
Insert picture description here
Insert picture description here
8. The
basic syntax format of Boolean type is: boolean variable name= Initial value;
Note: 1. There are only two values ​​for boolean type variables, true means true, false means false.
2. Java's boolean type and int type cannot be converted to each other. There is no 1 for true and 0 for false.
3. The boolean type. Some JVM implementations occupies 1 byte, and some occupies 1 bit. This is not clearly defined.

Insert picture description here
Insert picture description here

9. String type variable The
basic syntax format is: string variable name = initial value;
Note: string is not a basic type, but a reference type.

Insert picture description hereInsert picture description here

2. Operators
1. Arithmetic operators.
Basic operators: +, -, *, /, %.
The rules are relatively simple, but you should pay attention to division and modulo operations.
Division: (1) int/int type, the result is also int type, and double is required for calculation;
Insert picture description here
Insert picture description here
(2) 0 cannot be used as a divisor;
Insert picture description here
Insert picture description here
(3)% means to take the remainder, not only modulo int, but also Double to find the modulus.
Insert picture description here
Insert picture description here
In addition, there are += -= *= /= %=, ++,-operators.
2. Relational operators
There are mainly 6 relational operators, namely: ==,! =, >, <, >=, <=.

Insert picture description here
The results of its operation are all boolean type
Insert picture description here
3. Logical operators
There are three main logical operators: && ||!
Note: The operands of logical operators (operands are often the result of relational operators) and return values ​​are also boolean types .

4. Bit Operators
The smallest unit of data operations in Java is not bytes, but binary bits.
There are four main bit operators: & | ~ ^
bit operations represent binary bit operations. Computers are all using binary Represents the data (sequence formed by 01), bitwise operation is to calculate according to each bit of the binary bit in turn
&: As long as the corresponding bit is not the same, it is 0;
|: As long as the corresponding bit is not 0, the result is 1. ;
^: XOR of two same numbers, the result is 0; (XOR is performed on the different bits, the same is 0, the difference is 1);
~: The bit is reversed.

5.
Conditional operator There is only one conditional operator:
expression 1? Expression 2: expression 3 When the value of expression 1 is true, the value of the entire expression is the value of expression 2; when the value of expression 1 When it is false, the value of the entire expression is the value of expression 3.

6. Shift operators
There are three main shift operators: << >> >>>
Shift left: << The leftmost bit is not needed, and the rightmost bit is filled with 0; (the following figure uses the binary value of 2 as an example) Shifting to the left is equivalent to multiplication.
Insert picture description here

Right shift: >> The rightmost bit is not
needed , and the leftmost bit is complemented with the sign bit (positive number complements 0, negative number complements 1); (the following figure uses 11 binary as an example) Right shift is equivalent to division.
Insert picture description here

Unsigned right shift: >>> Regardless of whether the sign bit is 0 or 1, add 0 uniformly.

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46551861/article/details/107387430