Home >Java >javaTutorial >How to use Wrapper in Java
Packaging classes | |
---|---|
| Byte
|
| Short
|
| Integer
|
| Long
|
Float |
| ##double
Double |
| boolean
Boolean |
##char |
Character
|
3. Use |
int i = 500; Integer t = new Integer(i);
Float f = new Float(“4.56”); //Long l = new Long(“asdf”); 报错:NumberFormatException
Integer in3 = new Integer("123abc"); System.out.println(in3.toString()); /* 报错: Exception in thread "main" java.lang.NumberFormatException: For input string: "123abc" */2. Packaging class - basic Data type (unboxing)⭕ Call the
xxxValue()
method of the packaging class:boolean b = bObj.booleanValue();
JDK1.5, automatic support is supported Packing, automatic unboxing. But the type must match
⭕ Implemented through the constructor of the wrapper class:
int i = new Integer(“12”);⭕ Through the
Float f = Float.parseFloat(“12.1”);⭕ Error example:
int num1 = (int)str1; Integer in1 = (Integer)str1; //可能会报NumberFormatException
4. Basic data type/packaging class-String
⭕ Call string heavy Loaded valueOf()
method:String fstr = String.valueOf(2.34f);
String intStr = 5 + "";
5. Conversion graph
//(1) Object o1 = true ? new Integer(1) : new Double(2.0); System.out.println(o1);//1.0 Object o2; if (true) o2 = new Integer(1); else o2 = new Double(2.0); System.out.println(o2);//1 } //(2) //java Integer i = new Integer(1); Integer j = new Integer(1); System.out.println(i == j);//false Integer m = 1; Integer n = 1; System.out.println(m == n);//true Integer x = 128; Integer y = 128; System.out.println(x == y);//false
The above is the detailed content of How to use Wrapper in Java. For more information, please follow other related articles on the PHP Chinese website!