-1

I have got some information that string addition using + allows moved to new memory address in java but in case for int it is not happening why?

means

String a="fi-rstname";
a=a+" "+lastname;

a+" "+lastname moves to the new address instead of address of a

but for

 int a=22;
a=a+2323;

a+2323; vale does not move to the new address for addition why?

Suraj Khanra
  • 480
  • 1
  • 5
  • 23

2 Answers2

1

String is immutable in Java. You can't change the content of immutable field after initialization. That is the reason, you create a new memory case.

You can get more information in this post : String is immutable. What exactly is the meaning? that explain well why String is immutable.

Youssouf Maiga
  • 6,701
  • 7
  • 26
  • 42
0

Because String is immutable so each time you make change in string it'll create new object

in int we can make changes because primitive datatype is mutable(changeable).