Home  >  Article  >  Java  >  What is the difference between Boolean and boolean in Java

What is the difference between Boolean and boolean in Java

PHPz
PHPzforward
2023-05-05 19:40:043091browse

Preface

Someone asked, aren’t there only two types of values ​​of Boolean type: true and false? Why does the property he defined have a null value?

We should first make it clear that boolean is the basic data type of Java, and Boolean is a class of Java. The boolean type assigns false to the property during the "assign zero" phase. Boolean is a class that will assign null to the object during the "assign zero value" phase.

If it is a static attribute, it will be assigned when the class is loaded. If it is a normal class attribute, the value will be assigned when the object is instantiated. These two points can help you understand the "class loading mechanism" and "object creation process".

Class loading mechanism:

  • Loading: Get the binary byte stream of the class according to the full name of the class, load the class into memory and generate a representation of this in the heap The Class object of the class serves as the access entrance to the method area data

  • Verification: Verify whether the byte stream in the class file complies with the JVM specification

  • Preparation: Allocate memory for the static properties of the class in the method area, and initialize the default value (the default value of boolean is false, and the default value of Boolean is null)

  • Analysis: Transfer the constant pool The symbol references in are converted into direct references, which can be understood as object references converted into pointers

  • Initialization: actually start executing the code in the class, static attribute assignment and static block

Object instantiation process:

  • Check whether the class has been loaded (parental delegation)

  • Allocate memory to the object Space (pointer collision)

  • Zero value initialization (false/null)

  • Set the object header (object generation age and other information)

  • Execute the method (attribute initialization, statement block and construction method)

So, Boolean has only been loaded, not yet Instantiation, no memory is allocated before being instantiated, so it is null

Next we can look at the properties and construction methods of Boolean to understand how it wraps boolean

// final boolean类型的属性,通过构造方法注入值
private final boolean value;

// 构造方法 Boolean a = true 实际上就是调用这个方法
public Boolean(boolean value) {
    this.value = value;
}

// 构造方法
public Boolean(String s) {
    this(parseBoolean(s));
}

For other The attributes and methods are relatively simple to view by yourself.

There is a risk point in the use of Boolean. The Alibaba development manual is also very well written

What is the difference between Boolean and boolean in Java

To put it simply, the attributes defined by boolean must have a value. If the Boolean object value is null, NPE will occur during the unpacking process.

Imagine a scenario: Your girlfriend asks you: Do you love me? But you didn't hear clearly. If you are a Boolean, you will answer, I didn't hear it clearly (null), if you are a boolean, you will answer, if you don't love it anymore (false)

, you will be beaten.

Supplement: Boolean and boolean performance exploration

For Boolean and true

It is not clear from the source code perspective which one has better performance; Big Boolean Also initialized two static objects

The source code screenshot is as follows:

What is the difference between Boolean and boolean in Java

Write a test class: test method (get the big Boolean type true time to get the small The time of Boolean type true, pass 100, 1000, 10000, 100000 times to see which time is less and more times)

The test code is as follows:

public class Test {	
	/**
	 * 方法一
	 * 
	 * @return
	 */
	public static Boolean A() {
		return Boolean.TRUE;
	}
	/**
	 * 方法二
	 * 
	 * @return
	 */
	public static boolean D() {
		return true;
	}
	public static String get() {
		long i = 0L;
		long j = 0L;
		for (int n = 0; n < 100000; n++) {
			long startTime = System.nanoTime();
			D();
			long endTime = System.nanoTime();
			long booleanTime = endTime - startTime;
			
			long start = System.nanoTime();
			A();
			long end = System.nanoTime();
			long booleanca = end - start;
			
			
			if (booleanca > booleanTime) {
				i = i + 1;
			} else {
				j = j + 1;
			}
		}
		return i+" "+j;
	}
	public static void main(String[] args) {
        System.out.println("---100000次的比较结果---->"+get());
	}
}

The execution result is as follows:

What is the difference between Boolean and boolean in Java

The above is the detailed content of What is the difference between Boolean and boolean in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete