Bootstrap

【Java】Java易错知识点

Java易错知识点

1

int x = 12L; //错误,L表示用 long 类型表示

2,

boolean x = 3==5; // x 's value is false

3,

x = false == (3==0); // x is true

4, 

System.out.println( 4 / 2.3 ); // output 1.700000003

5,

System.out.println(4.5f == 4.5); // 4.5f会转换成4.5(double类型的)因此true
System.out.println(4.4f == 4.4); // false
//这是因为4.4在浮点数中不能精确表示,
//而4.5可以精确表示出来。

6,

int x = (int) true; // wrong, true不能转换,这里Java跟c++的表示不一样

7,

		long  x = 2;
		switch(x) {   //错误!long类型不能用于switch
		case 2:
			System.out.println(2);

		}

8,

Primitive type: 4 bytes for int, 8 bytes for double, ...

Object reference: 8 bytes.

Array: 24 bytes + memory for each array entry.

Object: 16 bytes + memory for each instance variable + 8 if inner class (for point to enclosing class).

Padding: round up to multiple of 8 bytes.

;