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