Java Writer equals()方法及示例

Java Writer equals()方法及示例

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

签名

public boolean equals(Writer second_Writer)

语法

first_Writer.equals(second_Writer)

参数: 该方法接受一个强制性参数second_Writer,它指的是要与第一个Writer比较的第二个Writer。

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

以下程序用于说明java.io.Writer.elements()方法的工作。

程序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

程序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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程