String vs. StringBuffer
by , 01-08-2012 at 11:26 AM (652 Views)
Following are the major differences between these 2:
• String is immutable as compared to StringBuffer. You need to create a new instance to replace an object.
• StringBuffer is mutable. Also it is not synchronized.Java Code: This is an example of immutable string//Inefficient version using immutable String String output = “Some text” Int count = 100; for(int I =0; i<count; i++) { output += i; } return output;
Java Code: This is an example of mutable string//More efficient version using mutable StringBuffer StringBuffer output = new StringBuffer(110); Output.append(“Some text”); for(int I =0; i<count; i++) { output.append(i); } return output.toString();









Email Blog Entry
Size Reduced for Images in PDF &...
05-15-2013, 05:53 PM in Java Software