Método StringWriter equals() en Java con ejemplo

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

Firma:

public boolean equals(StringWriter second_StringWriter)

Sintaxis:

first_StringWriter.equals(second_StringWriter)

Parámetros: este método acepta un parámetro obligatorio second_StringWriter que hace referencia al segundo StringWriter para compararlo con el primer StringWriter.

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

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

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

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

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

StringWriter 1: GFG
StringWriter 2: GFG
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 *