Writer equals() method in Java with Examples
The Java.io.Writer.equals(Object obj) method of Writer class in Java is used to check whether the two instances of Writer are equal or not. It returns a boolean stating whether they are equal or not.
Signature:
public boolean equals(Writer second_Writer)
Syntax:
first_Writer.equals(second_Writer)
Parameters: This method accepts a mandatory parameter second_Writer which refers to the second Writer to be compared to the first Writer.
Return value: The method returns true if the equality holds and both the objects and Writer are equal else it returns false.
Below programs are used to illustrate the working of the java.io.Writer.elements() method:
Program 1:
// Java code to illustrate the equals() method import java.io.*; public class Writer_Demo { public static void main(String[] args) { try { // Creating an empty Writer Writer writer1 = new PrintWriter(System.out); // Inserting elements into the Writer writer1.write("GeeksForGeeks"); // Displaying the Writer System.out.println("Writer 1: " + writer1.toString()); // Creating an empty Writer Writer writer2 = new PrintWriter(System.out); // Inserting elements into the Writer writer2.write("GFG"); // Displaying the Writer System.out.println("Writer 2: " + writer2.toString()); System.out.println("Are both of them equal? " + writer1.equals(writer2)); } catch (Exception e) { System.out.println(e); } } } |
Writer 1: java.io.PrintWriter@232204a1 Writer 2: java.io.PrintWriter@4aa298b7 Are both of them equal? false
Program 2:
// Java code to illustrate the equals() method import java.io.*; public class Writer_Demo { public static void main(String[] args) { try { // Creating an empty Writer Writer writer1 = new PrintWriter(System.out); // Inserting elements into the Writer writer1.write("GFG"); // Displaying the Writer System.out.println("Writer 1: " + writer1.toString()); // Creating an empty Writer Writer writer2 = new PrintWriter(System.out); // Inserting elements into the Writer writer2.write("GFG"); // Displaying the Writer System.out.println("Writer 2: " + writer2.toString()); System.out.println("Are both of them equal? " + writer1.equals(writer2)); } catch (Exception e) { System.out.println(e); } } } |
Writer 1: java.io.PrintWriter@232204a1 Writer 2: java.io.PrintWriter@4aa298b7 Are both of them equal? false
Recommended Posts:
- Writer getClass() method in Java with Examples
- Writer toString() method in Java with Examples
- Writer write(int) method in Java with Examples
- Writer flush() method in Java with Examples
- Writer close() method in Java with Examples
- Writer write(char[]) method in Java with Examples
- Writer append(char) method in Java with Examples
- Writer append(CharSequence, int, int) method in Java with Examples
- Writer append(CharSequence) method in Java with Examples
- Writer write(String) method in Java with Examples
- Writer write(char[], int, int) method in Java with Examples
- Writer write(String, int, int) method in Java with Examples
- Set equals() method in Java with Examples
- Map equals() method in Java with Examples
- ChoiceFormat equals() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

