Fundamentals of Java -- basic data types

1. Basic data type

typepositionbytecharacteristicData range
byte81-2 ^ 7 ~ 2 ^ 7, the first digit indicates positive and negative
char1620 ~ 2^16-1
short162-2 ^ 15 ~ 2 ^ 15-1, the first digit indicates positive and negative
int324Default type of integer in java-2 ^ 31 ~ 2 ^ 31-1, the first digit indicates positive and negative
long648Add L at the end of the number to identify the long type-2 ^ 63 ~ 2 ^ 63-1, the first digit indicates positive and negative
float324You need to add F / f at the end of the number-2 ^ 31 ~ 2 ^ 31-1, the first digit indicates positive and negative
double648The default type of floating-point number in java. Add D at the end of the number to identify it as double type-2 ^ 63 ~ 2 ^ 63-1, the first digit indicates positive and negative
booleanThere is no precise definition givenThere is no precise definition

1.1 level of basic data type

  • Byte < short < int < long in integer

  • Float < double in floating point numbers

In java basic data operation, if "low-level" data type and "high-level data type" operation are involved, the result will be automatically transformed to "high-level data type".

Readers can understand ↓ through the operation practice in 1.2

1.2 operation of basic data type

1.2.1 char

//practice whether the following questions are compiled successfully
    char c1 = 97; 
	char c2 = 97; 
    1.c1 = c1 + 1; 
	2.System.out.println(c1);
//output: 
	1.Compilation failed, char Type and int Add types and the result is int type(Automatic upward transformation),and char It is 2 bytes and cannot hold 4 bytes of data. It is necessary to forcibly convert the operation result to char type(Forced downward transformation) : c1 = (char) (c1 + 1); Or use int Variable of type to receive operation results: int i = c1 + 1;
     2.b // 97 in ASCII table corresponds to 'a'

1.2.2byte

//practice whether the following questions are compiled successfully
    byte b1 = 3;
    byte b2 = 4;
    1. byte b3 = b1 + b2; 
    2. byte b4 = 3 + 4;
    3. b3 += b1; 
    byte b5 = 'a';
    byte b6 = 'a';
    4. byte b7 = b5 + b6; 
    5. System.out.println(b7);
    6. byte b8 = 128; 

//output
	1.Compilation failed, automatic upward transition to int,Strong rotation is required
    2.Compiled successfully
    3.The compilation is successful. The principle is shown below. one.3 Assignment Operators 
    4.Compilation failed, automatic upward transition to int,Strong rotation is required
    5.The output is 194 because byte Is an integer type,'a' stay ASCII Corresponding number 97 in the table
    6.Compilation failed, exceeding the data range

1.2.3 short

//practice whether the following questions are compiled successfully
    1.short s1 = 'a';
	short s2 = 2;
    2.short s0 = 'ab'; 
    3.short s5 = 'you'; 
    4.short s3 = s1 + s2; 
    5.short s4 = 3 + 4;
    6.s3 += s2; 
    short s6;
    Member variable s5;(Not assigned by code)
    7.s5 += 3; 
    8.s6 += 3; 

//output
	1.Compiled successfully
    2.Compilation failed, although short It's two bytes, but java A single quotation mark can contain only one character.
    3.After compilation, Chinese characters take up two bytes
    4.Compilation failed, automatic upward transition to int
    5.Compiled successfully
    6.Compiled successfully,**The principle is shown below, 1.3 Assignment Operators **
    7.The compilation is successful, and the member variable has been assigned during class loading
    8.Compilation failed, s6 Not initialized yet

1.2.4 float

//practice whether the following questions are compiled successfully
    float f1 = 1.0F;
    float f2 = 2.0F;
    float f3 = 2.0; //If the compilation fails, the floating-point number is double by default. F should be added at the end of the number to indicate strong conversion
    1.float f4 = f1 + f2;
    2.float f5 = 1.0 + 2.0; 
    3.float f6 = 1.0F + 2.0; 

//output
	1.Compiled, f1,f2 Has been declared as float Type, two float Type addition has not been completed float type
    2.Compilation failed, 1.0 And 2.0,Not added at the end f,stay Java The default floating point number in is double Type, double Types are added as double Type, cannot be directly double Type value assigned to float Type variable.
    3.Compilation failed, 2.0 by double Type, and 1.0F Add automatically up to double Type.

1.3 comparison operator

  1. '>' 2. '<' 3.'==' 4. '<=' 5. ' >=' 6.'!='

1.4 assignment operator

= ,+=, -=, *=, /=

Except =, others are special assignment operators, and their meaning can be understood through the following examples

int i1 = 100;
i1 += 1.0f; //Compiled successfully

The second line of code is equivalent to i1 = (int) (100 + 1.0f);

The special assignment operator will perform corresponding type conversion on the result after calculation, and then assign the result to the variable.

1.5 self increasing and self decreasing operators

In Java, non separable operations are called atomic operations. Self increasing and self decreasing are non atomic operations.

Example: i + +. The JVM decomposes the auto increment operation into four atomic operations

// javap decompiles i + +, and the results are as follows
getfield     take out i Value of
iconst_1	Press the constant 1 on the stack to prepare for the operation
iadd		operation i + 1
putfield	Assign the operation result to i	 	

1.6 logical operators

  • &&: and

(condition1 & & condition2) returns true when both conditions are true. Returns false if at least one of the conditions is false.

&&It has short-circuit effect. When the condition on the left is false, it is enough to determine the result, then the condition on the right will not be judged. So as to save some performance

  • ||: or

(condition1 | condition2) return false only when both conditions are false. Other results return true.

&&It has short-circuit effect. When the condition on the left is true, it is enough to determine the result, then the condition on the right will not be judged. So as to save some performance

  • ! : wrong

(! condition1) returns true when the condition is false. Otherwise, false is returned.

1.7 bit operator

Bit operators are only evaluated for integers, and bit operations have the lowest priority

  • '&': according to phase and, if the corresponding bits of two binary numbers are 1, the result is 1, otherwise it is 0.
num1 & num2 = num3 
num3 Must be less than or equal to num1,num2
Eg:
	 00001110 
   & 00000101
  ------
     00000100   

In Java, calculating array subscripts through the '&' operator can be seen everywhere. For ex amp le, in the putVal() method of HashMap:

if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null); 

n - 1 is the maximum value of the array subscript, and hash is the hash value of the new element.
(n-1) & hash when these two values are combined by phase, only the maximum number not greater than n-1 can be obtained, which avoids the possibility of array out of bounds.

  • '|': by bit or, if the corresponding bits of two binary numbers are 0, the result is 0, otherwise it is 1.
Eg:
	 00001110 
   | 00000101
  ------
     00001111   
  • '^': bitwise exclusive or. If the corresponding bit values are the same, the result is 0, otherwise it is 1
Eg:
	 00001110 
   ^ 00000101
  ------
     00001011   
  • '~': negation, bitwise negation operator flips each bit of the operand, that is, 0 becomes 1 and 1 becomes 0
int a = 10;
int b = ~a;
a: 1010
b: 0101

Exercise: use addition to subtract through bit operation

//Complement = inverse code + 1, and complement represents the opposite number
int i = 10;
System.out.println(Integer.toBinaryString(i));
int ii = 7;
System.out.println(Integer.toBinaryString(ii));
System.out.println(Integer.toBinaryString(~ii));
System.out.println(Integer.toBinaryString(~ii+1));
int iii = i + (~ii+1); //Original code + complement
System.out.println(Integer.toBinaryString(iii));
System.out.println(iii);

//output
1010
111
11111111111111111111111111111000
11111111111111111111111111111001
11
3
  • ’< < ': logical shift left
num << n; amount to num * (2^n)
Shift left one bit, equivalent to * 2. Shift left n position * 2^n
 stay Java Often used in i + j << 1,replace (i + j) * 2
i + j << 1 in i+j Do not use parentheses because bit operations have the lowest default priority
  • '> >': the arithmetic moves to the right and the sign bit remains unchanged
num >> n; amount to num / (2^n)
Moving one bit to the right is equivalent to / 2. Shift right n Bit: / 2^n
    
stay Java Often used in i + j >> 1,replace (i + j) / 2
i + j >> 1 in i+j Do not use parentheses because bit operations have the lowest default priority
    
Eg: ArrayList Capacity expansion mechanism in: grow() method
    int newCapacity = oldCapacity + (oldCapacity >> 1);
	The new capacity is 1% of the old capacity.5 times
  • '> >': logical shift right. When the integer number is positive, it is the same as arithmetic shift right
int bit1 = 2;
int bit2 = -8;
System.out.println(bit1 + bit2 >> 1);
System.out.println(bit1 + bit2 >>> 1);
System.out.println(Integer.toBinaryString(-6));
System.out.println(Integer.toBinaryString(bit1 + bit2 >>> 1));
System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));

//output
-3
2147483645
11111111111111111111111111111010
1111111111111111111111111111101 //"31 bit + sign is 0" - > 011111111111111111111111101
1111111111111111111111111111111 //"31 bit + sign is 0" - > 011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

As can be seen from the above example, when '> > >' is shifted to the right, the symbol bit will also be shifted to the right. If it is a negative number, the symbol bit will be 1, and after shifting to the right, the symbol bit will be 0

1.8 ternary operator

(condition) ? Expression 1: expression 2;

When condition is true, execute expression 1; When condition is false, expression 2 is executed

Added by countrydj on Wed, 02 Mar 2022 02:32:26 +0200