java八种基本数据类型及其包装类
【摘要】 八种基本数据类型以及包装类 1. 基本数据类型 byte 占用1个字节(8位),范围:-2^7~2^7-1 short 占用2个字节(16位),范围:-2^15~2^15-1 int 占用4个字节(32位),范围:-2^31~2^31-1 long 占用8个字节(64位),范围:-2^63~2^63-1 float 占用4个字节(32位,1位符号位,8位指数位),范围:2^...
八种基本数据类型以及包装类
1. 基本数据类型
byte
占用1个字节(8位),范围:-2^7~2^7-1
short
占用2个字节(16位),范围:-2^15~2^15-1
int
占用4个字节(32位),范围:-2^31~2^31-1
long
占用8个字节(64位),范围:-2^63~2^63-1
float
占用4个字节(32位,1位符号位,8位指数位),范围:2^-149~2^128-1
double
占用8个字节(64位,1位符号位,11位指数位),范围2^-1074~2^1024-1
表格表示:
2. 包装类:
short → Short
int → Integer
long → Long
char → Character
byte → Byte
float → Float
boolean → Boolean
double → Double
包装类中提供了更多的方法来对数据进行操作。
基本数据的值存放在栈中,包装类是在堆中分配空间给对象,栈中存放的是对象的引用的地址。因此,包装类的效率会比基本数据类型的效率要低。
3. 包装类的装箱与拆箱
- 装箱
以Integer为例。
将基本数据类型变为包装类,例如
Integer i = 2;
这里自从jdk15后可以这样写,jdk15之前必须显示的new Integer(2),这里实际上是自动调用了Integer.valueOf(int i)方
法,Integer.valueOf(int i)源码如下
-
Java 代码
1 |
public static Integer valueOf(int i) { |
|
2 |
if (i >= IntegerCache.low && i <= IntegerCache.high) |
3 |
return IntegerCache.cache[i + (-IntegerCache.low)]; |
|
4 |
return new Integer(i); |
5 |
} |
发现里边有个IntegerCache,是什么东西呢。再看源码
-
Java 代码
01 |
private static class IntegerCache { |
|
02 |
static final int low = -128; |
03 |
static final int high; |
|
04 |
static final Integer cache[]; |
05 |
||
06 |
static { |
07 |
// high value may be configured by property |
|
08 |
int h = 127; |
09 |
String integerCacheHighPropValue = |
||
10 |
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); |
11 |
if (integerCacheHighPropValue != null) { |
|
12 |
try { |
13 |
int i = parseInt(integerCacheHighPropValue); |
|
14 |
i = Math.max(i, 127); |
15 |
// Maximum array size is Integer.MAX_VALUE |
16 |
h = Math.min(i, Integer.MAX_VALUE - (-low) -1); |
17 |
} catch( NumberFormatException nfe) { |
|
18 |
// If the property cannot be parsed into an int, ignore it. |
19 |
} |
|
20 |
} |
21 |
high = h; |
|
22 |
23 |
cache = new Integer[(high - low) + 1]; |
|
24 |
int j = low; |
25 |
for(int k = 0; k < cache.length; k++) |
|
26 |
cache[k] = new Integer(j++); |
27 |
||
28 |
// range [-128, 127] must be interned (JLS7 5.1.7) |
29 |
assert IntegerCache.high >= 127; |
|
30 |
} |
31 |
||
32 |
private IntegerCache() {} |
33 |
} |
可以看出这个类中已经默认创建了值为-128~127的Integer对象,所以当我们赋值的时候如果变量值在这个范围内,则会直接引用这个存在的对象,不必再new新对象,这种缓存 机制,可以减少大量的new对象,提高效率,并且其中的high值是可以配置的。配置方法
-XX:AutoBoxCacheMax=< size >
float、double类似。
对此,有以下代码:
-
Java 代码
1 |
public static void main(String[] args) { |
|
2 |
Integer a = 1; |
3 |
Integer b = 1; |
|
4 |
System.out.println(a==b); |
5 |
||
6 |
Integer c = 200; |
7 |
Integer d = 200; |
|
8 |
System.out.println(c==d); |
9 |
} |
输出为:
-
Java 代码
1 |
true |
|
2 |
3 |
false |
在配置了high的值(例如1000)后输出都为true.
因为a和b在[-128,127]之间,引用的是同一个对象,所以相等。
而c和d虽然值都为200,但是不在[-128~127]之间,是两个完全不同的对象,所以不相等。
所以,判断两个Integer的值是否相等,应该用equlas方法。
- 拆箱
将包装类变为基本数据类型,例如
-
Java 代码
1 |
Integer i = 2; |
|
2 |
3 |
int j = i; |
这里默认调用了intValue()方法实现自动拆箱
intValue()的源码很简单:
-
Java 代码
1 |
public int intValue() { |
|
2 |
return value; |
3 |
} |
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)