• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with the output statement in java  RSS feed

 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Everyone

It has been about 2 years that I'm programming in JAVA and never has this occurred to me about the reason why this happens. So, in this code snippet


The statement "System.out.println(m+p);" returns "80" which is evident that the type of the entire expression is promoted to the value of the largest data type int. But in the statement " System.out.println("Answer :"+m+p);, why does the output change to "Answer :A15" but not "Answer :80"?

Regards,
Ranajoy
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of people think a char is a character, but it isn't. It is a number. If you try arithmetic with it, the fact that it is a number becomes obvious.

If you want to print A15 and go from Norman Cross to Hessle, you can try converting the char to a String
System.out.println("" + 'A' + 15);
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The A15 is a road which runs from Norman Cross near Peterborough to the Humber Bridge near Hessle.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert the 'A' to part of a String by catenating it to the existing String "Answer: ", as you have already seen. If you look in the Java Language Specification, you find that + changes from addition to String catenation if either of its operands is a String. And remember you always go left‑to‑right. Remember that you might write 'A' but the system does not store an A. It stores the number 0x0041 which is 65 in decimal.
You find the String "Answer: " and catenate the char '\u0041' to it. Now you have the String "Answer: A"
Now you have the String "Answer: A" and the int 15 as operands for the + operator. Arithmetical addition is not possible, so it catenates the number 15 (in decimal) to the end of the String.
Now you have the String "Answer: A15".
 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Campbell Ritchie! I think it's time for me to brush up on my basics of JAVA.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would happen if you declared m as an int instead? See if you can work it out, then try it in real code!
 
Ranajoy Saha
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it would show m as 65. int are char are complementary to each other. Thanks for making me understand this!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The A15 is a road which runs from Norman Cross near Peterborough to the Humber Bridge near Hessle.


That's one of them... full list here: List of A15 roads.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranajoy Saha wrote: . . . Thanks for making me understand this!
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote: . . . That's one of them... full list here: List of A15 roads.
Is that brilliant or very sad that somebody took the time to list all the A15s?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Lots of people think a char is a character, but it isn't. It is a number. If you try arithmetic with it, the fact that it is a number becomes obvious.

Doesn't it have more to do with when/how Java does widening/narrowing conversions rather than with char being a number? In certain contexts, it gets widened but other conversions can also happen after that.

On line 1, it doesn't get widened to an int.
On line 2, the widening conversion to int happens, thus the output is 122. On line 3, the widening conversion still happens but the result is narrowed down to char because that's the type of variable ch. Hence, the output of line 4 is z. On line 5, we have the same expression but because we're assigning it to an int variable, the widening conversion happens but there is no narrowing. Thus, Line 6 outputs 122.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: . . .
. . .
Line 1: Agree: the JLS link about the String catenation operator says that whichever operand is not a String (remembering you might have both operands as Strings) undergoes String conversion. String conversion of a char uses Character#toString which changes it to its Unicode equivalent.
Line 2: Agree: Numeric promotion to an int and then addition.
Line 3: Both operands of the + operator are compile‑time constants, otherwise that line would not compile. I think it is numeric promotion then arithmetic then narrowing.
Line 5: Numeric promition then arithmetic.
I think numeric promotion is a kind of widening conversion, so I think you are correct about them all.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: String conversion of a char uses Character#toString which changes it to its Unicode equivalent.



Campbell.. I read your reply that said that the Character Class' toString() method causes the character to be converted to a value and this is given in the docs as well.
But I wrote the following program to check out the theory and I'm not getting the output I expect which is supposed to be the unicode value of the character.



Output: a

Thanks in advance for the help!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I obviously wasn't clear. If you use arithmetic you get a as a number, 0x0061 (97 decimal).
If you use toString, the Unicode value of 0x0061 is a, so you get the letter.

Don't use Character#digit (or do use it and see what happens ‍). Similarly with this method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!