File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ternary operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ternary operator" Watch "ternary operator" New topic
Author

ternary operator

pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
This question comes from Jamie Jaworski's book "Java 2 Certification":
What is the value displayed by the following program?
class Question {
public static void main (string[] args) {
int x = 0;
boolean b1, b2, b3, b4;
b1 = b2 = b3 = b4 = true;
x = (b1 | b2 & b3 ^ b4) ? x++: --x;
System.out.println(x);
}
}
A. -1
B. 0
C. 1
D. 2
The correct answer is B. I ran the code and verified it. Would someone explain to me why the answer isn't 1? Isn't the variable x the same variable that both receives the result of the ternary expression and is incremented inside the ternary expression afterwords?
The order of events as I understand it are as follows
- ternary expression evaluates to true
- current value of x (0) is returned to x
- x is incremented to 1
- x is printed
What am I missing here? Please help out a confused beginner. Thanks.
SRINI VASAN
Ranch Hand

Joined: Aug 29, 2000
Posts: 48
Hai friend,
The answer to your question is quit simple.
when you try to execute a code like below
int x =0;
x=x++;
The value stored in x is 0 than 1. This is because in the above assignment the value value incremented is not assigned to x , rather the original value.
i guess u are convinced :-)

Regards,
Srini


You can contact me in srinivas_an@yahoo.com
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by pete hesse:
This question comes from Jamie Jaworski's book "Java 2 Certification":
What is the value displayed by the following program?
class Question {
public static void main (string[] args) {
int x = 0;
boolean b1, b2, b3, b4;
b1 = b2 = b3 = b4 = true;
x = (b1 | b2 & b3 ^ b4) ? x++: --x;
System.out.println(x);
}
}
A. -1
B. 0
C. 1
D. 2
The correct answer is B. I ran the code and verified it. Would someone explain to me why the answer isn't 1? Isn't the variable x the same variable that both receives the result of the ternary expression and is incremented inside the ternary expression afterwords?
The order of events as I understand it are as follows
- ternary expression evaluates to true
- current value of x (0) is returned to x
- x is incremented to 1
- x is printed
What am I missing here? Please help out a confused beginner. Thanks.

Hi,
This is how I understand what happens.
int x=0;
x=x++;
x Starts with 0
The right hand side of the = is evaluated (++ is post increment, so x is not incremented yet)
0 is put into a storage area which I will call - Store.
x is then incremented to 1 (because of ++).
Lastly, Store is assigned to x which sets x back to 0.
In code form this could also help to explain what is happening
int x=0;
int y;
y=x;
x++;
x=y;
Hope this helps.
Guy
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
Thanks for the info. What you are saying makes sense, but if the order of events does go this way, then it contradicts the "postfix" implication!
- put current value in temp storage
- increment x
- return value in temp storage
I always assumed that
x = x++
was redundant, and never bothered to verify it, since the java documentation implies this. But we all know what happens when we assume!
Thanks for clarifying.
Pete
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
This question is for the conception of bitwise procedure:
x =(b1 | b2 & b3 ^ b4)
=(b1 | ((b2 & b3) ^ b4))=true;
=======================================
class Question {
public static void main (string[] args) {
int x = 0;
boolean b1, b2, b3, b4;
b1 = b2 = b3 = b4 = true;
x = (b1 | b2 & b3 ^ b4) ? x++: --x;
System.out.println(x);
}
}
A. -1
B. 0
C. 1
D. 2
The correct answer is B. I ran the code and verified it. Would someone explain to me why the answer isn't 1? Isn't the variable x the same variable that both receives the result of the ternary expression and is incremented inside the ternary expression afterwords?
The order of events as I understand it are as follows
- ternary expression evaluates to true
- current value of x (0) is returned to x
- x is incremented to 1
- x is printed
What am I missing here? Please help out a confused beginner. Thanks.[/B]
pete hesse
Ranch Hand

Joined: Aug 29, 2000
Posts: 44
Hi javardousoguru,
You've lost me. Are you asking a question?
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
about i=i++ problem
Actually, postfix increment operator has precedence level 2, and assignment operator = has precedence level 15. So.
i = i++;
must be equal
tempVar = i++;
i = tempVar;
and if i was initially 3, it has to become 4.
But even if to interepret i = i++; as
i = i;
i++;
it still has to be 4.
Could some Java expert explain, why is it 3 ???


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
Consider Paul's rocket mass heater.
 
subject: ternary operator