Author Topic:   Another conversion question
Wai Iu
greenhorn
posted April 15, 2000 05:51 AM             
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:


1: int i =1;
2: char c = '2';
3: i += c;
4: System.out.println("i="+i);
5: System.out.println("c="+c);

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.

Please help! Especially for those contributing to Umesh's
Conversion message. Thanks!

suresh
unregistered
posted April 15, 2000 06:50 AM           
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
greenhorn
posted April 15, 2000 07:37 AM             
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?

maha anna
bartender
posted April 15, 2000 02:52 PM             
Wai Iu,
In both cases ,

i+=c;
c+=i;

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;

Simillarly for your 2nd case
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
greenhorn
posted April 15, 2000 04:52 PM             
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
bartender
posted April 15, 2000 06:52 PM             
We both did it. I am really happy you got it.
regds
maha anna

|