The + operators and characters you don't know

Source: Internet
Author: User

    • Today, a person asked me on QQ, said Java use System.out.print (' a '); print correctly, but System.out.print (' a ' + ' B '); Print 195, what's the situation?
    • In this regard I give the following explanations to share to everyone, so as not to encounter such problems in the future unprepared.
    • The problem is the single quotation mark, the single quotation mark is a character, that is, the char type, double quotation marks is a string, that is, string type, string type is not the base data type, for the object. Is the type of the two-character connection obtained with the plus sign exactly a character type or a string? The system cannot be identified. This means that the JDK is not allowed to use character stitching.
    • To explain the function of the + number, the + operator has two different meanings. The first is a connector , which is only possible if there is a string type in the operand. There is also an addition operator, which is not much to say.
    • So the following code executes an addition, not a connection. That is, A and B decimal ASCII code added: So the result is 195
    • System.out.print (' A ' + ' B ');

      If you want to get the AB string, you can use the following methods:

    • System.out.print ("" + ' a ' + ' B ');

      Of course, you can also use methods such as join and printf.

    • The principle has already shared everybody, the solution will not need to explain carefully ....
    • Good luck.

The + operators and characters you don't know

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.