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 } }
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
posted
0
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...
Hope this will helps you. [ September 18, 2006: Message edited by: Ankur Sharma ]
The Best way to predict your future is to create it
Ankur Sharma
Robert Hill
Ranch Hand
Joined: Feb 24, 2006
Posts: 94
posted
0
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.
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