Java中的基本数据类型分四类八种
byte(Byte-1)/short(Short-2)/int(Integer-4)/long(Long-8)
boolean(Boolean-1bit)
char(Character-2)
float(Float-4)/double(Double-8)
括号后是他们的包装类和所占字节大小(Java中的基本数据类型所占字节大小是固定的,和C/C++中不一样)
基本数据类型的默认值:
| Data Type | Default Value (for fields) |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | 'u0000' |
| String (or any object) | null |
| boolean | false |
代码如下:
package com.example.li;
public class TestBaseType {
public static void main(String[] args) {
System.out.println("Byte.SIZE__" + Byte.SIZE / 8);// 1字节
System.out.println("Short.SIZE__" + Short.SIZE / 8);// 2字节
System.out.println("Integer.SIZE__" + Integer.SIZE / 8);// 4字节
System.out.println("Long.SIZE__" + Long.SIZE / 8);// 8字节
System.out.println("Character.SIZE__" + Character.SIZE / 8);// 2字节
System.out.println("Float.SIZE__" + Float.SIZE / 8);// 4字节
System.out.println("Double.SIZE__" + Double.SIZE / 8);// 8字节
// System.out.println(Boolean.SIZE);
}
}
运行结果如下:
Byte.SIZE__1
Short.SIZE__2
Integer.SIZE__4
Long.SIZE__8
Character.SIZE__2
Float.SIZE__4
Double.SIZE__8
最后
以上就是鲤鱼跳跳糖最近收集整理的关于Java中的基本数据类型在内存所占字节的全部内容,更多相关Java中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复