- 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