• 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 and binary operators

 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, i'm reading a book to pass the SJCP2 exam but while the author talks about unary and binary operators, he never explains what they are clearly:
could anyone of you sent me a list of these operators so that i can understand?
Regrds,
Marco
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unary operators are !, ~, ++, --, +, -, (), (cast), new.
All the other operators take two operands except ternary operator ?:
[ February 11, 2003: Message edited by: Sarma Lolla ]
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply now it's clear. The only thing is that + and - could be unary and binary operators at the same time, am i correct?
1 + 2 - 3 ==> binary
- - 4 ==>unary
Marco
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Binary operators operate on two operands.
ex +,-,*,/,%
ex op1 + op2;
op1/op2;
op1 - op2;
op1*op2;

Unary operators operate on single operand
ex ++ , --
op1++;
op2++;
Hope this clears your doubt
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramnath, you raised my doubts now?!
Are you implying that + and - are not unary operators?
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Culache:
Ramnath, you raised my doubts now?!
Are you implying that + and - are not unary operators?


Dan ,
They come under both categories....
I am trying to explain the distinction/....lets see how I will fare
caution...these rules are framed by me for myself... i dont know whether they exist somewhere
the operator precedence goes like , unary and then arithmetic
unary consists of (as someone else already said)
++,--,+,-,!,~,()(cast operator)
in this + and - can be recognized because they are always the prefix of the operand
i,e +4, +5 , +6, -5, -7.5
Arithmetic operators are *,/,% , +,-
these operate between two operands
i,e 5 + 6 , 6 - 5 and so on
Now
there can be some tricky situations like
6+ +5 ,
in these situations it is calculated as 6 + (+5)
because unary + has higher precedence over arithmetic operator + .
but if you see something like
6++5 , then it is a compiler error. I have read that java takes the longer version of operators always (listen...I am not really sure about the statement.... i think I have read it in Dan's exams). I might be wrong....over to our great Dan Chisholm to continue
Thanks and hope it will clear atleast some of your doubts
Sri
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was just trying to trick Ramnath into saying that +,- are not unary operators. I guess I'm getting some bad influence from the trick questions in the mock exams
Thanks for the 6+ +5, 6++5 example, useful indeed.
The rules you mentioned do exist. I believe I've seen them in the Java lang spec.
Summarized in the descending order of precedence:
Casts and ()
The unary operators ++, --, +, -, ~, and !
The multiplicative operators *, /, and %
The additive operators + and -
The shift operators <<, >>, and >>>
The relational operators <, <=, >, >=, instanceof
The equality operators == and !=
The bitwise and logical operators &, ^, and |
The conditional-and operator && and the conditional-or operator ||
The ternary conditional operator ? :
The assignment operators =, +=, -=, *=, /=, etc.
[ February 11, 2003: Message edited by: Dan Culache ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I think it is important to differentiate expressions with literals and expressions with primitive variables.
While 6+ +5 is legal (note the space between the two plus) 6++5 is illegal because ++ cannot be applied to the literal 6, i.e. 6 cannot be post-incremented.
If we declare
int i = 1;
int j = 1;
then i++j (as well as i ++j, i++ j, i ++ j) is illegal because during the lexical parsing the longest valid token is eaten by the parser, i.e. in this case we have three tokens, namely i, ++, j. The ++ applies to the variable j (or i) and there is no operator between i and j, thus this code is illegal.
Moreover, i+++j (i +++ j, i+++ j, i +++j, i+ ++j, i + + + j, ...) are all legal.
i+++j => (i++) + j
i +++ j => (i++) + j
i +++j => (i++) +j
i+ ++j => i + (++j)
i + + + j =>> i + (+ (+ j))
Make sure you never apply the ++ (or --) operator to a primitive literal since its value is constant, and thus, cannot be incremented.
A simple rule is that when the + (or -) operators are separated by a space the expression is always valid. For instance 1 + + + + + + + + 2 is valid and looks to the compiler as 1 + (+ (+ (+ (+ (+ (+ (+ 2))))))).
[ February 11, 2003: Message edited by: Valentin Crettaz ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin....
That is indeed a beautiful explanation.
It helps me appreciate the language better.
However,

Make sure you never apply the ++ (or --) operator to a primitive literal since its value is constant, and thus, cannot be incremented.

,
I did not understand the meaning of this one ....
Can you be generous enough to explain this for me...
Thanks in advance,
Sri
P.S. your mock exam is very good..
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure you never apply the ++ (or --) operator to a primitive literal since its value is constant, and thus, cannot be incremented.
this means that the compiler will not accept expressions, such as, but not limited to, 6++ + 5, or 6 + 5--, etc, since it doesn't make sense to increment the literal value 6 or decrement the literal value 5. You can only apply the ++ (and --) operator to non-final primitive variables, that is, in my example above, i and j.
P.S. your mock exam is very good..
Thank you but I still need to update it for the 1.4 exam
[ February 11, 2003: Message edited by: Valentin Crettaz ]
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million Val....
It didnt strike me when I was passively reading that one line.
Sri
Advance wishes for your day (!!!)
Valentin's day
[ February 11, 2003: Message edited by: Sri Sri ]
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Advance wishes for your day (!!!)
Sure My wishes to all lovers on this planet
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your further clarification.
 
reply
    Bookmark Topic Watch Topic
  • New Topic