Java basics - data types, operators and structures

1 data type

insert image description here
The smallest unit of data operation -
the smallest unit of bit data - byte

type of data Size (unit bit) Corresponding packaging class
byte 8 (1 byte) Byte
short 16 (2 bytes) Short
int 32 (4 bytes) Integer
long 64 (8 bytes) Long
float 32 (4 bytes) Float
double 64 (8 bytes) Double
char 16 (2 bytes) Character
boolean - Boolean

2 data type conversion

Convert basic data types to each other

First sort the basic data types according to the value range from small to large:
byte < short < int < long < float < double

  • Assign a small value to a large one, assign directly, and Java will automatically convert;
  • Assignment of a large value to a small one, forced conversion, will lose precision;
  • Java will default the decimal to double
float f = 1.0; //错误写法
float f = 1.0f; //正确写法,后缀f大小写都可以

Convert between basic data types and wrapper classes

The basic data type has no concept of object, and the reference data type is equivalent to a class, which has the concept of object and is very convenient to operate.
In order to facilitate the operation of basic data types, the basic data types also have the concept of objects, and there are corresponding packaging classes.

  • Convert primitive data types to wrapper classes
int i = 10;
Integer j = i; 
//就是如此直接,实际上完整写法是下面这样,只不过Java虚拟机自动搞定了,这个过程称为自动装箱
Integer j = Integer.valueOf(i);
  • Wrapper classes converted to primitive data types
int k = j;
//同上,这个过程称为自动拆箱
int k = j.intValue();

3 Differences between types and generics

  • There is polymorphism in types, but there is no polymorphism in generic types
  • Types are used to constrain the usage scenarios of external objects, and generics are used to constrain the usage scenarios of internal objects

3 operators

insert image description here

  • Elements and operators are combined to obtain an expression. The minimum type of the expression operation result is int, and the maximum is the largest element type;
  • Assignment operators except = are all compound operators, and the characteristic is that the data type will not change after the operation;
  • What is the difference between & and &&?
    & must execute the second expression, && does not necessarily execute the second expression.
    For &&, if the first expression is false, the result is set to false, and the second expression will not be judged.
    The difference between | and || is the same. For ||, if the first expression is true, the result will be true.

4 structure

Branch structure switch

int i = 10;
switch (i) {
    
    
	case 10:
		输出1;
	case 20:
		输出2;
	default:
		输出其它;
}

结果:
1
2
其它

分析:
switch语句会依次根据i值与case作等值判断,当判断成功后,会执行该case后的所有语句,除非用break截断
缺点:
只能做等值判断,范围判断不如if方便

The difference between loop structure while and do...while

while(条件){
    
    
	执行语句
}
// 先判断后执行,执行语句最少一次都不执行

do{
    
    
	执行语句
}while(条件)
// 先执行后判断,执行语句最少执行一次

Guess you like

Origin blog.csdn.net/weixin_46838605/article/details/129858843