+ 7

Are Strings reference type?

I reverse my information in java and see this note arrays and strings are also reference type what I know about that is the reference type change when call a method that change it and the other does not for example edited String x="aba"; change(x); //will not affect String []y={"aba"}; change(y]); //will affect is that true ??

5th Sep 2018, 9:25 PM
ABADA S - avatar
4 Answers
6th Sep 2018, 12:02 AM
NimWing Yuan
NimWing Yuan - avatar
+ 4
https://code.sololearn.com/cw81bEZ2qe1q/?ref=app does the previous code show the defference between reference or not ?to make sure that I really understood reference type truly we can see there the array element was affected by the method but the string is not
5th Sep 2018, 10:51 PM
ABADA S - avatar
+ 2
Strings are immutable in Java, so you can't modify the object once created. methods like substring etc. return a new String instead. The code is a bit misleading, if you just make an assignment inside the method then both will not be affected, because you are just reassigning the local variable that is passed by value. But when you modify a property of the object or index of the array you are dereferencing the object reference that was passed into the method and this is what allows reference types to be affected outside the scope of a method. As I said, Strings are immutable so you can't change its internal state once you create it
6th Sep 2018, 2:18 AM
Dan Walker
Dan Walker - avatar
String and arrays are implemented in objects in java. which means the variables of them holds the reference type. all object arguments are passed by reference not by value. String x = "abc"; String[] y = {"abc"}; change (x);// will affect. change (y (0));// will affect.
5th Sep 2018, 10:29 PM