Monday 18 February 2019

String class in java




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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...