• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unary Operators?  RSS feed

 
Melissa Nikolic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).

So, my question is regarding unary operators. Specifically, can someone please clarify the difference between unary arithmatic operators (addition + and subtraction -) and negative (e.g. -1) and positive (+1) unary operators? I didn't think that addition and subtraction were unary operators; I thought they were binary operators.

Any clarification would be greatly appreciated.

Regards,

M
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Addition and subtraction are binary operators.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melissa Nikolic wrote:.unary arithmatic operators (addition + and subtraction -)

The unary arithmetic operators are ++ and --. They increment or decrement the variable by 1.
They are unary because they only require one operand e.g. i++, --x
 
Melissa Nikolic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Addition and subtraction are binary operators.


Thank you. Yes, this is what I thought. However, the Java Certification book I am reading also says they are both unary and binary. Hence my question.

M
 
Melissa Nikolic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Melissa Nikolic wrote:.unary arithmatic operators (addition + and subtraction -)

The unary arithmetic operators are ++ and --. They increment or decrement the variable by 1.
They are unary because they only require one operand e.g. i++, --x


Thank you. Yes, the unary operators also include the negative (e.g. -1) operator and positive (e.g. +1) operator in addition to those that you mentioned. My question was whether the addition and subtraction operators were also unary (as my books says, although as I mentioned I thought they were binary).

It seems that my book may be mistaken and the arithmetic addition (+) and subtraction (-) operators are binary, not unary.

M
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melissa Nikolic wrote:Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).


To be fair, people are not usually chastised simply for asking newbie questions. They will be spanked a bit on some sites for failing to do any research on their own before posting, for asking others to do their work for them, or for copping an attitude of entitlement. This place just goes out of its way to be explicitly friendly more than most sites, while other sites tend to be more down to business without any frills.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melissa Nikolic wrote:
Thank you. Yes, the unary operators also include the negative (e.g. -1) operator and positive (e.g. +1) operator in addition to those that you mentioned. My question was whether the addition and subtraction operators were also unary (as my books says, although as I mentioned I thought they were binary).

It seems that my book may be mistaken and the arithmetic addition (+) and subtraction (-) operators are binary, not unary.

M


"Addition" and "subtraction" are the names for binary + and -, while "unary plus" and "unary minus" are the names for the unary uses of those operators.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the book meant that the same symbols are used for both the unary and binary operations.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your book say the positive operator does? Then check that against the Java Language Specification (JLS). The JLS is not easy to understand, however.
 
Melissa Nikolic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Melissa Nikolic wrote:Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).


To be fair, people are not usually chastised simply for asking newbie questions. They will be spanked a bit on some sites for failing to do any research on their own before posting, for asking others to do their work for them, or for copping an attitude of entitlement. This place just goes out of its way to be explicitly friendly more than most sites, while other sites tend to be more down to business without any frills.


I suppose my experience has been different (mind you this is when I was looking for answers and came across these various confrontations on different sites). I think the point is that this site is friendly, whether one has to go out of one's way is a different thing entirely. This is my impression but if no frills means lack of civility I'll pass.

M
 
Melissa Nikolic
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What does your book say the positive operator does? Then check that against the Java Language Specification (JLS). The JLS is not easy to understand, however.


My book says that the positive operator is a unary operator. I was confused because there is another schematic that shows addition and subtraction as unary operators and subsequently as binary operators. But it looks like I'll just stick to positive and negative operators as unary operators and addition and subtraction as binary operators.

Thank you,

M
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Melissa Nikolic wrote:
Jeff Verdegan wrote:
Melissa Nikolic wrote:Hello,

I wanted to first thank the moderators for a place where newbies can come and ask questions without being berated or chastised (sadly I have seen this on numerous sites).


To be fair, people are not usually chastised simply for asking newbie questions. They will be spanked a bit on some sites for failing to do any research on their own before posting, for asking others to do their work for them, or for copping an attitude of entitlement. This place just goes out of its way to be explicitly friendly more than most sites, while other sites tend to be more down to business without any frills.


I suppose my experience has been different (mind you this is when I was looking for answers and came across these various confrontations on different sites). I think the point is that this site is friendly, whether one has to go out of one's way is a different thing entirely. This is my impression but if no frills means lack of civility I'll pass.

M


To each his (or her) own I guess. I avoided this site for many years because I found it too syrupy.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are unary and binary versions of + and -. You can use -i where the - is the sign-change operator. That is probably obvious to you. What is not quite so obvious is that you must use it before the value 2147483648 with nothing else intervening.
But what does it say about the unary + operator? You would have thought that converted a number to positive, but if you look in the JLS link I provided earlier, you find it does something completely different. I don’t find what it says in the Java Tutorials about unary + very helpful.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is not quite so obvious is that you must use it before the value 2147483648 with nothing else intervening.
But what does it say about the unary + operator? You would have thought that converted a number to positive


You would? I would have thought it would do the same thing as it does algebraically. That is, nothing.

For instance, I would expect


to be -3.

What I didn't expect was the promotion to int. That's what I love about the JLS--just when you think you've got it figured out, it sneaks up and surprises you.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has already been mentioned a few times in different words, but just to clear up some possible confusion.

The positive operator is a unary operator. The symbol it uses is +.
The addition operator is a binary operator. The symbol it uses is +.

They are two different operators, that happen to use the same symbol. When your book uses the word "positive", it's referring to the unary operator.

The clarification for negative and subtraction operators is analogous.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . I would have thought it would do the same thing as it does algebraically. That is, nothing.
. . .
What I didn't expect was the promotion to int. . . .
Yes, it’s a surprise, isn’t it.And the bit about must use - means there can be a puzzle in Java™ Puzzlers by Block and Gafter about “how can you break code by adding a pair of ()?”
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And the bit about must use - means there can be a puzzle in Java™ Puzzlers by Block and Gafter about “how can you break code by adding a pair of ()?”


Insidious. MWUAH HA HA HA!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the preface to that book? They say most of the puzzles were reported as bugs.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you read the preface to that book? They say most of the puzzles were reported as bugs.


Nope. Never even seen the book. I've heard of it though, and I guess it's not too surprising that a lot were reported as bugs. And this from a "simple" language like Java.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It’s great fun to read. And whenever there is spare space, it is filled with an optical illusion, making it even more entertaining to read.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Puzzlers is great. Between it and Effective Java, these two books taught me most of the best practices I use today, in Java and other languages alike.

Where Effective Java is great for good practices in general, Java Puzzlers will teach you a lot about the idiosyncrasies of the Java language itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!