String object in a java is reference type or value type? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

10th Jun 2017, 12:37 PM
Hemant Makar
Hemant Makar - avatar
4 Answers
+ 5
Name = "Santosh" and newName ="makar" according your code... Addendum What is your point actually?
10th Jun 2017, 1:04 PM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 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
10th Jun 2017, 2:10 PM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
0
@devender that means its value type
10th Jun 2017, 5:14 PM
Hemant Makar
Hemant Makar - avatar
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.
10th Jul 2017, 7:58 AM
Arjun Singh
Arjun Singh - avatar