Método Writer equals() en Java con ejemplos

El método Java.io.Writer.equals(Object obj) de la clase Writer en Java se utiliza para verificar si las dos instancias de Writer son iguales o no. Devuelve un valor booleano que indica si son iguales o no.

Firma:

public boolean equals(Writer second_Writer)

Sintaxis:

first_Writer.equals(second_Writer)

Parámetros: este método acepta un parámetro obligatorio second_Writer que se refiere al segundo escritor para compararlo con el primero.

Valor devuelto: el método devuelve verdadero si se mantiene la igualdad y tanto los objetos como Writer son iguales; de lo contrario, devuelve falso .

Los siguientes programas se utilizan para ilustrar el funcionamiento del método java.io.Writer.elements():

Programa 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);
        }
    }
}
Producción:

Writer 1: java.io.PrintWriter@232204a1
Writer 2: java.io.PrintWriter@4aa298b7
Are both of them equal? false

Programa 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);
        }
    }
}
Producción:

Writer 1: java.io.PrintWriter@232204a1
Writer 2: java.io.PrintWriter@4aa298b7
Are both of them equal? false

Publicación traducida automáticamente

Artículo escrito por Code_r y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *