Author | Topic: Its the hairy ++ again. |
Ajith Kallambella ranch hand |
posted May 01, 2000 08:06 AM
I still cant figure out why the following code works. byte b = 10 ; b = b++ ; Since ++ does an implicit conversion to int, I am very very
|
Prabhu greenhorn |
posted May 01, 2000 08:53 AM
Ajith, Pt 1: ++ operation doesn't convert a byte to int (Refer R&H Assignemnts chapter. If your R&H is a old version, check the Errata, because old version doesn't specify this point).
Now, I've a question: When I assign a value 127 to byte b1, do a byte b2=b1++, display b2 after this operation, b2 is having a value -128 (negative 128). I don't know why???? [This message has been edited by Prabhu (edited May 01, 2000).]
|
Ajith Kallambella ranch hand |
posted May 01, 2000 09:09 AM
I noticed that too. On the same lines, the following code byte b = 10 ; prints 10. I just can't figure out why b is not Am I missing something very obvious here ??
|
bongadi greenhorn |
posted May 01, 2000 09:14 AM
quote:
|
Ajith Kallambella ranch hand |
posted May 01, 2000 09:33 AM
Gotcha!. I couldn't be more dumb. Huh?
|
satya5 ranch hand |
posted May 01, 2000 10:10 AM
bongadi: Could you go over the "First" again please.... b = 10; While it is true that b is printed as 10, I still b = b; b++; I was thinking this should be the case. I know both (Edited) int i = 10; i = i++ // Prints i = 10
- satya [This message has been edited by satya5 (edited May 01, 2000).]
|
maha anna bartender |
posted May 01, 2000 11:31 PM
Satya, For simple and easy method take a look at here. Further details in next post.
|
satya5 ranch hand |
posted May 02, 2000 07:15 AM
Maha: Thanks for the link. Not sure how I missed it Regds. - satya
|
Prabhu greenhorn |
posted May 02, 2000 07:34 AM
Maha, I'm still . Say int i = 10; When I do i=i++, then according to the method you specified, i should calculate like i = 10(11). So here the value which is non-bracketed (i.e 10) will be assigned to i. But at the same time the current value of i will be 11, isn't it?? Please clarify. Prabhu.
|
satya5 ranch hand |
posted May 02, 2000 09:07 AM
Prabhu: While, the current value is 11, it is assigned a value Now, if you have time AND want to confuse yourself, int i = 10 Write C/C++/Java code with these statements. (I know this Compile (and run) them using say "cc" UNIX C compiler, ( result i = 10 ) If anyone thinks I SHOULD NOT DISCUSS such problems on I am not sure if the PC based compilers. Hope someone can answer this. Thanks. - satya
|
Mark Owens greenhorn |
posted May 02, 2000 02:05 PM
The moral of the story is: If you want to increment a variable, do not use i=i++ This is essentially defeating the "++" shortcut. Instead use: i++ Just thought it needed to be said... Mark
|
maha anna bartender |
posted May 02, 2000 04:35 PM
Prabhu, 1. int i=10; 2. i=i++; //3. i = 10(11); 4. System.out.println("i="+i); //prints 10; After execution of these statements i value is still 10. It is because, the result of the right hand operation is 10, and this 10 value is again stored in the SAME OLD VAR i. This is the reason why i=10 at line no 4. If you would have assigned the result 10 to another var, say j, if you print and see the value of i at line no. 4 it will be in fact 11. 1. int i=10;
[This message has been edited by maha anna (edited May 02, 2000).]
|
| | |