0

String is passed by value or reference?

String is passed by value or reference? As it is an object I think pass by reference but when I tried to create an impure method based on strings then the string wasn’t mutated .... please help 🙏

3rd Oct 2018, 7:18 PM
Hbot07
Hbot07 - avatar
3 Answers
+ 3
Everything in java is passed by value
3rd Oct 2018, 10:10 PM
D_Stark
D_Stark - avatar
+ 1
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created its data or state can't be changed but a new string object is created. I guess, that the string object that was first referenced by „reference“ was not altered, all that you did was make „reference“ refer to a new string object.
3rd Oct 2018, 8:30 PM
asa22
0
in java, parameters are passed by value. An object variable is in fact a reference to the content of that variable in memory, and that reference is passed by value, that's why you can modify the members of a mutable object within a function. String are non mutable , you can't change them.
5th Oct 2018, 4:57 AM
ifl
ifl - avatar