One . Packaging :

1. Packaging :

Java Language is an object-oriented language , however Java The basic data types in are not object-oriented . Basic types of data do not have " object " Characteristics of ( There are no member variables and member methods to call ), therefore ,java The corresponding classes are designed for each data type , Packaging .

Basic data type The corresponding packaging class
byte Byte
short Short
int Integer
long Long
char Character
floar Floar
double Double
boolean Boolean

(1) All packaging classes are final type , So you can't create their subclasses .
(2) Packaging class is immutable class , The object of a wrapper class has been created , The basic types of data it contains cannot be changed .

2. The characteristics of packaging class :

Two .Integer class

1. Packing and unpacking

Packing : Change the basic data type into a wrapper class object .
Unpacking : Take out the basic data type of the package in the wrapper class .

int To Integer Integer To int
Use Integer Construction method of class call Integer Class intValue() Method
Use Integer Intra class valueOf( ) Method Automatic dismantling (JDK1.5)
Automatic boxing (JDK1.5) To nteger class :

Three methods :
(1) Use Integer Construction method of class
(2) Use Integer Intra class valueOf( ) Method
(3) Automatic boxing

(1) Method 1: Use Integer Construction method of class

  • public Integer(int value): take int Type conversion to Integer class .
  • public Integer(String s): take String Type conversion to Integer class .
    Be careful : When using this construction method , The characters in a string must consist of numbers , It can't be letters or other forms , Otherwise throw NumberFormatException.
public static void main(String[] args)
    // The way 1:public Integer(int value)
    int i= 100;
    // take int Type of i Convert to Integer Type of ii
    Integer ii = new Integer(i);

    // The way 2:public Integer(String s)
    String s = "100";
    //String s = "abc"; error , Because the string must consist of numeric characters . 
    Integer iii = new Integer(s);

(2) Method 2: Use Integer Intra class valueOf Method

public static Integer valueOf(int i): take int Type conversion to Integer class .

Integer i3 = Integer.valueOf(30);

(3) Method 3: Automatic packing method :
in fact , The compiler automatically executed valueOf Method .

Integer ii = 100;// The compiler automatically executed Integer ii = Integer.valueOf(100)    
Packaging Packing Unpacking
Byte valueOf() byteValue()
Short valueOf() shortValue()
Integer valueOf() intValue()
Long valueOf() longValue()
Float valueOf() floatValue()
Double valueOf() doubleValue()
Character valueOf() charValue()
Boolean valueOf() booleanValue()

valueOf Source code analysis :

public static Integer valueOf(int i) {
     assert IntegerCache.high>= 127;
     if (i >= IntegerCache.low&& i <= IntegerCache.high)
     return IntegerCache.cache[i+ (-IntegerCache.low)];
     return new Integer(i); 

By looking at the source code, we can see that ,java in the light of -128-127 Between the data made a data buffer pool .
If the data is in this range , No new space is created each time .
If the data is in this range , Just new A space

Relevant interview questions :

public static void main(String[] args)
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);//false
        System.out.println(i1.equals(i2));//true Rewrote equals Method , The content of the comparison 

        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);//false

        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);//false

        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true

3.Integer Class to int:

(1) Call the intValue() Method
(2) By automatic unpacking :

  • Method 1: Call the intValue() Method
    public int intValue(): With int Type returns the Integer Value .
  public void test2() {
    Integer i1 = new Integer(14);
    Float f1 = new Float(12.3F);
    //1. Call the XxxValue() Method 
    int i2 = i1.intValue();//i2 = 14
    float f2 = f1.floatValue();//f2 = 12.3
  • Method 2: By automatic unpacking :
    The compiler automatically executed valueOf Method
        //5 It's the basic data type , By automatically boxing into object type .
        // The compiler executed Integer iii = Integer.valueOf(5)
        Integer iii=5;
        // Automatic dismantling , Actually  int iii2 = iii.intValue()
        int iii2=iii;
        System.out.println(iii2); Type data and String The transformation between forms :

int To String String To int
String splicing String–>Integer–>int
String Class valueOf() Method int parseInt(String s)
toString(int i)
public static void main(String[] args)
        //int--String hold int Type data is converted to the form of a string 
        int number = 100;

        // The way 1: String splicing 
        String s1 = "" + number;

        // The way 2:String Class valueOf You can convert any type of data into String type 
        String s2 = String.valueOf(number);

        // The way 3
        //int -- Integer -- String
        Integer i = new Integer(number);
        String s3 = i.toString();

        // The way 4
        //public static String toString(int i) Returns a representation of the  Integer  It's worth it  String  object .
        String s4 = Integer.toString(number);

        String s ="100";
        // The way 1
        Integer ii = new Integer(s);
        //public int intValue() With  int Type returns the  Integer Value 
         int x = ii.intValue();

        // The way 2
        //public static int parseInt(String s)
        int y = ii.parseInt(s);

5. Base conversion :

Conversion between bases Method explain
Decimal to binary toBinaryString(int i) Convert a decimal integer to a binary number in string form
Decimal to octal toOctalString(int i) Convert a decimal integer to an octal number in string form
Decimal to hexadecimal toHexString(int i) Convert a decimal integer to a hexadecimal number in string form
Decimal to other bases toString(int i,int radix) Convert a decimal number to a string radix Hexadecimal number ,radix Is a decimal value
Other base to decimal int parseInt(String s, int radix) Will a radix Convert a decimal number to a decimal number
public static void main(String[] args)
        // Decimal to binary , octal , Hexadecimal 

        // Decimal to other bases 

        // Other bases are converted to decimal 
        //System.out.println(Integer.parseInt("123",2)); error , because 2 There is no system 3

3、 ... and .Character class

1.Character Class Overview :
Character Class wraps a char Value , Used to manipulate a single character .
This class provides several ways to manipulate characters , To determine the type of characters ( Lowercase letters , Numbers , wait ), And convert characters from uppercase to lowercase, etc .

2.Character Construction method of class :

  • public Character(char value): take char Type conversion to Character class .
Character ch1 = new Character('a');
System.out.println("ch1:"+ch1);  //ch1:97

Be careful :char and Character The conversion method between classes and int and Integer The conversion method between classes is the same

3.Character Class method :

Serial number Method explain
1 isDigit() Whether it's a numeric character
2 isWhiteSpace() Is it a space
3 isUpperCase() Is it a capital letter
4 isLowerCase() Is it a lowercase letter
5 toUpperCase() Specifies the uppercase form of the letter
6 toLowerCase() Specifies the lowercase form of the letter
7 toString() Returns the string form of a character
public static void main(String[] args)
        //public static boolean isUpperCase(int codePoint) Determine whether a given character is an uppercase character 

        //public static boolean isLowerCase(char ch): Determine whether a given character is a lowercase character 

        // public static boolean isDigit(int codePoint): Determine whether a given character is a number 

        //public static char toUpperCase(char ch): Convert a given character to an uppercase character 

        //public static char toLowerCase(char ch): Convert a given character to a lowercase character