File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incrementing and decrementing." Watch "Incrementing and decrementing." New topic

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


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

Joined: Jul 08, 2003
Posts: 24183

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.
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
Mock Exam doubt increment expresion
Doubt in the output of this program.
Superclass and subclass reference type confusion
Help with an iteration