Autoboxing
For various reasons it is useful in java to have object versions of integers.
Auto1.java |
1 | public class Auto1 { |
2 | public static void main(String[] args) { |
3 | Integer i = new Integer(3); |
4 | System.out.println("i="+i.intValue()); |
5 | Character c = new Character('a'); |
6 | System.out.println("c="+c.charValue()); |
7 | Short s = new Short((short)5); |
8 | System.out.println("s="+s.shortValue()); |
9 | Long l = new Long(100l); |
10 | System.out.println("l="+l.longValue()); |
11 | Byte b = new Byte((byte)'c'); |
12 | System.out.println("b="+b.byteValue()); |
13 | Boolean o = new Boolean(true); |
14 | System.out.println("o="+o.booleanValue()); |
15 | Double d = new Double(3.14); |
16 | System.out.println("d="+d.doubleValue()); |
17 | Float f = new Float(4.13f); |
18 | System.out.println("f="+f.floatValue()); |
19 | } |
20 | } |
$ javac Auto1.java
| $ java Auto1
i=3
c=a
s=5
l=100
b=99
o=true
d=3.14
f=4.13
| $ javap java.lang.Number
Compiled from "Number.java"
public abstract class java.lang.Number extends java.lang.Object implements java.io.Serializable{
public java.lang.Number();
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue();
public short shortValue();
}
|
It is also possible to access their value through the base class Number.
Auto2.java |
1 | public class Auto2 { |
2 | public static void main(String[] args) { |
3 | Number n = new Integer(3); |
4 | System.out.println("n="+n.intValue()); |
5 | System.out.println("n="+n.doubleValue()); |
6 | |
7 | n = new Double(3.14); |
8 | System.out.println("n="+n.intValue()); |
9 | System.out.println("n="+n.doubleValue()); |
10 | } |
11 | } |
$ javac Auto2.java
| $ java Auto2
n=3
n=3.0
n=3
n=3.14
|
Autoboxing is a feature by which primitive types and their object
counterparts may undergo automatic conversion:
Auto3.java |
1 | public class Auto3 { |
2 | public static void main(String[] args) { |
3 | Number n = 3; |
4 | System.out.println("n="+n.intValue()); |
5 | System.out.println("n="+n.doubleValue()); |
6 | |
7 | n = 3.14; |
8 | System.out.println("n="+n.intValue()); |
9 | System.out.println("n="+n.doubleValue()); |
10 | } |
11 | } |
$ javac Auto3.java
| $ java Auto3
n=3
n=3.0
n=3
n=3.14
|
This is very useful (as you are about to see) except there's one
potential piece of confusion...
Auto4.java |
1 | public class Auto4 { |
2 | public static void main(String[] args) { |
3 | Number i1 = 3; |
4 | Number d1 = 3.0; |
5 | if(i1 == d1) |
6 | System.out.println("equal #1a is true"); |
7 | if(i1.equals(d1)) |
8 | System.out.println("equal #1b is true"); |
9 | if(i1.doubleValue() == d1.doubleValue()) |
10 | System.out.println("equal #1c is true"); |
11 | |
12 | int i2 = 3; |
13 | double d2 = 3.0; |
14 | if(i2 == d2) |
15 | System.out.println("equal #2 is true"); |
16 | } |
17 | } |
$ javac Auto4.java
| $ java Auto4
equal #1c is true
equal #2 is true
|
Test for equality in the object version does not work the same way
as it does for primitives. It's something to remember and work with.
The various integer, short, etc. classes are also useful for converting
Strings to other types.
Convert.java |
1 | public class Convert { |
2 | public static void main(String[] args) { |
3 | Integer i = new Integer("407"); |
4 | Double d = new Double("2.718"); |
5 | System.out.println("i="+i+", d="+d); |
6 | } |
7 | } |
$ javac Convert.java
| $ java Convert
i=407, d=2.718
|
|