Q:

Handling java types

Does java really handle all primitive types differently than custom Objects? I pose this question as I   examined and tried to "interpret" the results of this simple experimental program: 
public class RandomObject {

 String name;
 int value;

public RandomObject(String s, int i){
setName(s);
setValue(i);
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
 }

}

public static void main(String[] args) {

int x = 2;
int y = x;
System.out.println(y);
x = 4;
System.out.println(y);

RandomObject obj1 = new RandomObject("object1", 4);
RandomObject obj2;

obj2 = obj1;

System.out.println(obj2.getValue());
obj1.setValue(17);
System.out.println(obj2.getValue());

 }

The results are: 
2
2
4
17
Although x has changed, the value of y remains immutable, whereas in objects the change affects both of them.
Does the same happen in all primitive types (other than integers) and why? 

A:

Java does treat the values stored in the y and obj2 variables at the point that they are assigned (y = x and obj2 =obj1` respectively) similarly, but the key difference is that with Objects the variable only contains a reference to an actual object, not the data-values themselves.
int x,y;
x = 2;
y = x; //x = 2, y = 2
x = 3; //x = 3, y = 2 - Changing x does not change y

Object a,b;
a = new MyObject("foo"); //Create Object O on the heap, a = [Address of Object O]
b = a; //a = [Address of Object O], b = [Address of Object O]
a.updateValue("barr"); //Update a property of Object O
//a and b still contain [Address of Object O], but Object O has a new value.
a = new MyObject("Bazz"); //Create Object P, a = [Address of Object P]
//a = [Address of Object P], b = [Address of Object O]

The key thing to note is that the variables only get updated when there is an assignment (=) statement.

