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

4

-3
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?

Rainbolt answered 6/9, 2013 at 1:4 Comment(10)
This isn't even valid Java. -1Thilde
Once the code is correctly formatted (made compilable), it works fine for me..Weasel
That was a typo, it's right in my code.Rainbolt
@DanielBall Then why did you retype the code here? Instead of copying and pasting?Thilde
The infix + operator, as in x + y, results in an expression which evaluates to a new value but does not change the value assigned to either x or y. This is different than the ++ or -- (prefix or postfix) operators.Felishafelita
@Doorknob because the actual code uses three variables and is in a couple separate lines, so typing something syntactically similar was more effective in communicating my issue in a question rather than adding clutter.Rainbolt
@DanielBall Well posting nonworking code is not particularly helpful.Thilde
@Doorknob, that's why I fixed it. But the problem persists (and still makes no sense to me)Rainbolt
It's not fixed. You should actually try the code before posting it.Thilde
There, happy? I do have to wonder how much good you could do if you put all your energy into helping instead of abusing people.Rainbolt
M
8

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)

Mavilia answered 6/9, 2013 at 1:9 Comment(3)
Well that explains the numbers from char +1, but not my other issue ... char++ just doesn't seem to do anything, if I input char++ when char is a, it just prints a, but if I do char++; before I do the println, it works.Rainbolt
That is what the post-increment operator does. It increments it, but the value of the expression is the old value, 'a'.Mavilia
OH! I see, I need to use a pre increment. Right. Thanks :) I completely forgot about that little nuance.Rainbolt
S
1

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

char1 = char1 + 1;

or

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

Sanctimony answered 6/9, 2013 at 1:7 Comment(3)
Perhaps you should provide an explanation to as why they're correct :) (And why his code is incorrect)Calandracalandria
Unless of course you are unable to explain why yours is correct and his is incorrect...Calandracalandria
Sorry, I replyed to a questioner's stupid comment but it has been deleted.Sanctimony
S
1

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.

Skyward answered 6/9, 2013 at 1:27 Comment(0)
O
0

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);
Outcast answered 6/9, 2013 at 1:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.