A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Spring in Action
this week in the
Incrementing and decrementing.
Joined: Dec 21, 2007
Aug 17, 2010 10:34:33
Am I understanding this correctly...?
int myInt = 1; System.out.print("My int is :" + myInt++);
will print "My int is : 1
The reason for this is because the compiler acts like this :
use the variable, then increment it after
int myInt = 1; System.out.print("My int is :" + ++myInt);
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 :
Google App Engine
My Bite-Size SCJP Study Blog
author and iconoclast
Joined: Jul 08, 2003
Aug 17, 2010 10:43:30
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]
Joined: Apr 12, 2009
Aug 17, 2010 12:13:38
Also see this
Joined: Aug 07, 2010
Aug 20, 2010 19:57:52
100% Correct .......
MCSA 2003 | Preparing For OCPJP/SCJP6
I agree. Here's the link:
subject: Incrementing and decrementing.
Mock Exam doubt increment expresion
Superclass and subclass reference type confusion
Help with an iteration
Doubt in the output of this program.
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014