Comparison of three methods for Java to generate a Boolean object

Source: Internet
Author: User

The first common method for Java to generate a Boolean object is through the new operator Boolean boolean1 = new Boolean (1 = 1); the second is through the static method valueOfBoolean boolean1 = Boolean. valueOf (1 = 1); the third is the automatically packed Boolean boolean1 = 1 = 1 after JDK1.5; What are the differences between the three methods? First look at a piece of code Boolean [] boolean1 = new Boolean [100];
Boolean [] boolean2 = new Boolean [1, 100];
Boolean [] boolean3 = new Boolean [0, 100];
For (int I = 0; I <100; I ++ ){
Boolean1 [I] = Boolean. valueOf (1 = 1 );
}
For (int I = 0; I <100; I ++ ){
Boolean2 [I] = new Boolean (1 = 1 );
}
For (int I = 0; I <100; I ++ ){
Boolean3 [I] = 1 = 1;
}
System. out. println ("valueOf:" + String. valueOf (boolean1 [1] = boolean1 [2]);
System. out. println ("new Boolean:" + String. valueOf (boolean2 [1] = boolean2 [2]);
System. out. println ("auto wrap:" + String. valueOf (boolean3 [1] = boolean3 [2]); the output result is: valueOf: true
New Boolean: false
Auto wrap: true. Why? The reason is that the Boolean object created with new is constantly creating a new instance object, while the valueOf is a static member variable in the Boolean class and does not produce a large number of identical instance variables. Automatic packaging is similar to valueOf. In fact, we recommend that you use valueOf instead of new to create a Boolean object in jdk documentation.

This article from the "technology lovers" blog, please be sure to keep this source http://chenqiangjsj.blog.51cto.com/2331729/550420

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.