aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operators again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operators again" Watch "Operators again" New topic
Author

Operators again

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
hi all,

Please check out this program,

int i3 = 0;
i3 = i3++;
i3 = i3++;
System.out.println("The value of i3 is " + i3);

On execution it gives the output as "The value of i3 is 0".
I thought it would be 2,
Please any one explain me whats happening here
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Nice observation! Just observe what exactly is happening here:

First is, you are assigning the variable 'i3' with the value 0.
Second, you are doing:



What happens here? This is a post-incrementation operation. That means, you are first assigning i3 with the existing value of i3 itself and then immediately incrementing it by 1. But this assigned i3 is now zero itself and not one. If you split up this into two lines of code, it would be like this:



So the third statement also does the very same thing! Finally, the value of i3 is juzt zero. The proper way of doing this would be as follows:



Got the point? Try this out!


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
Kali Praveen
Greenhorn

Joined: May 27, 2004
Posts: 10
Please look at Corey's tipline on this topic.

Increment Decrement Operators

Hope this helps.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Originally posted by Vijayendra V Rao:

What happens here? This is a post-incrementation operation. That means, you are first assigning i3 with the existing value of i3 itself and then immediately incrementing it by 1. But this assigned i3 is now zero itself and not one. If you split up this into two lines of code, it would be like this:





But when the second line of code is executed, isn't i3 assigned a value of 1? So this example isn't the same as:

i3++ ; is it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18977
    
  40

The following code:



Can be translated to:



Or in other words, it doesn't really do anything.

Henry
[ January 07, 2005: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operators again