When you say String is immutable, what do you mean by that?
Say I have String s = "Ness"
s= s+"Technologies";
What will happen? If the value gets appended, then what is
the meaning of immutable here?

Answer Posted / anjani kumar jha


Now u add s= s+"Technologies";
so it will NessTechnologies because you append. and assign
new varible,means that new addition is assigned to s

take one example for more clarity

String x = "Java";
x.concat(" Rules!");
System.out.println(x);

output is x=java
because string is immutable.............think hard on it


Consider the following another example---------and think
about that

public class NewClass {

public static void main(String as[])
{
String s="abc";
String s1=s+"cde";
System.out.println(s);
System.out.println(s1);


String x = "Java";
x.concat(" Rules!");
System.out.println(x);
}

}
output is
s=abc;
s1=abcde
x=java

since u have added cde in s but still from the output it is
abc...............means string is immutable,

For more contact me
Anjani Kumar jha
09623154095
CDAC,PUNE

Is This Answer Correct ?    6 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can I extend singleton class in java?

618


Which is fastest collection in java?

557


What language is pass by reference?

564


What super () does in java?

480


Explain about anonymous inner classes ?

571






What is a generic code?

522


How do generics work?

514


What is generic class?

640


What is the difference between static method and instance method in Java?

560


What is callablestatement? How you can call stored procedure to pass in parameter?

541


What is the equal sign?

553


Give any two differences between C++ and java.

553


Explain wait() method of object class ?

626


Can we use switch statement with strings?

614


What is the default value of local and global variables?

559