programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Junilu Lacar
• Liutauras Vilda
Sheriffs:
• Paul Clapham
• Jeanne Boyarsky
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Piet Souris
• Carey Brown
Bartenders:
• Jesse Duncan
• Frits Walraven
• Mikalai Zaikin

# Another conversion question

Ranch Hand
Posts: 39
• Number of slices to send:
Optional 'thank-you' note:
The following question is related to Umesh's Conversion message
on April 13. But here I have a different question. See the
following Code fragment:

<CODE>
1: int i =1;
2: char c = '2';
3: i += c;
4: System.out.println("i="+i);
5: System.out.println("c="+c);
</CODE>
The result: i=51 c=2
However, if I remove line 3:i+=c, the result will change to
i=1 c=2 .
Moreover, if I replace line 3 with c+=i, the result will be
i=1 c=3.
I know "+" function in System.out.println(). I just can not
understand what 's happening to the i value and c value in the
above three cases.
Conversion message. Thanks!

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Hi Wai ,

If I am correct the answer goes this way..
the op1 += op2 expression is calculated as follows
op1 = (type) (op1 + op2 ) where type is the type of op1.
In First case i += c becomes

i = (int) ( i + c ) . so c is promoted to integer(Arithmetic promotion) and gets the ascii value of characer '2'.
hence the result for i is 51.
In second case c += i becomes

c = (char) (c + i) which is evaluated in the same way as above except that the resulting value is casted to char.
hence the result of c .
HOPE IT IS CLEAR.
Suresh R .

Wai Iu
Ranch Hand
Posts: 39
• Number of slices to send:
Optional 'thank-you' note:
I guess that my question is Why c gets ascii code "50" in i+=c
[i=i+c]and gets "2" in c+=i[c=(char)(c+i)]. Should c get same
value "50" in both cases? If a char value converts to a int
value, does it convert to its ascii value or convert to its
'face' value, like this case?

Ranch Hand
Posts: 1467
• Number of slices to send:
Optional 'thank-you' note:
Wai Iu,
In both cases ,<pre>
i+=c;
c+=i;
</pre>
the ASCII value of the Right Hand operand is added to the left hand operand. Which means
i= (int)(i+c); is SAME as i=(int) 1+50; which is SAME as i=51;
c+=i is SAME AS c = (char)(c+i); SAME AS c=(char)(50+1); same as c=(char)(51) same as c= 51;
The key here is how you represent the numeral value 51 ? When you print it as integer by assigning to a var of type 'int' which here is var 'i' and pass it to the perfect matching method among all other overloaded methods of println method, the 'integer' value which is '51' is printed.
At the same time when you represent the numeral as 'character' by assigning it to a char var and call the appropriate println(..) the char representation of that int val which here is '2' (the face value as you said ) is printed. Is this clear to you now? OR if still not convincing please reply back.
regds
maha anna

[This message has been edited by maha anna (edited April 15, 2000).]

Wai Iu
Ranch Hand
Posts: 39
• Number of slices to send:
Optional 'thank-you' note:
I think I got it. In the above example, for i+=c, System.out.println("i="+i) will print out i=51 because i is an
int with the value of 51(ascii code); on the other hand, for
c+=i, System.out.println("c="+c) will print out c=3 because
c is a character with the value of 3, which is the text
representation of ascii code 51.
Thanks! Anna.

maha anna
Ranch Hand
Posts: 1467
• Number of slices to send:
Optional 'thank-you' note:
We both did it. I am really happy you got it.
regds
maha anna

 pie. tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton