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 :
 Packaging

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)

2.int 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);
    System.out.println("ii:"+ii);

    // 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);
    System.out.println("iii:"+iii);//100    
  }

(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
        System.out.println(i3.equals(i4));//true

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

        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);//true
        System.out.println(i7.equals(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);

4.int 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;
        System.out.println("s1:"+s1);

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

        // The way 3
        //int -- Integer -- String
        Integer i = new Integer(number);
        String s3 = i.toString();
        System.out.println("s3:"+s3);

        // 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);
        System.out.println("s4:"+s4);

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

        // The way 2
        //public static int parseInt(String s)
        int y = ii.parseInt(s);
        System.out.println("y:"+y);
    }

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 
        System.out.println(Integer.toBinaryString(100));
        System.out.println(Integer.toOctalString(100));
        System.out.println(Integer.toHexString(100));
        System.out.println("--------");

        // Decimal to other bases 
        System.out.println(Integer.toString(100,10));
        System.out.println(Integer.toString(100,2));
        System.out.println(Integer.toString(100,8));
        System.out.println(Integer.toString(100,15));
        System.out.println(Integer.toString(100,5));
        System.out.println(Integer.toString(100,7));
        System.out.println(Integer.toString(100,-7));
        System.out.println(Integer.toString(100,70));
        System.out.println(Integer.toString(100,34));
        System.out.println(Integer.toString(100,37));
        System.out.println(Integer.toString(100,36));

        // Other bases are converted to decimal 
        System.out.println(Integer.parseInt("100",10));
        System.out.println(Integer.parseInt("100",2));
        System.out.println(Integer.parseInt("100",4));
        System.out.println(Integer.parseInt("100",16));
        System.out.println(Integer.parseInt("100",23));
        //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 
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase('0'));

        //public static boolean isLowerCase(char ch): Determine whether a given character is a lowercase character 
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase('0'));

        // public static boolean isDigit(int codePoint): Determine whether a given character is a number 
        System.out.println(Character.isUpperCase('A'));
        System.out.println(Character.isUpperCase('a'));
        System.out.println(Character.isUpperCase('0'));

        //public static char toUpperCase(char ch): Convert a given character to an uppercase character 
        System.out.println(Character.toUpperCase('a'));

        //public static char toLowerCase(char ch): Convert a given character to a lowercase character 
        System.out.println(Character.toLowerCase('A'));
    }