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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











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

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

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

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.
