equals() on String and StringBuffer objects in Java Last Updated : 08 Jul, 2021 Comments Improve Suggest changes Like Article Like Report Consider the following codes in java: Java // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java // This program prints true class GFG { public static void main(String[] args) { String s1 = "GFG"; String s2 = "GFG"; System.out.println(s1.equals(s2)); } } Output: true The output is false for the first example and true for the second example. In second example, parameter to equals() belongs String class, while in first example it to StringBuffer class. When an object of String is passed, the strings are compared. But when object of StringBuffer is passed references are compared because StringBuffer does not override equals method of Object class.For example, following first program prints false and second prints true. Java // This program prints false class GFG { public static void main(String[] args) { String s1 = "GFG"; StringBuffer sb1 = new StringBuffer("GFG"); System.out.println(s1.equals(sb1)); } } Output: false Java // This program prints true class GFG { public static void main(String[] args) { String s1 = "GFG"; StringBuffer sb1 = new StringBuffer("GFG"); String s2 = sb1.toString(); System.out.println(s1.equals(s2)); } } Output: true Comment More infoAdvertise with us Next Article equals() on String and StringBuffer objects in Java N Niraj_Pandey Follow Improve Article Tags : Java Java-Strings java-StringBuffer Java-String-Programs Practice Tags : JavaJava-Strings Similar Reads StringBuffer append() Method in Java with Examples Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boole 13 min read StringBuffer setCharAt() method in Java with Examples The setCharAt() method of StringBuffer class sets the character at the position index to character which is the value passed as parameter to method. This method returns a new sequence which is identical to old sequence only difference is a new character ch is present at position index in new sequenc 2 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read StringBuffer reverse() Method in Java with Examples The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the Str 2 min read StringBuffer appendCodePoint() Method in Java with Examples appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an in 3 min read Like