Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Secure Financial Transactions with Ansible, Terraform, and OpenSCAP
this week in the
Cloud/Virtualization
forum!
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
Ron McLeod
Paul Clapham
Jeanne Boyarsky
Bear Bibeault
Sheriffs:
Rob Spoor
Henry Wong
Liutauras Vilda
Saloon Keepers:
Tim Moores
Carey Brown
Stephan van Hulst
Tim Holloway
Piet Souris
Bartenders:
Frits Walraven
Himai Minh
Jj Roberts
Forum:
Programmer Certification (OCPJP)
Why this happens
Sujeevan Nagarajah
Greenhorn
Posts: 8
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I know this is a bad question. In real situation we won't use this. But I just want to know why this happens.
int a = 1;
a = a++;
System.out.println(a);
Why does this print 1?
Since this is a postfix increment, the value is assigned first So value is 1. Then executes the postfix increment. So value should be 2.
Anybody knows the answer?
Sujeevan Nagarajah.
Jesper de Jong
Java Cowboy
Posts: 16084
88
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This question is asked often on JavaRanch, so we have a section in our FAQ about this:
Post Increment Operator and Assignment
.
Jesper's Blog
-
Pluralsight Author Page
Sujeevan Nagarajah
Greenhorn
Posts: 8
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thank You Jesper.
That means the value of the increment is evaluated first regardless of the type of increment. But post increament uses value before increment (remembered) and pre increment uses value after increment.
Sujeevan Nagarajah.
Jesper de Jong
Java Cowboy
Posts: 16084
88
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes, exactly!
Jesper's Blog
-
Pluralsight Author Page
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Post increment and the assignment operator
i=i++ .................................?
PostFix Notation Confusion
i & i++ print same( int i) , how?
increment operator ++
More...