• Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Its the hairy ++ again.

 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still cant figure out why the following code works.
byte b = 10 ;
b = b++ ;
Since ++ does an implicit conversion to int,
how come the assignment works here? Try it
out with b = 127, and it still works!
I am very very

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

Pt 2: Though you assign 127 to a byte b1 and do a b2=b1++, it'll accept it because this a post operation addition and hence b2 will be assigned the original value of b1 (and then b1 gets incrememted,) which is 127 and it's valid.
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
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed that too. On the same lines, the following code
byte b = 10 ;
b = b++ ;
System.out.println( b ) ;
prints 10. I just can't figure out why b is not
getting incremented. Again, very very
Am I missing something very obvious here ??
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajith Kallambella:
I still cant figure out why the following code works.
byte b = 10 ;
b = b++ ;
Since ++ does an implicit conversion to int,
how come the assignment works here? Try it
out with b = 127, and it still works!
I am very very


<pre>
Hi! Here goes,
First:
<code>
byte b = 10;
b = b++;
</code>

Your code is setting the var "b" to its "old" value
which is 10. You are incrementing b and then setting
it back to its old value.
Second:
I gather from your question that you want to know
about the narrowing and widening conversion rules.
I am assuming that you know about the binary numeric
promotion
and narrowing primitive conversion
<bold>
This is from JLS.
15.14.1 Prefix Increment Operator ++
[...]
At run time, if evaluation of the operand expression
completes abruptly, then the prefix increment expression
completes abruptly for the same reason and no incrementation
occurs. Otherwise, the value 1 is added to the value of the
variable and the sum is stored back into the variable.
Before the addition, binary numeric promotion (�5.6.2) is
performed on the value 1 and the value of the variable. If
necessary, the sum is narrowed by a narrowing primitive
conversion (�5.1.3) to the type of the variable before it is
stored. The value of the prefix increment expression is the
value of the variable after the new value is stored.

So
<code>
(1) byte b = 127;
(2) b++; // [ same as b = b + 1]
</code>
Apply numeric promotion. So now "b" and "1" are ints.
so now (before addition) the int b is

00000000 00000000 00000000 01111111

after addition :
00000000 00000000 00000000 10000000
Now apply narrowing conversion (which means
pick up the lowest order 8 bits)
So you have a byte b

10000000
The signed bit is set and the value is -128.

HTH
BoN.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha!. I couldn't be more dumb. Huh?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bongadi:
Could you go over the "First" again please....
I don't get it.
b = 10;
b = b++;
While it is true that b is printed as 10, I still
din't understand why?
Is the above stmt (b = b++) not same as:
b = b; b++;
I was thinking this should be the case. I know both
give different answers. (In the latter case I get 11.)
(Edited)
BTW, the same happens even when you use "int"
instead of "byte".
int i = 10;
i = i++ // Prints i = 10


Regds.
- satya

[This message has been edited by satya5 (edited May 01, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satya,
For simple and easy method take a look at here.
Further details in next post.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maha:
Thanks for the link. Not sure how I missed it
earlier. I think its a MUST SEE for all who plan
for SCJP exam.
Regds.
- satya
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
So, now if I print the value of i, it should print 11 isn't it??
Please clarify.
Prabhu.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prabhu:
While, the current value is 11, it is assigned a value
of 10, right! So when you print i, it prints the current
value at the time of printing which is 10.
The so-called current value of 11 is JUST BEFORE assignment.
Now, if you have time AND want to confuse yourself,
do this ( I did and am )
int i = 10
i = i++;
printf (i)
Write C/C++/Java code with these statements. (I know this
is NOT THE DISCUSSION BOARD FOR SUCH PROBLEMS, but from a
"PROGRAMMER" point of view this is valid, IMO.)
Compile (and run) them using say
"cc" UNIX C compiler, ( result i = 10 )
"CC" UNIX C++ compiler,( result i = 10 )
"g++" GNU C++ compiler,( result i = 11 )
"javac" java compiler( result i = 10 )
If anyone thinks I SHOULD NOT DISCUSS such problems on
this discussion board, I am totally with it.
But, remember we all aspire to be programmers. Right!
I am not sure if the PC based compilers.
Hope someone can answer this.
Thanks.
- satya
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
2. int j=i++;
//3. j = 10(11);
4. System.out.println("i="+i+" j="+j); //prints i=11 j = 10;


regds
maha anna

[This message has been edited by maha anna (edited May 02, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic