Java String concatenation operator "+"

Source: Internet
Author: User

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.
 

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.