This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

The Java wrapper class

Basic overview

  • In certain situations require all the data content must be the object of class type, and there are eight kinds of basic data types in the Java language statement variables are not objects, in order to make these data on this occasion to be able to use, you need good objectification processing, at this point you need to use wrapper classes will be packaged into an object for use

  • A wrapper class is a class that converts value type data and objects to each other and provides a mechanism for boxing and unboxing

Basic data types Packing type
byte Byte
Boolean Boolean
short Short
char Character
int Integer
long Long
float Float
double Double

Automatic unpacking and packing

  • Automatic unpacking: Automatic conversion of packing classes to basic data types
  • Automatic boxing: Automatic conversion of base data types to wrapper classes

The Integer types

  • The java.lang.Integer class is an indirect subclass of Object and is used to wrap data of type int
  • The class is modified by the final keyword to indicate that the class cannot be inherited
  • Commonly used method
    • This class overrides the equals(), hashCode(), and toString() methods of the Object class
    • Integer(int value) – Constructs the object based on the Integer value specified by the parameter
    • Integer(String s) – Constructs the object based on the String specified by the argument
    • Int intValue() – Gets the int data contained in the calling object and returns it
      • Unpacking an Integer into an int (automatic unpacking)
    • Static Integer valueOf(int I) – Retrieves the object based on the Integer specified by the argument
      • Use to implement wrapping int types into Integer types (auto-boxing)
    • Static int parseInt(String s) – Used to convert character-to-type data to an int
  • case
/* * * like dust */
package packing;

/** * demonstrates the use of the Integer class *@author ruochen
 * @version1.0 * /
public class TestInteger {
	public static void main(String[] args) {
		// Use constructors in the Integer class to construct objects, which have no no-parameter constructors
		
		Integer it = new Integer(123);
		// Automatically call toString() to get a string decimal integer
		System.out.println(it);
		
		Integer it2 = new Integer("234");
		System.out.println(it2);
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		
		// Convert an int to an Integer
		Integer it3 = Integer.valueOf(222);
		System.out.println(it3);  / / type String
		
		int res = it3.intValue();
		System.out.println(res);  / / int type

		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		// Convert String to int
		int res2 = Integer.parseInt("33333");
		System.out.println(res2);
		
		// java.lang.NumberFormatException
		// Each character in the string must be a decimal integer; otherwise, a number format exception occurs
		// int res3 = Integer.parseInt("1234a");
		// System.out.println(res3);
		
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		// Automatic packing and automatic unpacking mechanism
		Integer it4 = 100;  // int -> Integer automatically calls ValueOf()
		res = it4;  // Integer -> int intValue()
		
		
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		Integer it5 = 128;
		Integer it6 = 128;
		Integer it7 = new Integer(128);
		Integer it8 = new Integer(128);
		
		System.out.println(it5.equals(it6));  // true compares content
		System.out.println(it5 == it6);  // false compares addresses
		System.out.println(it7.equals(it8));  // true compares content
		System.out.println(it7 == it8);  // false compares addresses
		
		
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		// Source code 993 lines
		/ / time consuming
		// -128 to 127 are packed in advance
		// There is no need to recreate the object. The two objects point to the same object
		// The above 128 is not in the scope, need to create the object
		Integer it9 = 127;
		Integer it10 = 127;
		// Here are two objects that are manually new by themselves
		Integer it11 = new Integer(128);
		Integer it12 = new Integer(128);
		
		System.out.println(it9.equals(it10));  // true compares content
		System.out.println(it9 == it10);  // false compares addresses
		System.out.println(it11.equals(it12));  // true compares content
		System.out.println(it11 == it12);  // false compares addresses}}Copy the code
123
234
----------------------
222
222
----------------------
33333
----------------------
----------------------
true
false
true
false
----------------------
true
true
true
false
Copy the code

Automatic packing tank (-128~127)

  • To improve performance, an automatic boxing pool is provided inside the Integer class, that is, the Integer between -128 and 127 is packed in advance. If the program needs data in this range, it obtains data directly from the boxing pool without creating new objects

Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news