• 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

Is + , - r Unary operators?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am thiniking upto now that +,- r binary operators
and ++,-- r unary operators.
Since a+b (a,b r two operands)
++a (a is a single operand)
For this concept i understand like that,But in Mughals book +,- r both unary and binary operators.i get confused.
Pl clear my doubt???
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anupama
You can use them as either binary or unary. Binary is the traditional x+y and x-y usage.
As unary the + re-enforces the fact that the variable/literal is a postive number, it will also promote the operand to an int if it is a byte, short or char..
The - makes an operand negative in value and will also promote to an int if it is a byte short or char.
for a more detailed explaination go here: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#24924
hope that answers your question.
Dave
[This message has been edited by Dave Vick (edited May 31, 2001).]
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends on the context:
<pre>
int i = 5;
int j = -i; // using "-" as a unary operator
int k = i - 2; // using "-" as a binary operator
</pre>
Pretty much the same thing for "+"
HTH
Junilu
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,Junilu
For this i came to know that operator(+,-) is used for calculating the results for expressions and significance like it is +(positive) r -(Negative).
If worng pl correct me.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
apart from expression calculation they have significance in precedence order too.unary plus/unary minus has higher precedence then binary plus/binaty minus operators. moreover, unary plus/minus have right to left associativity whereas the binary counterpart(i.e. bainary plus/minus) have left to right associativity
Hope that helps
Lusha
 
Anupama Kota
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lusha
Thanks for clearing my doubt
reply
    Bookmark Topic Watch Topic
  • New Topic