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 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" Watch "unary operator" New topic
Author

unary operator

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
Q:An unary operator operates on a single value
1.True
2.False
The answer is given as False,bcos + and -,can take two values.
Since,+ and - can be used both as unary and binary operator,what would be the best possible explanation in this context?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The same symbols are used for unary operators:
+variable
-variable
as for additive operators:
var1 + var2
var1 - var2
I disagree with the answer. Because additive operators are not unary ones. Though they share the same symbols.


SCJP2. Please Indent your code using UBB Code
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
When '+' or '-' is used as a unary operator,it acts only on a single value,right.
So,the answer should be true.
There was another question about operands :
Q:All operands r evaluated left to right.
1.true
2.false.
The answer is given as true.Will the precedence and associativity rules not apply?
Pl.clarify.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
After the operands are evaluated, the operations are performed based on the precedence of the operators (and parentheses). Associativity plays a rol when considering operators with the same precedence.
Please see an example
JLS deals with Evaluation Order
 
jQuery in Action, 2nd edition
 
subject: unary operator
 
Similar Threads
convert Postiive number to negative..
Operators
what does =+ do?
infix to postfix with negative numbers
try it