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


Java StringWriter equals()用法及代碼示例


Java中StringWriter類的Java.io.StringWriter.equals(Object obj)方法用於檢查StringWriter的兩個實例是否相等。它返回一個布爾值,說明它們是否相等。

簽名:

public boolean equals(StringWriter second_StringWriter)



用法:

first_StringWriter.equals(second_StringWriter)

參數:此方法接受強製性參數second_StringWriter,該參數表示要與第一個StringWriter進行比較的第二個StringWriter。

返回值:如果相等相等且對象和StringWriter相等,則該方法返回true,否則返回false。

下麵的程序用於說明java.io.StringWriter.elements()方法的用法:

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

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


相關用法


注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 StringWriter equals() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。