• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unary Operators

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I correct
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;//here value of i =0
System.out.println(x); //here it is 1
}
}
A)1--true
b)2
c)3
D)0
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile it?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is D ie 0 on compilation, but how??
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds weird, right? Even the expression,
int x =0;
x = x++;
results in assigning 0 to x;
I think we need to see how this is done step by step.
First the expression is evaluvated. In this case, we get x = 0.
Then, x is incremented; x = x + 1;
Finally the evaluvated expression is executed, so x = 0 is executed resulting in assigning 0 to x.
I hope i haven't missed anything.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is simple.
it goes via order of precedence.
ie "XOR" "AND" and "OR".
first it would return false.ie "true" XOR "true" is "false".
then it would return false. ie "false" AND "true" is "false".
then it would return true. ie "false" OR "false" is "true".
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uday-
Actually "AND" is higher in precedence than "XOR". So it evaluates like this:
<code><pre>
(b1 | b2 & b3 ^ b4)
(b1 | ((b2 & b3) ^ b4))
(true | ((true & true) ^ true))
(true | ((true) ^ true))
(true | (false))
(true)
</pre></code>
The rest of the question is, how is this evaluated?
<code><pre>int x = 0;
x = (true) ? x++ : --x;</pre></code>
This is equivalent to:
<code><pre>int x = 0;
x = x++;</pre></code>
...which is what Thandapani correctly explained.

[This message has been edited by Jim Yingst (edited February 10, 2000).]
 
uday krishna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi jim,
thanx for the suggestion.
i thought it was xor first.
and thandapani was right.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm, most interesting question, not because of the boolean tests but because of what results from the following expressions:
int x = 0;
x = x++;
System.out.println("x = " + x); // prints 0

int y = 0;
y = ++y;
System.out.println("y = " + y); // prints 1
Since BOTH the pre and postfix ++ operators have higher precedence than the assignment operator, one would think the result should be 1 in both cases. Go figure....
 
I am Arthur, King of the Britons. And this is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic