-7

What happens if you change the variable b, or what if you change a. What does the order have to do with anything.

I know count = count + 1 but the two variables is messing up my brain.

b = 7;
a = 7;
a = b;
a += 1;

What happens to b?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Tastybot
  • 55
  • 9

5 Answers5

8

What happens to b?

Nothing happens to b.

When you do

a = b;

you're copying the value stored in b and putting it in a. (You're not making a an alias of b.)

When you then do a += 1; you're changing the value stored in a (and the value stored in b remains unchanged).

You can verify this by printing the final values after your code snippet:

System.out.println(a);  // prints 8
System.out.println(b);  // prints 7

What happens if you change the variable b, or what if you change a. What does the order have to do with anything.

a and b are two independent variables and changing one will never affect the other.

The order matters since when you do a = b the value of b is copied into a and whatever a stored before is discarded. If you had done a += 1 prior to a = b, then a would have been restored to 7 again.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Yeah I did. Also, why does this have downvotes? I wasn't able to print or do anything because I was at the library and couldn't run code. – Tastybot Jun 17 '15 at 12:30
  • That's not a very common situation to be in so I think people just downvoted for "lack of effort". A future tip, you can use http://ideone.com (example: http://ideone.com/xaoGwm). – aioobe Jun 17 '15 at 12:37
2

int is raw type you don't copy reference but the value itself. This will work same way for Integer because it is immutable class.

 int b = 7;
 int a = 7;
 a = b;
 a+=1;
 System.out.println(a);// ->8
 System.out.println(b);// ->7
Milkmaid
  • 1,659
  • 4
  • 26
  • 39
  • Your reference to `Integer` is a bit confusing. There's more to it than just immutability. (You wouldn't be able to do `+=` if it wasn't for autoboxing for instance.) – aioobe Jun 17 '15 at 11:32
  • @aioobe for sure more info [here](http://stackoverflow.com/a/3085357/4327527) I just want to avoid confusing why some classes does not works as reference. – Milkmaid Jun 17 '15 at 11:36
1

Still 7. integer is raw type and if you assign a int variable to another int, just its value is received by the new one. Not the object itself.

Asqan
  • 4,319
  • 11
  • 61
  • 100
1

b stays 7.
a becomes 8.

You could use System.out.println(); to print values of variables and find out yourself if you ever doubt. That or use the debugger.

Vahx
  • 626
  • 10
  • 23
1
public static void main(String[] args) {
        int b = 7; // b points to 7
        int a = 7; // a points to 7
        a = b; // b and a points to 7
        a += 1; // a points to 8 now, b is still pointing to 7
        System.out.println(a);
        System.out.println(b);
    }

output

8
7

When we do a += 1; we change the value stored in a (value stored in b is still same).

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116