📜  Java中的字符.hashCode() 和示例

📅  最后修改于: 2022-05-13 01:55:26.390000             🧑  作者: Mango

Java中的字符.hashCode() 和示例

Java.lang。 字符.hashCode() 是Java中的内置方法,它返回此字符的哈希码。返回的哈希码等于调用 charValue() 的结果。

句法:

public int hashCode()

This function does not accepts any parameter.

返回值:此方法返回此字符的哈希码值。

下面的程序说明了Java.lang。字符.hashCode()函数:

程序 1

// Java program to demonstrate the
// function when the value passed in the parameter
// is a character 
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // parameter ch
        char ch = 'B';
        // assigns character values
        Character c = Character.valueOf(ch);
         
      
        // assign hashcodes of c1, c2 to i1, i2
        int i = c.hashCode();
          
        // prints the character values
        System.out.println("Hashcode of " + ch + " is " + i);
    }
}
输出:
Hashcode of B is 66

方案二

// Java program to demonstrate the
// function when the value passed in the parameter
// is a number 
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // parameter ch
        char ch = '6';
        // assigns character values
        Character c = Character.valueOf(ch);
         
      
        // assign hashcodes of ch 
        int i = c.hashCode();
          
        // prints the character values
        System.out.println("Hashcode of " + ch + " is " + i);
    }
}
输出:
Hashcode of 6 is 54