Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring variables using Java wrapper classes

Tags:

java

reference

In Java, we can use the wrapper classes for declaring a variables. For example

    Integer x=5;

This means that there is a reference 'x' that points to a value of 5.

Then I declared another reference called y that points to the same value

    Integer y=x;  //now y should point to the number "5"

then I changed the value which y points to

     y=20;

Doesn't this make both x and y point to 20 ? because when I print x , I still get 5

like image 651
Exorcismus Avatar asked May 13 '26 23:05

Exorcismus


1 Answers

The following:

y=20;

rebinds y to point to a different Integer object.

It does not touch x, so its value does not change.

like image 64
NPE Avatar answered May 15 '26 12:05

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!