What is the difference between Boolean and boolean in Java
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
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:
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:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
