Quantcast
Viewing all articles
Browse latest Browse all 8

Difference between String and String Buffer and in which situations we use them

Image may be NSFW.
Clik here to view.
String is immutable but StringBuffer is mutable. I think it will be more useful while passing into a method. StringBuffer can be used as reference object. If you change in method's logic it will be reflected in StringBuffer which is actually sent as parameter in method since it is called by reference. String is immutable where new object is created when it is sent as parameter. There wont be reference with String object while you pass in method. Just String is sent as value in new String object. Eg: String a="java"; String b="beat"; a=b; Here the "b" object's value is copied to "a" object, but there wont be reference same for "a" and "b".

Viewing all articles
Browse latest Browse all 8

Trending Articles