Java se note 9 - basic data type object wrapper class

I. concept

In order to facilitate the operation of basic data type values, they are encapsulated into objects, in which properties and behaviors are defined.
The class used to describe the object is called the basic data type object wrapper class.

Basic data type Basic data type object wrapper class
byte Byte
short Short
int Integer
long Long
boolean Boolean
float Float
double Double
char Character

2, Common functions

Used for conversion between basic data type and string type

1. Convert basic data type to string
/ / method 1
 Basic data type value + string;
//Method 2: use toString method
 static basic data type object wrapper class. ToString (basic data type value);

Example:

class IntegerDemo{
    public static void main(String[] args){
        Integer i=2;
        //1. Basic data type value + string
        System.out.println(i+"ab");
        //2. Basic data type object wrapper class. ToString (basic data type value)
        System.out.println(Integer.toString(34));
    }
}

The operation result is:

2. Convert string to basic data type
//Method 1: static method, which can be called directly by the basic data type object wrapper class
 static basic data type a = basic data type object wrapper class. parse basic data type object wrapper class (string);
//Method 2: non static method, to be called with object
 Basic data type object wrapper class i=new basic data type object wrapper class (string);
Basic data type a=i. Basic data type Value();

Example 1:

class IntegerDemo{
    public static void main(String[] args){
        int a=Integer.parseInt("123");
        System.out.println(a+4);
        double b=Double.parseDouble("12.23");
        System.out.println(b+4);
        boolean c=Boolean.parseBoolean("true");
        System.out.println(c);
    }
}

The operation result is:

Example 2:

class IntegerDemo{
    public static void main(String[] args){
        /*
        int a=Integer.parseInt("123");
        System.out.println(a+4);
        */
        Integer a=new Integer("123");
        System.out.println(a.intValue()+4);
        /*
        double b=Double.parseDouble("12.23");
        System.out.println(b+4);
         */
        Double b=new Double("12.23");
        System.out.println(b.doubleValue()+4);
        /*
        boolean c=Boolean.parseBoolean("true");
        System.out.println(c);
         */
        Boolean d=new Boolean("true");
        System.out.println(d.booleanValue());
    }
}

The operation result is:

3. Decimal to other base
String integer.tobinarystring (integer value); / / returns the binary string of integer value
 String integer.tohexstring (integer value); / / returns the hexadecimal string of integer value
 String integer. Tooctalstring (integer value); / / returns the octal string of integer value

Example:

class IntegerDemo{
    public static void main(String[] args){
        //Convert to binary string
        System.out.println(Integer.toBinaryString(6));
        System.out.println(Integer.toBinaryString(-6));
        //Convert to hex string
        System.out.println(Integer.toHexString(60));
        //Convert to octal string
        System.out.println(Integer.toOctalString(60));
    }
}

Operation result:

4. Conversion from other base to decimal
int i=Integer.parseInt(String,radix);//radix stands for base number

Example:

class IntegerDemo{
    public static void main(String[] args){
        int x=Integer.parseInt("110",2);
        System.out.println(x);
        int y=Integer.parseInt("110",8);
        System.out.println(y);
        int z=Integer.parseInt("110",10);
        System.out.println(z);
        int m=Integer.parseInt("110",16);
        System.out.println(m);
    }
}

The operation result is:

3, New features

1. Automatic packing and unpacking

The new features after JDK 1.5 facilitate the conversion between basic data types and their corresponding packaging types.
Auto packing: assign the value of a basic data type to its corresponding packing type
Auto unpacking: assign the value of a packing class type to its corresponding basic data type

Example:

class IntegerDemo
{
    public static void main(String[] args)
    {
        Integer x=4;  //Integer x=new Integer(4), auto boxing 4=new Integer(4);
        x=x+4;       //x+4: x is automatically unpacked to int type, and 4 is added, and then and are automatically boxed to x
        System.out.println(x);
    }
}

The operation result is:

2. Packaging data cache

Java caches part of the data of the wrapper class corresponding to some basic data types. When the equivalent object is used again, it is directly obtained from the cache, and no new object will be created again, which can improve the execution performance of the program.

  • byte/short/int/long: the data cache range is - 128 ~ 127, including - 128 and 127
  • Float / double: no data cache range
  • Character: the data cache range is 0-127, including 0 and 127
  • The corresponding packing class of Boolean: the data cache range is true and false

Example:

class IntegerDemo {
    public static void main(String[] args) {
        //1. Integer packaging data cache range: - 128 ~ 127
        Integer i1 = -128;
        Integer i2 = -128;
        System.out.println(i1 == i2);//Output true
        Integer i3 = 128;
        Integer i4 = 128;
        System.out.println(i3 == i4);//Output false

        //2. Floating point wrapper class has no data cache range
        Double d1 = 1.0;
        Double d2 = 1.0;
        System.out.println(d1 == d2);//Output false

        //3. Character packaging data cache range: 0 ~ 127
        Character c1 = 127;
        Character c2 = 127;
        System.out.println(c1 == c2);//Output true

        //4. Data cache range of Boolean wrapper class: true and false
        Boolean b1 = true;
        Boolean b2 = true;
        System.out.println(b1 == b2);//Output true
    }
}

The operation result is:

Published 33 original articles, won praise 0, visited 1300
Private letter follow

Tags: JDK Java

Posted on Sun, 08 Mar 2020 06:52:33 -0400 by jonez