Thursday, February 19, 2026
HomeLanguagesJavaStringWriter equals() method in Java with Example

StringWriter equals() method in Java with Example

The Java.io.StringWriter.equals(Object obj) method of StringWriter class in Java is used to check whether the two instances of StringWriter are equal or not. It returns a boolean stating whether they are equal or not.

Signature:

public boolean equals(StringWriter second_StringWriter)

Syntax:

first_StringWriter.equals(second_StringWriter)

Parameters: This method accepts a mandatory parameter second_StringWriter which refers to the second StringWriter to be compared to the first StringWriter.

Return value: The method returns true if the equality holds and both the objects and StringWriter are equal else it returns false.

Below programs are used to illustrate the working of the java.io.StringWriter.elements() method:

Program 1:




// Java code to illustrate the equals() method
  
import java.io.*;
  
public class StringWriter_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty StringWriter
        StringWriter writer1 = new StringWriter();
  
        // Inserting elements into the StringWriter
        writer1.write("GeeksForGeeks");
  
        // Displaying the StringWriter
        System.out.println("StringWriter 1: "
                           + writer1.toString());
  
        // Creating an empty StringWriter
        StringWriter writer2 = new StringWriter();
  
        // Inserting elements into the StringWriter
        writer2.write("GFG");
  
        // Displaying the StringWriter
        System.out.println("StringWriter 2: "
                           + writer2.toString());
  
        System.out.println("Are both of them equal? "
                           + writer1.equals(writer2));
    }
}


Output:

StringWriter 1: GeeksForGeeks
StringWriter 2: GFG
Are both of them equal? false

Program 2:




// Java code to illustrate the equals() method
  
import java.io.*;
  
public class StringWriter_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty StringWriter
        StringWriter writer1 = new StringWriter();
  
        // Inserting elements into the StringWriter
        writer1.write("GFG");
  
        // Displaying the StringWriter
        System.out.println("StringWriter 1: "
                           + writer1.toString());
  
        // Creating an empty StringWriter
        StringWriter writer2 = new StringWriter();
  
        // Inserting elements into the StringWriter
        writer2.write("GFG");
  
        // Displaying the StringWriter
        System.out.println("StringWriter 2: "
                           + writer2.toString());
  
        System.out.println("Are both of them equal? "
                           + writer1.equals(writer2));
    }
}


Output:

StringWriter 1: GFG
StringWriter 2: GFG
Are both of them equal? false
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS