Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ternary operator

 
pete hesse
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi javardousoguru,
You've lost me. Are you asking a question?
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic