Java wrapper class (note 17)

Java wrapper class

The concept of packaging

The Java language is an object-oriented language, but the basic data types in Java are not object-oriented, which brings a lot of inconvenience in actual use. A corresponding class is represented, so the eight classes corresponding to the basic data types are collectively referred to as wrapper classes, and some places are also translated as outer classes or data type classes.

2. Basic data types correspond to wrapper classes

Basic data types (native classes) - wrapper classes
byte - Byte
short - Short
int   - Integer
long  - Long
float - Float
double- Double
char    - Character
boolean -Boolean

3. Some practical applications of basic data types in related packaging classes

//The example takes the maximum value of the long type
System.out.println(Long.MAX_VALUE);
  
//The example takes the uppercase form of a character
System.out.println(Character.toUpperCase(c));
  
//Example to judge whether a character is a number or not
System.out.println( Character.isDigit('9')); true
  
//Example convert number to string
int x=90; 
x+""; //This works
Integer.toString(x); //This is also possible
  
//Example convert String to numeric type
String a="4.14";
String b="90";
System.out.println(  Float.parseFloat(a) + Integer.parseInt(b) );  =>94.14
Boolean.parseBoolean("true")  => true
  
//example 
Integer x=new Integer("90");   
int a= x.intValue(); Convert a wrapper class object to a basic data type
  
//The example is related to the base
binary
int x=65;
System.out.println(Integer.toBinaryString(x)); // 0B1000001
Octal
System.out.println(Integer.toOctalString(x)); // 0101
hex
System.out.println(Integer.toHexString(x)); // 0x41

//Example: How to generate four digits like 0011,1111,0110 (job)    
int x=new Random().nextInt(16);
System.out.println(Integer.toBinaryString(x)); //Find a way to add 0 to the front
  
====== About Unboxing and Boxing ======
Boxing => turn primitive data type into object type 9 => Integer(9)
Unboxing => turn the object type into a basic data type Integer(9) => 9
  
Integer x=new Integer("90"); //Yes
Integer y=new Integer(90); //Yes
System.out.println(x==y);  //false;
System.out.println(x.equals(y));  //true
      
From jdk1.5 onwards 
Integer x= 5; //can be autoboxed 
x=x+3; //Yes, there is an automatic unboxing operation in the middle, after the operation, it will be automatically boxed
Integer x= null; //Yes
x=x+3; //Error when running, a null pointer exception

Integer a=100;
Integer b=100; 
System.out.println(a==b); //true
But the following writing
Integer a=128;
Integer b=128; 
System.out.println(a==b);  //false


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560840&siteId=291194637