java 99% 是对象处理的,还有1%是primitive(int,short,char...等字面值)的处理,与之相对的wrapper class 是Integer,Short,Character...等,因此,总需要要在primitive和wrapper之间不断的转换...
Tiger来使用boxing和unboxing来解决这个问题...自动执行这些转换...即autoboxing和auto-unboxing..
1.primitive to wrapper
[quote]
[color=blue]//Tiger之前定义个Integer必须如下[/color]
Integer i = new Integer(100);
[color=blue]//Tiger只需这样[/color]
Integer i = 100;
int j = 100;
Integer x = j;
[/quote]
还有,JVM将特定primitive都给box成相同的不可变的wrapper对象(ture,false -128——127,部分char),放在缓存...高效率。
看下面两个例子..
[quote]
Integer counter1 = 100;
Integer counter2 = 100;
System.out.print("counter1 == counter2: " );
System.out.println(counter1 == counter2);
Integer counter3 = 256;
Integer counter4 = 256;
System.out.print("counter3 == counter4: ");
System.out.println(counter3 == counter4);
[/quote]
结果输出:
[quote]
counter1 == counter2: true
counter3 == counter4: false
[/quote]
Why?
[color=blue]因为counter1 和counter2 的值在-128——127之间,是不可变的wrapper,所以会将Integer boxing 成int类型,然后进行比较,所以是相等的。[/color]
[color=blue]因为counter3 和counter4 的值不在-128——127之间,没有将Integer进行boxing因此比较的还是对象(两个对象是在不同的内存空间),所以不相等的。[/color]
2.wrapper to primitive
[quote]
Integer j = 100;
int i = j;[color=blue]//自动将wrapper转换为primitive[/color]
[/quote]
但是如果是这样..
[quote]
Integer j = null;
int i = j;[color=blue]//将抛出NullPointerExcepion异常,所以建议初始换wrapper对象的值.[/color]
[/quote]
3.wrapper的递增与递减
wrapper对象也可以递增与递减,如
[quote]
Integer counter = 1;
counter++;
--counter;
[color=blue]//先把Integer先unboxing成int,递增或递减,然后再boxing回去[/color]
[/quote]
4.Boolean and boolean
通过Boolean 也可以直接使用逻辑运算符(||、&&、!....),先unboxing成primitive(不可变的wrapper),再做比较...如
[quote]
Boolean case1 = true;
Boolean case2 = true;
Boolean case3 = false;
[color=blue]//Boolean对象先会被unboxing成primitive,然后进行比较,再boxing最后值成为Wrapper对象赋值给result[/color]
Boolean result = (case1 || case2) && case3;
[/quote]
Tiger来使用boxing和unboxing来解决这个问题...自动执行这些转换...即autoboxing和auto-unboxing..
1.primitive to wrapper
[quote]
[color=blue]//Tiger之前定义个Integer必须如下[/color]
Integer i = new Integer(100);
[color=blue]//Tiger只需这样[/color]
Integer i = 100;
int j = 100;
Integer x = j;
[/quote]
还有,JVM将特定primitive都给box成相同的不可变的wrapper对象(ture,false -128——127,部分char),放在缓存...高效率。
看下面两个例子..
[quote]
Integer counter1 = 100;
Integer counter2 = 100;
System.out.print("counter1 == counter2: " );
System.out.println(counter1 == counter2);
Integer counter3 = 256;
Integer counter4 = 256;
System.out.print("counter3 == counter4: ");
System.out.println(counter3 == counter4);
[/quote]
结果输出:
[quote]
counter1 == counter2: true
counter3 == counter4: false
[/quote]
Why?
[color=blue]因为counter1 和counter2 的值在-128——127之间,是不可变的wrapper,所以会将Integer boxing 成int类型,然后进行比较,所以是相等的。[/color]
[color=blue]因为counter3 和counter4 的值不在-128——127之间,没有将Integer进行boxing因此比较的还是对象(两个对象是在不同的内存空间),所以不相等的。[/color]
2.wrapper to primitive
[quote]
Integer j = 100;
int i = j;[color=blue]//自动将wrapper转换为primitive[/color]
[/quote]
但是如果是这样..
[quote]
Integer j = null;
int i = j;[color=blue]//将抛出NullPointerExcepion异常,所以建议初始换wrapper对象的值.[/color]
[/quote]
3.wrapper的递增与递减
wrapper对象也可以递增与递减,如
[quote]
Integer counter = 1;
counter++;
--counter;
[color=blue]//先把Integer先unboxing成int,递增或递减,然后再boxing回去[/color]
[/quote]
4.Boolean and boolean
通过Boolean 也可以直接使用逻辑运算符(||、&&、!....),先unboxing成primitive(不可变的wrapper),再做比较...如
[quote]
Boolean case1 = true;
Boolean case2 = true;
Boolean case3 = false;
[color=blue]//Boolean对象先会被unboxing成primitive,然后进行比较,再boxing最后值成为Wrapper对象赋值给result[/color]
Boolean result = (case1 || case2) && case3;
[/quote]