Java Writer hashCode()方法及示例

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程