"Java basics of" Java wrapper classes, unpacking and packing

Although the Java language is typical of object-oriented programming language, but one of the eight basic data type does not support object-oriented programming, basic types of data does not have the characteristics of the "object" - does not carry the property, there is no way you can call. They just follow in order to meet human ingrained habits, and indeed simple, effective routine data processing.

Java basic data types for each corresponding class are designed, called packaging (Wrapper Classes), also referred to as the outer cover materials class or data type classes.

Object classes can be packaged in each packaging a respective basic types of data, and provide other useful methods. Packaging an object once it is created (basic types of data values ​​encapsulated) the contents of which can not be changed.

And the corresponding basic types of packaging may be installed for each other: 
• referred to the corresponding packaging containers converted by basic types, such as the packaging int Integer class of objects; 
• packaging referred unboxing conversion to a corresponding base type , for example, an object of the Integer class back to simplified to int.

Application wrapper class

Use eight wrapper class is quite similar, the following is a common scenario. 
1) to achieve conversion of int Integer and 
can be constructed by the method of the Integer class int packing, by the method intValue Integer Integer class will unpacking. E.g:

public class var {

    public static void main(String[] args){
        int m = 500;
        Integer iobj = new Integer(m);
        int n = iobj.intValue();
        System.out.println("n=" + n);

        Integer iobj2 = new Integer(500);
        System.out.println("iobj 等价于 obj2 ?" + iobj.equals(iobj2));
    }
}

operation result:

2) convert the string to an integer of 
a static paseInt () method of class Integer, the string may be converted to an integer, the syntax is:

parseInt(String s, int radix);

Wherein, s is the string to be converted, the radix is ​​binary, optional, defaults to decimal.

Case:

public class var {

    public static void main(String[] args){
        String str[] = {"123", "123abc", "435ssA", "abcxyz", "aa", "a"};
        for(String elemStr : str){
            try{
                int iNumber = Integer.parseInt(elemStr, 10);
                System.out.println("ok."+iNumber);
            }catch(Exception e){
                System.out.println("can not cast to int type.");
            }
        }
    }
}

operation result:

3) to convert the string to an integer 
Integer class has a static toString () method, a string can be converted to an integer. E.g:

public class var {

    public static void main(String[] args){
        int myIntValue = 999;
        String intStr = Integer.toString(myIntValue);
        System.out.println("value is " + intStr);
    }
}

operation result:

Automatic packing and unpacking

The above example requires manual packaging instantiate a class, referred to as manual packing unpacking. Java 1.5 must be manually unpacking boxes before (5.0).

After 1.5 Java automatic packing unpacking, i.e. during the time corresponding to the basic data types and packaging conversion, the system will automatically, which will greatly facilitate the writing of code programmer. E.g:

public class var {

    public static void main(String[] args){
        int m = 500;
        Integer obj = m;
        int n = obj;
        System.out.println("n =  " + n);
        Integer obj1 = 500;
        System.out.println("iobj 等价于 obj2 ?" + obj.equals(obj1));
    }
}

operation result:

Reference: https://blog.csdn.net/y396397735/article/details/79394637

 

Guess you like

Origin www.cnblogs.com/jssj/p/11334194.html