This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain this ...please

 
aman hindustani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello to all...please explain this..why y,z is printing zero..


public class justSimple
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int x =0;
int y =0;
int z = 0;
y = y++;
z = y;
System.out.println("the value of x is "+x);
System.out.println("the value of y is "+y); // 0
System.out.println("the value of z is "+z); // 0
}
}
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by aman hindustani:
hello to all...please explain this..why y,z is printing zero..


public class justSimple
{
public static void main(String[] args)
{
System.out.println("Hello World!");
int x =0;
int y =0;
int z = 0;
y = y++;
z = y;
System.out.println("the value of x is "+x);
System.out.println("the value of y is "+y); // 0
System.out.println("the value of z is "+z); // 0
}
}


Howdy aman hindustani,

Welcome to the JavaRanch...

However, you may not have noticed that we have a policy on screen names here at the ranch. It must consist of a first name, a space, and a valid last name. It must also not be fictitious.

Unfortunately, your screen names do not seem to conform with this policy. Please take a moment to change them.

PS: Moderators Please look into this issue.

Anyways About your current question, you should first search this forum for earlier threads...

Anyways check out this link

Increment operation question

Hope this will helps you.
[ September 18, 2006: Message edited by: Ankur Sharma ]
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I won't play wanna be moderator, but will try to help you.

int y =0;
int z = 0;
y = y++;
z = y;

y++ is post increment. That is, it takes the value of y, does whatever it is supposed to, then increments it. As an aside try this:

int y=0;
y++;
System.out.println(y);

You could put ++y in this case and get the same result. This is because there is nothing else to do with the value, but increment it, and store it in y.

So what is going on here in a nutshell is the value is y++ is executed, but not stored in y yet, and never will be in this example, then the assigment operator is called, and since y is still 0, z=0.

There are a lot more technical details involved, but this is the basic gist,

i++ == i=i+1

Here is a link the JavaRanch FAQ that addresses this issue.

http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment

The FAQ is a pretty good source to search first.
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the value should have been:



instead of:



I changed the code slightly, to:



Note, the statement marked in bold.

And here's the output that i saw:


I am just a bit puzzled as to why:

did not yield the same result
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Robert Hill:
Here is a link the JavaRanch FAQ that addresses this issue.

http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment



I should have read that, before posting my earlier observations. Thanks, Robert Hill for that link.


[ September 18, 2006: Message edited by: Jaikiran Pai ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aman hindustani,
Welcome to JavaRanch!

In an effort to help you get the most from our forums, we've compiled a
list of tips for asking questions here. You can find the list in our
FAQ section here.
In particular please see:
UseAMeaningfulSubjectLine

"explain this ...please" tells us nothing of the nature of your question.

Again, welcome to JavaRanch and good luck with your question.
-Ben
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic