当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


相关用法


注:本文由纯净天空筛选整理自Code_r大神的英文原创作品 Writer equals() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。