programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# What's the result&#65311;

kwan Jang
Greenhorn
Posts: 16
int i=1;
int j=i++;
if((i==++j)|(i++==j)){
i+=j;
}
System.out.println("i="+i);

a:i=1 b:i=2 c:i=3 d:i=4 e:i=5 f:compliation fails.

what's the result?why?I just want to know why?and how can get the answer!thanks!!

Deepak Khillare
Greenhorn
Posts: 1
---the output is i=5

int i=1;
int j=i++;//here i becomes 2 but prior to that j is assigned 1 bcoz of //post-increment

if((i==++j)|(i++==j)){//first condition results true bcoz i is compared to 2 //and since it is bitwise 'OR' the right hand side expression is also //evaluated which results in i being incremented to 3
i+=j;//i is added 2 more to it hence now i is 5
}

Ramu Malur S R
Greenhorn
Posts: 26
Hi,

The output is 5.

int i=1; //i=1
int j=i++; //j=1,i=2 increment happens after assignment
if((i==++j)| //j is incremented first so, j=2, i is still 2, condition is
//true

(i++==j)){ //The operator "|" executes both the expressions irrespective
//of the result of the first expression.
//So the 2nd expression
//results in i=3

i+=j; //i=3+2
}
System.out.println("i="+i); //Result is 5

kwan Jang
Greenhorn
Posts: 16
thank you very much!

Joe Harry
Ranch Hand
Posts: 10128
3
Hi Kwang,

Try this,

int i=1;
int j=i++;
i=i++;
i=++i;
j=++j;
if((i==++j)|(i++==j)){
i+=j;
}
System.out.println("i="+i);

Try to guess the O/P in the above case. Hope this will explain you completely about post/pre increment assignments.

Prashant kumar Singh
Greenhorn
Posts: 22
Hi ranchers.
I want to know in following code at line 2.

at line 2 i am confused
Thanks,
Prashant Kumar Singh

Joe Harry
Ranch Hand
Posts: 10128
3
Hi Prashant,

the line i = i++ means you first assign the value of i to i before you make the increment. So when i = 1 and then saying i = i++ means i is given 1 and then incremented to 2 but that incremented value is never assigned to i, so i remains 1.

There is a similiar post some time before. Can anyone provide the link for prashant??

kwan Jang
Greenhorn
Posts: 16
i think "|" means "or",and "&" means "and".
if I use "|" the first expression is right,it won't run the 2 expression,
and "i" is also 2 ,not 3!

kwan Jang
Greenhorn
Posts: 16
i know, i mistook "|" for "||"
thanks everyone!

Ganesh Pujar
Ranch Hand
Posts: 45
Hi above,

check this out from JLS
15.24 Conditional-Or Operator ||
The || operator is like | (�15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false. It is syntactically left-associative (it groups left-to-right). It is fully associative with respect to both side effects and result value; that is, for any expressions a, b, and c, evaluation of the expression ((a)||(b))||(c) produces the same result, with the same side effects occurring in the same order, as evaluation of the expression (a)||((b)||(c)).

ConditionalOrExpression:
ConditionalAndExpression
ConditionalOrExpression || ConditionalAndExpression

Each operand of || must be of type boolean or Boolean, or a compile-time error occurs. The type of a conditional-or expression is always boolean.
At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (�5.1.8); if the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated. If the value of the left-hand operand is false, then the right-hand expression is evaluated; if the result has type Boolean, it is subjected to unboxing conversion (�5.1.8); the resulting value becomes the value of the conditional-or expression.

Thus, || computes the same result as | on boolean or Boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

Sanjeev Singh
Ranch Hand
Posts: 381

Posted by Jothi Shankar Kumar Sanjararaj
the line i = i++ means you first assign the value of i to i before you make the increment. So when i = 1 and then saying i = i++ means i is given 1 and then incremented to 2 but that incremented value is never assigned to i, so i remains 1.

This means i=i++ should be considered i=i.
Does not it sounds funny?
Can anybody provides the explanation for the same?
[ November 08, 2006: Message edited by: Sanjeev Kumar Singh ]

Prashant kumar Singh
Greenhorn
Posts: 22
Hi Jothi,
Thank you for explaining.
I want to come on Sanjeev query that he is saying that i=i++ means i=i
I want to come on basic of java. Means what is internally happening.In fact, two type of stacks are used by jvm in this case. One is Operand stack, which is used for doing operation on variables,and another is Method stack, which is used for store value.when we want to display value then jvm asks to operand stack for then value of that var.So operand stack again fetch value from the method stack.

But here,

operation is apllied on i but valu is not storing in method stack.

if you want more on stack then go through this link: http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html
Thanks you all to discuss here,
Prashant Kumar Singh

Saurabh Vyas
Ranch Hand
Posts: 72

Originally Posted by Sanjeev :

This means i=i++ should be considered i=i.

Yes i=i++ this can be considered as i=i, but the only difference would be that value of i would get incremented after assignment.

Sanjeev Singh
Ranch Hand
Posts: 381
No Saurabh,
The values will not get incremented after executing this line too.
[ November 08, 2006: Message edited by: Sanjeev Kumar Singh ]

Joe Harry
Ranch Hand
Posts: 10128
3
Hi All,

int i = 1;
i = i++;//here 1 is assigned to i and then it goes to the next line where i is incremented but that incremented value is not assigned to i.

There is a post regarding this some time ago. Does anyone has the link to it?? I'm not able to find it. So please anyone finds it post it here below.

Thanks guys.

Sanjeev Singh
Ranch Hand
Posts: 381
Hi Ranchers,

Posted by Prashant
operation is apllied on i but valu is not storing in method stack.