當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。