Two Laptop Bag*
The moose likes Beginning Java and the fly likes Incrementing and decrementing. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incrementing and decrementing." Watch "Incrementing and decrementing." New topic
Author

Incrementing and decrementing.

James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Am I understanding this correctly...?



will print "My int is : 1
The reason for this is because the compiler acts like this : use the variable, then increment it after



will print "My int is : 2
The reason for this is the compiler acts like this : increment the variable BEFORE using it


The compiler therefore must be smart enough to understand the scope of the variable, so it knows when it can increment it?

I'm just trying to understand these operators correctly

Thanks


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Your understanding is correct. The Java Language Spec includes some detailed and highly technical language about precisely when the increment/decrement can happen in either case, but it boils down to just what you've said here.


[Jess in Action][AskingGoodQuestions]
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Also see this FAQ.


http://muhammadkhojaye.blogspot.com/
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

100% Correct .......


MCSA 2003 | Preparing For OCPJP/SCJP6
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Incrementing and decrementing.
 
Similar Threads
Help with an iteration
Performance
Mock Exam doubt increment expresion
Doubt in the output of this program.
Superclass and subclass reference type confusion