aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What's the result? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What Watch "What New topic
Author

What's the result?

kwan Jang
Greenhorn

Joined: Sep 24, 2006
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

Joined: Nov 07, 2006
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

Joined: Oct 17, 2006
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


"Excellency is rarely found, more rarely valued"
kwan Jang
Greenhorn

Joined: Sep 24, 2006
Posts: 16
thank you very much!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
Hi ranchers.
I want to know in following code at line 2.

at line 2 i am confused
Please Anyone can explain it.
Thanks,
Prashant Kumar Singh
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9622
    
    2

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

Joined: Sep 24, 2006
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

Joined: Sep 24, 2006
Posts: 16
i know, i mistook "|" for "||"
thanks everyone!
Ganesh Pujar
Ranch Hand

Joined: Mar 22, 2006
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

Joined: Nov 01, 2006
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 ]

~Sanjeev Singh<br />SCJP 1.5
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
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

Joined: Sep 02, 2003
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

Joined: Nov 01, 2006
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

Joined: Sep 26, 2006
Posts: 9622
    
    2

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

Joined: Nov 01, 2006
Posts: 381
Hi Ranchers,

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

spreads some good rythmic sounds...

I can read this as the value of the i is assigned to i before it increments and the operand (+) and operator (1) will be pused back into the stack and will never be used.(If I am not wrong)

Bu(rkhard),Can you draw the content of the execution stack for this statement i=i++;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the result&#65311;