In java String is a class and it is imutable so we can not change its value.In following code it will concate other string without any error.So I want to ask that if it is immutable then why in this following code value of String is changed??
import java.util.*;
public class conc
{
public static void main(String args[])
{
String a="Sheetal";
a=a+"Ga";
System.out.println("Result:"+a);
}
}
Answer
In the code that you have shown, you have not changed the original String
object.
Instead, you have created a new String object, which represents a + "Ga"
, and then re-assigned it to the reference variable a
.
Note that all variables in Java
other than primitive types are references.
No comments:
Post a Comment