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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。