Question: What is the result of the following code?
| The code is as follows: |
Copy code |
|
Package quiz;
Public class Quiz22 {
Static String str;
Public static boolean test1 (){
Return new Boolean ("1 ");
}
Public static boolean test2 (){
Return new Boolean (str );
}
Public static void main (String [] args ){
If (test1 ())
System. out. print ("1 ");
If (! Test2 ())
System. out. print ("2 ");
If (test1 ()! = Test2 ())
System. out. print ("3 ");
}
} |
Output:
2
Let's take a look at the source code of the Boolean class: (some are omitted and only key code is retained)
| The code is as follows: |
Copy code |
|
Public final class Boolean implements java. io. Serializable, Comparable <Boolean>
{
Private final boolean value;
Public Boolean (boolean value ){
This. value = value;
}
Public Boolean (String s ){
This (toBoolean (s ));
}
Public static int compare (boolean x, boolean y ){
Return (x = y )? 0: (x? 1:-1 );
}
Private static boolean toBoolean (String name ){
Return (name! = Null) & name. Inclusignorecase ("true "));
}
Public boolean equals (Object obj ){
If (obj instanceof Boolean ){
Return value = (Boolean) obj). booleanValue ();
}
Return false;
}
Public int compareTo (Boolean B ){
Return compare (this. value, B. value );
}
Public static boolean parseBoolean (String s ){
Return toBoolean (s );
}
Public static Boolean valueOf (boolean B ){
Return (B? TRUE: FALSE );
}
/**
* Returns a {@ code Boolean} with a value represented by
* Specified string. The {@ code Boolean} returned represents
* True value if the string argument is not {@ code null}
* And is equal, ignoring case, to the string {@ code "true "}.
*
* @ Param s a string.
* @ Return the {@ code Boolean} value represented by the string.
*/
Public static Boolean valueOf (String s ){
Return toBoolean (s )? TRUE: FALSE;
}
Public String toString (){
Return value? "True": "false ";
}
/**
* Returns a hash code for this {@ code Boolean} object.
*
* @ Return the integer {@ code 1231} if this object represents
* {@ Code true}; returns the integer {@ code 1237} if this
* Object represents {@ code false }.
*/
Public int hashCode (){
Return value? 1231: 1237;
}
} |
We can see from the above:
1. Boolean supports using String or boolean to create objects.
If a boolean object is built, the Boolean object is automatically split and the original value is used.
If the string is used to construct the object, if the String is null or string. Inclusignorecase ("true") returns false, the value after Boolean unpacking is false.
2. The Boolean class implements the Comparable interface, that is, the ComparaTo method.
Analysis questions:
Test1 () is equivalent to (new Boolean ("1") + automatic unpacking, false
! Test2 () is equivalent! (New Boolean (str) + automatic unpacking). The result is true because str is null.
Test1 ()! = Test2 (), the result is false
If the returned values of test1 and test2 are not boolean objects but Boolean objects, the results will be different. Because it is a new object, the address must be different, and the Boolean code does not want the Integer to have a cache.
| The code is as follows: |
Copy code |
|
Public class DecoratorPatternEx {
Public static void main (String [] args ){
Boolean a = new Boolean ("1 ");
Boolean B = new Boolean ("1 ");
Boolean a1 = new Boolean (true );
Boolean b1 = new Boolean (true );
If (a = B ){
System. out. println (true );
} Else {
System. out. println (false );
}
If (a1 = b1 ){
System. out. println (true );
} Else {
System. out. println (false );
}
}
} |
Result:
False
False