Java foundation 5 - wrapper class

Java has eight basic types, each of which has a corresponding wrapper class. What is packaging?

A wrapper class is a class with an instance variable inside to store the value of the corresponding basic type. This class generally has some static methods, static variables and instance methods to facilitate data operation

Basic typePackaging

1. Basic usage

The process of converting a basic type to a packing class is generally called "packing", while the process of converting a packing type to a basic type is called "unpacking".

Integer a = Integer.valueOf(1);		// Packing
int b = a.intValue();		// Unpacking

// structure
Integer a = new Integer(1);

From the bytecode, we find that boxing actually calls the valueOf() method of the wrapper class, and unpacking actually calls the xxxValue() method.


  • Integer i = 10 is equivalent to Integer i = Integer.valueOf(10)
  • Int n = I is equivalent to int n = i.intValue();

1.1 common points of packaging

  1. Override Object method

  2. Implement Comparable interface

  3. Wrapper class and String method

  4. Common constants

  5. The common parent class Number. Through various methods defined in the parent class, the wrapper class instance can return any basic value type.

  6. Immutability, packaging classes are immutable. Immutability means that once an instance object is created, there is no way to modify it.


Alibaba Java development manual mentions that for the equivalence judgment between floating-point numbers, the basic data type cannot be compared with = = and the packaging data type cannot be judged with equals. The specific principle is related to the encoding method of floating-point numbers, which will not be mentioned here. Let's take the following example directly:

float a = 1.0f - 0.9f;
float b = 0.9f - 0.8f;
System.out.println(a);// 0.100000024
System.out.println(b);// 0.099999964
System.out.println(a == b);// false

When using BigDecimal, in order to prevent precision loss, we recommend using its BigDecimal(String) construction method to create objects. Alibaba Java development manual also mentions this part, as shown in the figure below.

2. Parse String

2.1 internal principle

The String class uses a character array to represent the String, and the instance variable is defined as:

private final char value[];

Most methods in String also operate on this character array.

2.2 code conversion

2.3 constant string

String a = "11";	// a points to the position of the constant string "11"
String b = "11";	
a == b;	// true, both point to the same location

String c = new String("11");
String d = new String("11");	
c == d;	// false, both point to different objects

String creation

  • String s = "a" + "b" + "c" how many objects are created?

One is created because at compile time, the JVM will merge "a", "d" and "c" into one object, put it into the constant pool, and then let s point to it

  • String s = new String("a" + "b") how many objects are created?

String s = new String("a" + "b") how many objects are created?

Two are created, one is a String constant and the other is a String object

3. Analyze StringBuilder

3.1 basic usage

StringBuilder sb = new StringBuilder();


String a = ab.toString();

3.2 internal principle

It also uses a character array. Unlike String, this array is not final. It saves an instance variable count to represent the number of characters used in the array.

  • During construction, call the parent class construction to generate a char array of a specific length,
  • The append method will directly copy characters to the character array. When the array length is not enough, it will be expanded.
  • The toString method generates a String object that holds the contents of the character array
  • After ensuring sufficient length, the insert method will move the character of the original array back, and then insert.

3.3 + and of string+=

The + method of String generally generates a StringBuilder object, and the addition and subtraction of String are realized through the append method

3.4 String, StringBuilder and StringBuffer


The String class uses the final character array to save the String, so the String is immutable

private final char value[];

The other two inherit from the AbstractStringBuilder class, in which the character array is not decorated with final.

Thread safety:

String objects are immutable and can be understood as constants. They are thread safe.

StringBuffer adds a synchronization lock to the method or adds a synchronization lock to the called method, so it is thread safe.

StringBuilder does not apply synchronization locks to methods, so it is non thread safe.


Each operation of String type will generate a new String object and change the pointer to the new String object.

StringBuffer operates on the StringBuffer object itself every time, instead of generating a new object and changing the reference.

The performance of StringBuilder is 10% ~ 15% higher than that of StringBuffer, but the thread is not safe.

4. Random

4.1 Math.random

double a = Math.random();	// Nonparametric structure
double b = Math.random(seed);	// Parametric structure
Math.Random(seed1) == Math.random(seed1);

If the seeds are the same, the resulting random number sequence is the same.

4.2 basic principle of random

public Random(long seed) {
    if (getClass() == Random.class)
        this.seed = new AtomicLong(initialScramble(seed));
    else {
        // subclass might have overriden setSeed
        this.seed = new AtomicLong();

synchronized public void setSeed(long seed) {
	haveNextNextGaussian = false;

private static long initialScramble(long seed) {
	return (seed ^ multiplier) & mask;

4.3 shuffle

A common random scenario is shuffling, which is to rearrange an array or sequence randomly

	int[] arr = {1,2,3,4,5,6,7,8,9};
    private static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];    
        arr[j] = tmp;
    private static void shuffle(int [] arr){
        Random rnd = new Random();
        for(int i = arr.length; i> 1; i--){
            swap(arr, i-1, rnd.nextInt(i));

4.4 red envelope grabbing algorithm

public class RandomRedPacket {
    private int leftMoney;
    private int leftNum;
    private Random rnd;
    public RandomRedPacket(int totle, int num){
        this.leftMoney = totle;
        this.leftNum = num;
        this.rnd = new Random();
    public synchronized int nextMoney(){
        if(this.leftNum <= 0){
            throw new IllegalStateException("It's all gone");
        if(this.leftNum == 1){
            return this.leftMoney;
        double max = this.leftMoney / this.leftNum * 2d;
        int money = (int) (rnd.nextDouble()*max);
        money = Math.max(1,money);
        this.leftMoney -= money;
        return money;

// test
public void test_1(){

    RandomRedPacket redPacket = new RandomRedPacket(100, 5);
    for(int i =0; i < 5; ++i){

5. Exercises

  • In any case, Integer and new Integer will not be equal. There will be no unpacking process,
  • Both are non new integers. If the number is between - 128 and 127, it is true, otherwise it is false
    When java compiles Integer i2 = 128, it is translated into - > integer I2 = integer. Valueof (128); and the valueOf() function caches the number between - 128 and 127
  • Both are new and false
  • The ratio of int to Integer (whether new or not) is true, because Integer will be unpacked automatically to int and then compared

Tags: Java

Posted on Tue, 28 Sep 2021 03:44:23 -0400 by angryjohnny