• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

ternary operator

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi javardousoguru,
You've lost me. Are you asking a question?
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Do not set lab on fire. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic