0
String object in a java is reference type or value type?
String class object in java is value or reference type? String name = "Hemant"; String newName = "Santosh"; name = newName; name = "makar" what is the value of name snd newName?
4 ответов
+ 5
Name = "Santosh" and newName ="makar" according your code...
Addendum
What is your point actually?
+ 4
In Java Strings are immutable, so reassignment actually causes the variable to point to a new instance of String rather than changing the value of the String
String name = "Hemant";
String newName = "Santosh";
name = newName; //"Santosh"
name = "makar" //"makar"
So value of name and newName shall be
"makar" and "Santosh" (newName remains unchanged) respectively
0
@devender that means its value type
0
String object in a java is a reference type. Please read the chapter "Reference Types" again.
They have clearly mentioned, "Arrays and Strings are also reference data types".
Note: String also works in similar way of an Array.