Java中的基本数据类型和包装类型
Java 中的基本数据按类型可以分为四大类:布尔型、整数型、浮点型、字符型;
这四大类包含 8 种基本数据类型。
- 布尔型:boolean
- 整数型:byte、short、int、long
- 浮点型:float、double
- 字符型:char
这8 种基本类型取值如下:
数据类型代表含义默认值取值包装类boolean布尔型false0(false) 到 1(true)Booleanbyte字节型(byte)0﹣128 到 127Bytechar字符型'u0000'(空)'u0000' 到 'uFFFF'Charactershort短整数型(short)0-
我们可以看到除 char 的包装类 Character 和 int 的包装类 Integer之外,
其他基本数据类型的包装类只需要首字母大写即可。包装类的作用和特点,本文下半部分详细讲解。
这些都是我们很熟悉的知识了,那下面的知识你有了解吗?
你可能不知道的知识点
首先我们来看一道题目? 下面这段代码输出什么呢?
public class Demo1 {
public static void main(String[] args) {
Integer number1 = 127;
Integer number2 = 127;
System.out.println("number1==number2判断的值为" + (number1 == number2));
Integer number3 = 128;
Integer number4 = 128;
System.out.println("number3==number4判断的值为" + (number3 == number4));
}
}
让我们来看一下答案:
「number1」和「number2」均赋值为了127,「number3」和「number4」均赋值为了128,
那为什么「number1==number2」为「true」,「number3==number4」为「false」呢?
我们来看一下「Integer」中的valueOf的源码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
我们看到如果传入的参数在**[IntegerCache.low,IntegerCache.high]「之间就返回」IntegerCache.cache[i + (-IntegerCache.low)]**,如果值没在这里面,就创建一个新对象返回;
实际上这是一个 「高频区间的数据缓存」,我们再来看看IntegerCache类的实现:
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
我们看到IntegerCache.low为-128,IntegerCache.high为127;
所以在通过valueOf方法创建Integer对象的时候,如果数值在**[-128,127]**之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。
与Integer类似,有高频区间数据缓存的包装类还有:
- Byte:缓存区 -128~127
- Short:缓存区 -128~127
- Character:缓存区 0~127
- Long:缓存区 -128~127
- Integer:缓存区 -128~127
我们再来看一下以下代码:
public class Demo2 {
public static void main(String[] args) {
Boolean bool1 = false;
Boolean bool2 = false;
Boolean bool3 = true;
Boolean bool4 = true;
System.out.println("bool1==bool2判断的值为"+(bool1==bool1));
System.out.println("bool3==bool4判断的值为"+(bool3==bool4));
}
}
让我们来看一下答案:
我们来看一下Boolean的valueOf代码:
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
再来看一下TRUE和FALSE的定义:
public static final Boolean TRUE = new Boolean(true);
public static final Boolean FALSE = new Boolean(false);
可以看到它们使用静态 final 定义,就会返回静态值,所以答案2中返回都是true。
Double、Float的valueOf方法的实现是类似的,但是它们的valueOf与Integer、Short、Byte、Character、Long的不同。
我们再看一下下面的代码:
public class Demo3 {
public static void main(String[] args) {
Double d1 = 20.0;
Double d2 = 20.0;
System.out.println("d1==d2判断的值为" + (d1 == d2));
Double d3 = 30.0;
Double d4 = 30.0;
System.out.println("d3==d4判断的值为" + (d3 == d4));
}
}
让我们来看一下答案:
看一下Double类型的valueOf的实现
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
它会返回一个新的Double对象。 看一下Float类型的valueOf的实现
public static Float valueOf(String s) throws NumberFormatException {
return new Float(parseFloat(s));
}
Float类型的valueOf的实现与Double类型类似。
欢迎关注
扫下方二维码即可关注,微信公众号:「code随笔」
http://weixin.qq.com/r/XSh7YwTEzFmzrUAA931P (二维码自动识别)