Pregunta

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?

¿Fue útil?

Solución

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)

Otros consejos

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

char1 = char1 + 1;

or

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

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.

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top