# Abbreviated Arithmetic

• 10-10-2010, 09:08 AM
SweetLD215
Abbreviated Arithmetic
Hello,

I'm very new to Java, and I'm actually taking an intro course so hopefully my questions isn't silly. :) One of the questions we are asked is why is abbreviated arithmetic important to java and how it's used effectively. I've done several google searches to research this but haven't come up with anything. Can someone explain it to me or give me some good resources to read about it, please?
• 10-10-2010, 01:37 PM
Norm
Quote:

abbreviated arithmetic
How does that relate to the arithmetic operators: +-*/%
• 10-10-2010, 08:55 PM
SweetLD215
Well, from our notes, they discuss two types of abbreviated arithmetic within two categories of augments - increments and decrements.
So it looks like it is talking about things like c+=a; instead of c = c + a; or ++c; instead of c = c+1;
Honestly, when I reviewed the notes, I was actually wondering why we use the abbreviations like this and what makes using these easier than writing c = c + a;

Any help would be very much appreciated. Thanks for responding so fast!
• 10-10-2010, 09:00 PM
Norm
Quote:

why is abbreviated arithmetic important to java
If this is an opinion survey, then I don't think c +=a is very important. It can lead to confusion when reading code.
The prefix and postfix notations are useful, but again can be confusing.
• 10-10-2010, 09:07 PM
SweetLD215
It's more of a discussion question that I have to answer. I've tried googling it with different wording, but it doesn't seem like there is anything discussing what makes abbreviated arithemetic important.
• 10-10-2010, 09:22 PM
Fubarable
Many feel that abbreviated arithmetic is little more than syntactic sugar, and per Alan Perlis, "syntactic sugar causes cancer of the semicolon".
• 10-10-2010, 09:34 PM
Zack
Quote:

Originally Posted by Fubarable
...and per Alan Perlis, "syntactic sugar causes cancer of the semicolon".

I'm going to go and cry myself to sleep now. Thanks, Alan Perlis.
• 10-10-2010, 10:13 PM
Fubarable
Quote:

Originally Posted by Zack
I'm going to go and cry myself to sleep now. Thanks, Alan Perlis.

There's no need to worry, no need to cry, and in fact you're in luck:

I know how to do a semicolonoscopy.

I am deeply sorry for any groans and indigestion that this pitiful pun has caused.
• 10-10-2010, 10:16 PM
Zack
Quote:

Originally Posted by Fubarable
I am deeply sorry for any groans and indigestion that this pitiful pun has caused.

I'll get you for this. I will. :rolleyes:
• 10-10-2010, 10:22 PM
SweetLD215
LOL. So what you all are saying is that abbreviated arithemetic really isn't all that important?
Oh this will totally make the discussion question harder to answer. I'll have to look up syntactic sugar and read about that one. LOL
• 10-11-2010, 04:09 AM
Rectal Exambot
Don't forget c *= 2 amd c /= 2 :D.

I love increments, it makes for loops easier to write....
• 10-11-2010, 07:11 AM
DarrylBurke
You missed c &= 2; c |= 2; and c ^= 2;

Hmm, are there any more?

db
• 10-11-2010, 08:06 PM
Zack
All that remains is the remainder: %=.

So we have a list then: ++ (pre/post), -- (pre/post), +=, -=, *=, /=, &=, |=, ^=, %=.
• 10-11-2010, 08:12 PM
JosAH
Part of the 'fun' is that the left hand operand isn't evaluated and not prone to widening conversions; let b be of type byte and do this: 'b+= 3' versus 'b= b+3'. In e.g. C more fun can be shown ;-)

kind regards,

Jos
• 10-11-2010, 08:13 PM
Zack
In those cases (which are rarer than just a basic "int x = 5; x += 2;" type thing), the programmer should make it explicit which types are being used and how they're being added. ;)
• 10-11-2010, 08:29 PM
JosAH
Quote:

Originally Posted by Zack
In those cases (which are rarer than just a basic "int x = 5; x += 2;" type thing), the programmer should make it explicit which types are being used and how they're being added. ;)

There's more between heaven and earth than just basic arithmetic; that's 'abreviated arithmetic'. It is a well defined language feature, whether you like it or not so why not use it?

kind regards,

Jos
• 10-11-2010, 08:31 PM
Zack
I totally agree that abbreviated arithmetic should be used (only 'cause I'm lazy but that's beside the point); however when conversions are involved in a convoluted way (such as with bytes and ints being mixed), there should be some kind of explicit conversion rather than just hoping that it converts correctly.
• 10-12-2010, 11:27 AM
joshdgreen
If I had to write an answer to this question, I'd probably just say that abbreviated arithmetic eliminates redundancy in the code.
• 10-12-2010, 10:52 PM
SweetLD215
Thanks for all the responses. You guys are awesome!
• 10-13-2010, 12:46 AM
Rectal Exambot
I always chuckle when I see c++, c = c + 1, therefore 0 = 1... I know, I am mentally impaired