Sid Go
Sid Go

Reputation: 2141

I'm confused about the basics of pointers/references

Given this:

int a = 10;
int b = a;
a++;

Is b = 10 or 11? if 10, then why does this happen (I'm using Android as an example):

TextView x = new TextView();
TextView y = x;
x.setText("abcde");

which leads to y's text being also set to "abcde", doesn't it?

EDIT:

What if I use 'Integer' instead of 'int'? Will b = 11?

Upvotes: 1

Views: 72

Answers (6)

Shilong Dai
Shilong Dai

Reputation: 81

With the Integer question, Integer in Java is immutable. You will have to make a new Integer to increment. Therefore, it won't work.

The other part is already answered.

Upvotes: 0

Hammad Tariq
Hammad Tariq

Reputation: 13431

What if I use 'Integer' instead of 'int'? Will b = 11?

Answer: Integer is a wrapper class, when we want to use Objects instead of primitive data types we use wrapper classes. and "b" will be same in both cases. Wrapper classes provides mechanism for converting primitive types into objects and objects into primitive types (boxing and un-boxing)

Other part is explained well by other guys so am not adding anything for it.

Upvotes: 0

Ajit Medhekar
Ajit Medhekar

Reputation: 1078

In the second case, Memory is created for that object. And object y is pointing to object x. So Object x and object y are accessing same memory. Whenever we change any property of object x, it's get stored in that memory location. As object y also pointing to same memory location, values gets reflected to properties of object y as well.

In your first case, we are just assigning values to a variable, we are not assigning a separate memory block for that variable.

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15644

You need to go back to basics.

Things work differently for primitives and objects in Java.

For instance, for code below, a and b are different variables and have their individual values.

int a = 10;
int b = a;
a++;

So when you do a++ it only affects value for a and not b because they are primitives and they have separate individual values.

But for this code

TextView x = new TextView();
TextView y = x;
x.setText("abcde");

Both x and y are different variables, but they both are referring to the same object in memory.

So when you change the value using one referrer, it will get changed on the actual object in memory, and hence you get the new value when you access it, although this time using a different variable referring to the same object in memory.

Upvotes: 2

Eran
Eran

Reputation: 393771

An int is a primitive, so a and b don't refer to an object, they just hold a value. Therefore the assignment int b = a; copies the original value of a to b and a++ only modifies a.

With reference types, the behavior is different, as shown in your TextView snippet. x and y refer to the same TextView instance (object), so x.setText("abcde") modifies the single instance referred by both.

Upvotes: 2

Vampire
Vampire

Reputation: 38619

Well, a++ is the short form of a = a + 1 so you assign a new value to a which is the result of adding 1 to a. But in the second example you call a method on the object that is referenced by both x and y, so you modify the object.

Upvotes: 0

Related Questions