当前位置: 首页 > 文档资料 > 学习 Java 编程 >

int hashCode()

优质
小牛编辑
118浏览
2023-12-01

描述 (Description)

此方法返回此字符串的哈希码。 String对象的哈希码计算如下 -

s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1]

使用int算术,其中s [i]是字符串的第i个字符,n是字符串的长度,^表示取幂。 (空字符串的哈希值为零。)

语法 (Syntax)

以下是此方法的语法 -

public int hashCode()

参数 (Parameters)

这是参数的细节 -

  • 这是一个默认方法,不接受任何参数。

返回值 (Return Value)

  • 此方法返回此对象的哈希码值。

例子 (Example)

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("Welcome to iowiki.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}

这将产生以下结果 -

输出 (Output)

Hashcode for Str :1186874997