📜  Java中的 StringWriter equals() 方法及示例

📅  最后修改于: 2022-05-13 01:54:51.532000             🧑  作者: Mango

Java中的 StringWriter equals() 方法及示例

Java.io。 Java中StringWriter类的StringWriter .equals(Object obj)方法用于检查StringWriter的两个实例是否相等。它返回一个布尔值,说明它们是否相等。

签名:

句法:

first_StringWriter.equals(second_StringWriter)

参数:此方法接受一个强制参数second_StringWriter ,它指的是要与第一个 StringWriter 进行比较的第二个 StringWriter。

返回值:如果等式成立并且对象和 StringWriter 都相等,则该方法返回true ,否则返回false

以下程序用于说明Java.io.StringWriter.elements() 方法的工作原理:

方案一:

// 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));
    }
}
输出:
StringWriter 1: GeeksForGeeks
StringWriter 2: GFG
Are both of them equal? false

方案二:

// 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));
    }
}
输出:
StringWriter 1: GFG
StringWriter 2: GFG
Are both of them equal? false