Java中的编写器 hashCode() 方法与示例
Java中Writer类的hashCode()方法用来获取这个Writer实例的HashCode值。此方法不接受任何参数并返回所需的 int 值。
句法:
public int hashCode()
参数:此方法接受不接受任何参数。
返回值:此方法返回一个 int 值,它是 Writer 实例的 HashCode 值。
下面的方法说明了 hashCode() 方法的工作:
方案一:
// 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
方案二:
// 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