aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in ++ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in ++ operator" Watch "doubt in ++ operator" New topic
Author

doubt in ++ operator

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
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


Thanks and Regards, Amit Taneja
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

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


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
SomeswaraRao Vudattula
Greenhorn

Joined: Mar 28, 2005
Posts: 12
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

Joined: Mar 14, 2003
Posts: 810


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 ]
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
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
 
wood burning stoves
 
subject: doubt in ++ operator