• 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

doubt in ++ operator

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

in one of the mock exam ... i face the question as follown

What will happen when you attempt to compile and run the following code?


public class Static
{
static
{
int x = 5;
}

static int x,y;
public static void main(String args[])
{
x--; myMethod();
System.out.println(x + y + ++x);
}

public static void myMethod()
{
y = x++ + ++x;
}
}


the o/p is 3 ...How ? so i put println statement in various place as follow

public class Static
{
static
{
int x = 5;
System.out.println("x= "+x);
}

static int x,y;
public static void main(String args[])
{
System.out.println("x= "+x);
x--;
System.out.println("x= "+x);
myMethod();
System.out.println("x= "+x);
System.out.println(x + y + ++x);
System.out.println("x= "+x);
System.out.println("y= "+y);
}

public static void myMethod()
{
System.out.println("x= "+x);
System.out.println("y= "+y);
y = x++ + ++x;
System.out.println("x= "+x);
System.out.println("y= "+y);
}
}

and its o/p is as follow -


D:\java_prac>java Static
x= 5
x= 0
x= -1
x= -1
y= 0
x= 1
y= 0
x= 1
3
x= 2
y= 0


but my question is why variable y=0 when we done y = x++ + ++x;

pls tell me

regards
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi amit,

try to understand this statement:

y = x++ + ++x;

same as:

y = -1 + ++x;
y = -1 + 0;
y = 0;
x = 1;

x++ uses post increment and it means it increments its value AFTER the statement which x is in. So x still equals -1. Later in the same statement will get to a ++x, which increments x on the spot, so now x=0. So now y = 0.
But don't forget that x now = 1 because we had a post increment operation in the statement, so x got incremented after the statement.

I think that was the trickiest part, the rest should be ok to understand.

I hope i explained myself correctly.

Cheers
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

First We need to know one thing..Individual Static BLock with Variable X is not accessible for Main().. so X=0 and y=0 in main()

before coming to this following exp x = -1 and y = 0
y = x++ + ++x;
in this exp first x++ will be executed -> x++
-> x= -1 + 1
-> x= 0
in this exp next ++x will be executed -> ++x
-> x = 0 + 1
-> x = 0
next we will go for total exp

y = x++ + ++x
y = 0 + 0
y = 0
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


confused with this thread ...seriously
another doubt in ++ operator..

pls give final conclusion to the link also

i will be greatfull to all u


but what i have make the final conclusion is
x+++++x is equalant to (x++)+(++x);
and not =((x++)++)+x
and we can't write x+++++x as we have to put space like x++ + ++x
as its giving error in the link above ?

any way do post ur comments
regards
[ June 11, 2005: Message edited by: amit taneja ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

your doubt helped me in understanding the increment operator.
In case of a post increment operator, its value gets incremented after the expression is evaluated.

For e.g in the expression,
x=-1;
y=0;
y = x++ + ++x;

x++ itself is an expression, but usually we make mistake in taking the whole equation for y as an expression while applying the post increment rules.

if we take x++ or ++x itself as an expression, the execution steps are as follows,

y = (-1) + ++x; /* the value of x is substituted as it is in the expression x++ and then the value is incremneted by 1, so x now becomes 0*/

y= (-1) + (1) /*since the pre increment operator increments the value of x during execution of the expression x gets incremneted from 0 to 1.

therefore,y=0 and x=1
 
reply
    Bookmark Topic Watch Topic
  • New Topic