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:
The result: i=51 c=2 I know "+" function in System.out.println(). I just can not Please help! Especially for those contributing to Umesh's
|
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 In second case c += i becomes 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 ,
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 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. [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
|
| | |