
正文
Java中类型的长度
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
介绍:
Java中有8种基本类型,分别是boolean, char, byte, short, int, long, float, double。他们的长度固定,不是对象。对于有必要将基本类型作为对象处理的情况,java提供了包装器类,这样有个好处是Java编译器和运行时能够更容易的进行优化。由于java的可移植性,每个类型在不同的平台上大小一致。
代码实现:
package self; /**
* Created by Jimmy on 2015/5/18.
*/
public class sizeof {
public static void main(String[] args){
System.out.println("Integer: " + Integer.SIZE/8); //
System.out.println("Short: " + Short.SIZE/8); //
System.out.println("Long: " + Long.SIZE/8); //
System.out.println("Byte: " + Byte.SIZE/8); //
System.out.println("Character: " + Character.SIZE/8); //
System.out.println("Float: " + Float.SIZE/8); //
System.out.println("Double: " + Double.SIZE/8); // 8
//System.out.println("Boolean: " + Boolean.SIZE/8); //true/false
}
}
输出:
Integer: 4
Short: 2
Long: 8
Byte: 1
Character: 2
Float: 4
Double: 8







