# Doubt from Whizlab Question

cfish meena

Greenhorn

Posts: 19

posted 9 years ago

can anyone please explain me why the output is 3.And x-- should be 0.since it changes to 1 only in next place of x right ,as it is a post increment..Am i right ...Please explain this.

public class sample12 {

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;

}

}

public class sample12 {

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;

}

}

anil kumar

Ranch Hand

Posts: 447

posted 9 years ago

Hi

public class sample12 {

static{

int x=5;

}

static int x,y;//Default values are x=0,y=0

public static void main(String[] args) {

x--;

myMethod();//At this statement the value of x is -1 and y =0

System.out.println(x+y + ++x);//here 1+0+2

so output is 3

}

public static void myMethod(){

y=x++ + ++x;// at this statement y=-1+1

so y=0;but x=1;

because here x is incremeted twice

}

}

Thanks

Anil Kumar

public class sample12 {

static{

int x=5;

}

static int x,y;//Default values are x=0,y=0

public static void main(String[] args) {

x--;

myMethod();//At this statement the value of x is -1 and y =0

System.out.println(x+y + ++x);//here 1+0+2

so output is 3

}

public static void myMethod(){

y=x++ + ++x;// at this statement y=-1+1

so y=0;but x=1;

because here x is incremeted twice

}

}

Thanks

Anil Kumar

V Gala

Ranch Hand

Posts: 113

posted 9 years ago

hi

when code enter mymethod the value of x is -1

y=x++ + ++x;

x++->the value of x is read first as -1 and then incremented to 0.

++x->the value of x is first incremented to 1 and then read.

therefore the value of x is 1.

now y= -1+1=0

and x=1

now x+ y+ ++x

first expression are evaluated and then addition take place

++x->the value of x is first incremented to 2 and then read.

now x+y+++x = 1+0+2=3

when code enter mymethod the value of x is -1

y=x++ + ++x;

x++->the value of x is read first as -1 and then incremented to 0.

++x->the value of x is first incremented to 1 and then read.

therefore the value of x is 1.

now y= -1+1=0

and x=1

now x+ y+ ++x

first expression are evaluated and then addition take place

++x->the value of x is first incremented to 2 and then read.

now x+y+++x = 1+0+2=3

Amitji Sharma

Greenhorn

Posts: 16

posted 9 years ago

here is solution see comments

public class sample12 {

static{

int x=5;

}

static int x,y;;

public static void main(String[] args) {

x--; //x=-1 here because before this statement x=0(default value)

myMethod();

System.out.println(x+y + ++x);//here (x+y ++x)(1+0+2)and prints 3

}

public static void myMethod(){

y=x++ + ++x; //here y=-1+1 which is zero; and x becomes 1

}

}

public class sample12 {

static{

int x=5;

}

static int x,y;;

public static void main(String[] args) {

x--; //x=-1 here because before this statement x=0(default value)

myMethod();

System.out.println(x+y + ++x);//here (x+y ++x)(1+0+2)and prints 3

}

public static void myMethod(){

y=x++ + ++x; //here y=-1+1 which is zero; and x becomes 1

}

}

Amit Sharma<br />There is only one proof of ability that's Result.