Forums Register Login

Problem with the output statement in java

+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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);
1
+Pie Number of slices to send: Send
The A15 is a road which runs from Norman Cross near Peterborough to the Humber Bridge near Hessle.
1
+Pie Number of slices to send: Send
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".
+Pie Number of slices to send: Send
Thanks a lot Campbell Ritchie! I think it's time for me to brush up on my basics of JAVA.
+Pie Number of slices to send: Send
You're welcome
1
+Pie Number of slices to send: Send
What would happen if you declared m as an int instead? See if you can work it out, then try it in real code!
+Pie Number of slices to send: Send
Yes, it would show m as 65. int are char are complementary to each other. Thanks for making me understand this!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Ranajoy Saha wrote: . . . Thanks for making me understand this!

You're welcome
+Pie Number of slices to send: Send
 

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?
1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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!
1
+Pie Number of slices to send: Send
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.
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 665 times.
Similar Threads
doubts on questions #1 and #4 self-test Chapter 8
java
java primitive problem
Expressions!
Anomaly while using the Scanner class for taking inputs
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:03:05.