Key Points:
Methods:
Example Showing difference between String and String Buffer implementation:
class Test { public static void main(String args[]) { // Example with String String str = "study"; String newStr = str.concat(" tonight"); // Concatenation creates a new string System.out.println("Original String: " + str); // Output: study System.out.println("New String: " + newStr); // Output: study tonight // Example with StringBuffer StringBuffer strB = new StringBuffer("study"); strB.append(" tonight"); // StringBuffer appends directly to the existing object System.out.println("StringBuffer: " + strB); // Output: study tonight } }
Result:
Original String: study New String: study tonight StringBuffer: study tonight
This illustrates a fundamental difference between String (immutable) and StringBuffer (mutable) in Java. With String, operations like concatenation generate new string objects, while StringBuffer allows direct modification of the string content without creating new objects, making it more efficient for extensive string manipulation.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions