String vs String Buffer
String is a immutable and String Buffer is mutable. Confused?, no need to worry just understand the below definition and program , Its easy concept.
String - Let us discuss about sting , String class in Java is immutable because we want to increase the performance so once object is created with some content , if we try to add new content/change the existing object it would not change
Let us see the below program and its output to understand the nature of immutable class
Class String Test
{
public static void main(String[] args)
{
String s = new String("Hello");
s.concat("World");
System.out.println(s);
}
}
Guessing the output?
Here the output is Hello
String Buffer - String buffer is mutable class in java , which means if we try modify exiting object value it would get changed.
Class String Test
{
public static void main(String[] args)
{
String s = new String("Hello");
s.append("World");
System.out.println(s);
}
}
Here the Output would be HelloWorld
String is a immutable and String Buffer is mutable. Confused?, no need to worry just understand the below definition and program , Its easy concept.
String - Let us discuss about sting , String class in Java is immutable because we want to increase the performance so once object is created with some content , if we try to add new content/change the existing object it would not change
Let us see the below program and its output to understand the nature of immutable class
Class String Test
{
public static void main(String[] args)
{
String s = new String("Hello");
s.concat("World");
System.out.println(s);
}
}
Guessing the output?
Here the output is Hello
String Buffer - String buffer is mutable class in java , which means if we try modify exiting object value it would get changed.
Class String Test
{
public static void main(String[] args)
{
String s = new String("Hello");
s.append("World");
System.out.println(s);
}
}
Here the Output would be HelloWorld
0 comments:
Post a Comment