Why doesn't a character increment in System.out.println()?

强颜欢笑 提交于 2019-11-28 13:04:56

问题


char char1 = 'a';   
System.out.println(char1);      //prints char 1
System.out.println(char1+1);    //prints char 1
System.out.println(char1++);     //prints char 1
System.out.println(char1+=1);    //prints incremented char1
char1 += 1;                     
System.out.println(char1);      //prints incremented char1

In the above, why doesn't (char1+1) or (char++) print the incremented character but theother two do?


回答1:


First, I'm assuming that because you say the increment in System.out.println works, that you have really specified:

char char1 = 'a';

EDIT

In response to the change of the question (char1+1; => char1 += 1;) I see the issue. The output is

a
98
b

The 98 shows up because the char a was promoted to an int (binary numeric promotion) to add 1. So a becomes 97 (the ASCII value for 'a') and 98 results.

However, char1 += 1; or char1++ doesn't perform binary numeric promotion, so it works as expected.

Quoting the JLS, Section 5.6.2, "Binary Numeric Promotion":

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

(emphasis mine)




回答2:


You didn't assign the result of addition char1+1 to char1. So

char1 = char1 + 1;

or

char1 += 1; char1++; are correct.




回答3:


Okay, first of all, fixing the format of your code:

char char1;
char1 = 'a';
System.out.println(char1);          // print 1
System.out.println(char1 + 1);      // print 2
char1 += 1;
System.out.println(char1);          // print 3

which yields the output:

a
98
b

Now, let's look at each call to println() in detail:

1: This is simply taking the character handle named char1 and printing it. It's been assigned the letter a (note the single quotes around the a in the assignment, indicating character). Not surprisingly, this prints the character a.

2: For this line, you're performing an integer addition. A char in java is held as a unicode character. The unicode value for the letter a maps to the number 97. (Note that this also corresponds to that ASCII value for a). When performing arithmetic operations in Java between mismatched types, the smaller/less precise value type's value will be 'upgraded' to the larger type (this is very imprecisely stated). Because of this, the char is 'upgraded' to an int before the addition is performed, and the result is also an int. With this in mind, it's not surprising that the 97 from a +1 results in a 98 being printed.

3: In this instance we are once again printing the value of a char, so a character is printed. This time the 98 we saw generated before is implicitly cast back into a character. Again, unsurprisingly the next highest number mapping from a is b, so we see a b printed.




回答4:


try this.

System.out.println(char1);
System.out.println(++char1);
char1 += 1;
System.out.println(char1);

instead

char1 = a;   
System.out.println(char1);
system.out.println(char1+1);
char1 += 1;
System.out.println(char1);


来源:https://stackoverflow.com/questions/18648283/why-doesnt-a-character-increment-in-system-out-println

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!