包装类
1. java是面向对象的语言,是面向对象进行操作的,由于基本类型没有针对属性和方法的操作,需要手动完成。java因此给每个基本类型提供一个对应的类--包装类。
2. 包装类的自动拆装箱。自动拆装箱是JDK1.5推出的新特性。
3. 每个包装类的哈希码是唯一的。--只要不超过int类型范围,整数的哈希码是其本身。
代码分析
public static void main(String[] args) {
// 将基本类型--引用类型:封箱。
int i = 7;
Integer inter = new Integer(i);// 利用构造方法
// 将一个基本类型直接赋值给引用类型--自动封箱(JDK1.5的特性之一)。
// 自动封箱:在底层实际上是调用了对应类的valueOf();
// 例如: public static Integer valueOf(String s);
Integer integer = 8;
// 自动拆箱:将一个 类型的对象直接赋给了对应的基本类型的变量。
// 自动拆箱在底层实际上是调用的对应对象的public int intValue()方法。
int b = integer;
b = integer.intValue();
Double d = 3.25;
double dou = d.doubleValue();
Boolean b1 = true;
boolean b2 = b1.booleanValue();
}
public static void main(String[] args) {
// 自动封箱在底层调用valueOf()
/*
* 如果在-128-127之间这个地址直接指向常量池中的地址。
* 凡是字面量(凡是看见就能确定值,且不变的):都是存在常量池的。例如:整数,小数,字符,字符串,逻辑值(true、false),null。
* 目的都是为了值得复用。节省内存空间 。
* int i= 5; i中存的是常量池中5 的地址。因此也可以认为i为对象。
* 如果不在-128-127之间,则会在堆中开辟空间。栈中存堆空间的地址,堆中存的是常量池中的地址。
*/
/*
* 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) { int i =
* parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum
* array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE -
* (-low)); } high = h;
*
* cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k
* < cache.length; k++) cache[k] = new Integer(j++); } private
* IntegerCache() {} } public static Integer valueOf(int i) { assert
* IntegerCache.high >= 127; if (i >= IntegerCache.low && i <=
* IntegerCache.high) return IntegerCache.cache[i +
* (-IntegerCache.low)]; return new Integer(i); }
*/
Integer i1 = 30;
Integer i2 = 30;
// ==判断的是直接量--对象的地址。
System.out.println(i1==i2);//true
i1 = 300;
i2 = 300;
System.out.println(i1==i2);//false
//当引用类型和基本类型比较的时候,引用类型会自动拆箱为基本类型,在进行比较
System.out.println(new Integer(300)==300);//true
public static void main(String[] args) {
Integer in = new Integer("123");
// 字符串底层子底层是字符数组存储,字符串在转化为整数对象的时候,会一次判断每一位是否是一个素质字符。
// 在判断每一位之间,先判断字符串的首位是否为+ -号。除此,不允许其他字符出现。
System.out.println(in);//123
in = new Integer("+25");
System.out.println(in);//25
/*in = new Integer("0xff625");
System.out.println(in);//NumberFormateException
*/
// 将字符串转化为对应的基本类型
int i = Integer.parseInt("123");
//Boolean类型构造,只要参数不为true,就为false
/*public Boolean(String s) {
this(toBoolean(s));
}
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}*/
Boolean b = new Boolean("salkdf");//false
Boolean b1 = new Boolean(null);//false
//包装类的hashcode()。每个包装类对象对应的哈希码是唯一且不变的(在任何环境下的值都一样)
Double l = 3.25;
System.out.println(l.hashCode());// long bits = doubleToLongBits(value);return (int)(bits ^ (bits >>> 32));
Long lon = 4l;
System.out.println(lon.hashCode());//return (int)(value ^ (value >>> 32));
Character c = 'b';
System.out.println(c.hashCode());// return (int)value;
Boolean b3 = true;
System.out.println(b3.hashCode());//return value ? 1231 : 1237;
System.out.println(0.0/0);//NaN和任何对象都不相等,包括自己。
System.out.println(1/0.0);///Infinity-无穷大
下一篇:
基础系列【九】–日期类