Java Writer hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 21:17:27 作者:

Java Writer hashCode()方法及示例

Java中Writer类的hashCode()方法是用来获取这个Writer实例的HashCode值。这个方法不接受任何参数,并返回所需的int值。

语法。

公共int hashCode()

参数。此方法不接受任何参数。

返回值。该方法返回一个int值,即Writer实例的HashCode值。

下面的方法说明了hashCode()方法的工作。

程序 1:

// Java program to demonstrate// Writer hashCode() method  import java.io.*;  class GFG {    public static void main(String[] args)    {          try {              // Create a Writer instance            Writer writer                = new PrintWriter(System.out);              // Get the String            // to be written in the stream            String string = "GeeksForGeeks";              // Write the string            // to this writer using write() method            writer.write(string);              // Get the HashCode value using hashCode()            System.out.println("HashCode value: "                               + writer.hashCode());        }        catch (Exception e) {            System.out.println(e);        }    }}

输出:

HashCode value: 589431969

程序2。

// Java program to demonstrate// Writer hashCode() method  import java.io.*;  class GFG {    public static void main(String[] args)    {          try {              // Create a Writer instance            Writer writer                = new PrintWriter(System.out);              // Get the String            // to be written in the stream            String string = "GFG";              // Write the string            // to this writer using write() method            writer.write(string);              // Get the HashCode value using hashCode()            System.out.println("HashCode value: "                               + writer.hashCode());        }        catch (Exception e) {            System.out.println(e);        }    }}

输出:

HashCode value: 589431969

相关推荐