String is passed by value or reference? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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