This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unary operator evaluation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unary operator evaluation" Watch "Unary operator evaluation" New topic
Author

Unary operator evaluation

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Hi every1
Can any1 tell me how are these statements evaluated ?




They are unary operators. So how is the calculation being done ?


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

It may help to understand that those are not unary operators; they're binary (left and right operands). They are evaluated left-to-right in order of precedence (accounting for parentheses, of course).
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

P.S. The unary operators are +, -, ++, --, ~, !, and cast operators.

Examples:

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
I'm sorry I mentioned unary by mistake.

I meant Bitwise logical operators.

Thanks for pointing that out.

So how is it evaluated anyway ?
santhoshkumar samala
Ranch Hand

Joined: Nov 12, 2003
Posts: 156

The code is evaluated is as follows:

The numbers converted into the binary format and the opertaors on the binary format.

int a = 1 | 2 ^ 3 & 5;
The operation will be like this 001|010^011&101

The precedence is like this &,^,|

so the steps to the result are

1)001|010^001
2)001|011
3)011

The result is 3


santhosh<br />SCJP,SCWCD
Ryan Kade
Ranch Hand

Joined: Aug 16, 2005
Posts: 69
The results are:

a=3
b=0
c=3

The order of operations for a has already been explained by santoshkumar, and as he said, it's equivalent to the forced order that you imposed for c.
 
Don't get me started about those stupid light bulbs.
 
subject: Unary operator evaluation
 
Similar Threads
Operator Precedence
operator precedence & associativity
Unary operator and precedence evaluation
1.5 Integer with +=
what are compile time constant