1. 3 + 3 = 33?
2. char [] test = {'1', '2', '3'}; System. out. print ("test" + test); output test = 123 ??
3. "hello world" is not equal to "hello" + "world "???
4. "hello world" is not equal to "hello world" ???
The test code and result are as follows:
Q:
1. As we all know, in addition to the addition function of the plus sign, the most commonly used is to connect strings. So what is the function of the plus sign here? It is obviously used as a connection string. So what about System. out. println ('3' + '3? The result will be output 102 (the uicode value of 3 is 51). Here the + sign is added. In java, we need to distinguish character strings from character types. Local addition is not equal to the whole. 'H' + 'I' is not equal to "hi ".
2. this is related to calling the toString method on the char array. The array inherits the tostring method from the Object without overloading. In the specification, it is described as "return a string, it contains the Class Name of the object, "@" symbol and an unsigned hexadecimal integer representing the object hash code ", so we have the ugly output. To correctly output the character sequence, we can first call the String. valueOf (test) method to return a String object and then output it (convert the character array to a String first ). Of course, we can also. out. println ("test =" + test); separated twice for output to achieve the goal, namely: System. out. println ("test ="); System. out. println (test );
3. this is related to the = symbol. true is returned only when the references on both sides point to the same object, in general, we recommend the equals method to determine whether the reference is equal.
4. this is related to the priority of "+". Whether it is used as addition or connection string, the priority of "+" is higher than that of "=", so System. out. println ("h1 = h2? : "+ H1 = h2); equivalent to: System. out. println (" h1 = h2? : "+ H1) = h2); always outputs false. If we use System. out. println ("h1 = h2? : "+ H1.equals (h2); the correct result is displayed. Obviously, using the equals method can remove a lot of unnecessary troubles for us.