-4

I am very new to Java (4 weeks, 4 days a week), with zero prior programming knowledge. Can someone explain how this prints 32?

int a = 10;
a = a++ + a + a-- - a-- + ++a;
System.out.println(a);
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 2
    Please make an effort (and show it) to first look up what those operators do. – Sotirios Delimanolis Mar 19 '14 at 19:39
  • 5
    And discard this awful example. First lesson: never, EVER write code like that. Burn that book now. – duffymo Mar 19 '14 at 19:40
  • 2
    Please, just move on to the acquisiton of some more productive knowledge. You shall never, ever have to either read or write this in practice. If you don't know what it does or why, that's perfectly fine. – Marko Topolnik Mar 19 '14 at 19:40
  • This should not be one of your priorities. Learn how you would find the answer yourself instead, it will be more useful to you. – Pascal Cuoq Mar 19 '14 at 19:41
  • i'm sorry; looking at the chapter now on all things operators and this example confused me to all get out.thanks for the quick responses. seriously, wow that was fast. i realize it's a redundant example that will probably never be written in that form, but yes it's in our book – user3439312 Mar 19 '14 at 19:43
  • and i understand say i++ = i + 1, or i-- = i -1. i guess i was looking over one of the "+" or "-" when wrapping my head around it. either way, thanks again for all the responses :) – user3439312 Mar 19 '14 at 19:45
  • 2
    I would recommend switching to a different book to learn Java. Based on your question it looks like it came from the Java cert book by Mala Gupta (I am reading it now). This book is more designed for people who have a working knowledge of Java and are trying to prepare for for the exam and not beginning programmers trying to learn the basics. I started off with "Learning Java" from O'Reilly and I thought it moved at a good pace. Good Luck! – Tyson Moncrief Mar 19 '14 at 20:43

5 Answers5

3

a++ > means use then change .. So value of a is used first = 10 and then incremented = 11

++a > means change then use. So value of a is first changed then used.

So a = a++ + a + a-- - a-- + ++a;

 = (10) 
   + (11 [since a is incremented after use]) 
   + 11 [since a-- = use then change = 11, after -- becomes 10] 
   - 10 [since value of a is now decremented, and then decremented again, so a = 9 at this point] 
   + 10 [since ++a is change then use]

in summary

a = 10 + 11 + 11 - 10 + 10 = 32. 

Hope it helps :)

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Easy:

a = 10 + 11 + 11 - 10 + 10 = 32.  

It's clearer with parentheses added:

a = (a++) + (a) + (a--) - (a--) + (++a);
duffymo
  • 305,152
  • 44
  • 369
  • 561
1

Let's take this one step at a time.

  1. a++ will increment a by one.
  2. a++ +a will take the (11)+ the existing a(10), to give 21
  3. Another iteration will set a to 30, with a decremented by 1 at the end.
  4. - a-- will subtract 1 from a, and subtract this from the value. So -9,
  5. This one is the real trick. a is incremented before any other operation starts. So a becomes 11, before everything else even calculates.

Bottom line, this simplifies to:

4*a-a-2+1= 3*a-1, where a=11 because it has been incremented before anything started (++a).

If instead you moved the ++ to the other side of the ++a, you'd have 29, which is much easier to understand where it comes from.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • thank you for the response, i stated above i guess i was missing one of the "+" or "-". but thanks for the explanation! and wow, first post here, this question/answer/comment system might take a minute to get used to :) – user3439312 Mar 19 '14 at 19:49
0

The ++ and -- are evaluated based on where they are in relation to the variable, if it is a++ then a is first evaluated, then it is incremented. If you have ++a, then a is incremented and then evaluated.

So a++ + b will take a and add it to be, and then increment it, while, ++a + b will first increment a, and then add it to b.

0

In simple

public static int i = 10;

is indicating that the integer i has a value of 10.

then saying

i++;

will make i's value 11, so it just like saying 10 + 1

saying

i--;

will makes i's value 9, so its like saying 10 - 1.

then

i = i + 1;

will do the same as i++;

but its used like i = i + 20; in most cases to take its value and add 20 to it.

same for

i = i - 20;

but taking away instead of adding.

then

a + a;

that will double a.

Hope this helps, Luke.

Luke Melaia
  • 1,470
  • 14
  • 22