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 String Literal Vs String Object in Java Understanding the difference between String Literals and String Objects in Java plays a very important role. The main difference between String Literal and String Object is listed below:String Literal: A sequence of characters inside double quotes is known as a String Literal. String Literals are st 3 min read Collator equals(String, String) method in Java with Example The equals() method of java.text.Collator class is used to check if both strings are identical or not. Syntax: public boolean equals(String source, String target) Parameter: This method takes two strings between which comparison is going to take place. Return Value: if both strings are equal to each 2 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 StringBuffer offsetByCodePoints() method in Java with Examples The offsetByCodePoints() method of StringBuffer class returns the index within this String contained by StringBuffer that is offset from the index passed as parameter by codePointOffset code points. Unpaired surrogates lies between index and codePointOffset count as one code point each. Syntax: publ 2 min read StringWriter write(String) method in Java with Examples The write(String) method of StringWriter Class in Java is used to write the specified String on the stream. This String value is taken as a parameter. Syntax: public void write(String string) Parameters: This method accepts a mandatory parameter string which is the String to be written in the Stream 2 min read StringBuffer toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read Like