• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

explain this ...please

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic