aspose file tools*
The moose likes Java in General and the fly likes explain this ...please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "explain this ...please" Watch "explain this ...please" New topic
Author

explain this ...please

aman hindustani
Ranch Hand

Joined: Jun 15, 2006
Posts: 57
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

Joined: Dec 27, 2005
Posts: 1249

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 ]

The Best way to predict your future is to create it - Every great individual common man
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
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

Joined: Jul 20, 2005
Posts: 10202
    
166

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


[My Blog] [JavaRanch Journal]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

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

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explain this ...please