- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java - Character hashCode() 方法
描述
Java 的 Character hashCode() 方法返回给定 Character 的哈希码。哈希码是分配给 Java 中每个对象的唯一原始整数。
注意 -
Character 类中的此方法覆盖了 Object 类中的 hashCode() 方法。
调用此方法时获得的结果与调用 charValue() 方法时获得的结果相同。
此方法有两种多态形式。一种不带参数,另一种以 char 值作为参数。
语法
以下是 Java Character hashCode() 方法的语法
public int hashCode() or, public static int hashCode(char value)
参数
value - 必须返回其哈希码的 char 值。
返回值
此方法返回 Character 的哈希码值。
获取 Character 对象的哈希码示例
以下示例显示了 Java Character hashCode() 方法的使用。在此示例中,我们创建了两个 Character 变量并为其分配了两个 Character 对象。使用 hashCode() 方法,打印每个 Character 对象的哈希码。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create 2 Character objects c1, c2 Character c1, c2; // assign values to c1, c2 c1 = new Character('z'); c2 = new Character('Z'); // create 2 int primitives i1, i2 int i1, i2; // assign hashcodes of c1, c2 to i1, i2 i1 = c1.hashCode(); i2 = c2.hashCode(); String str1 = "Hashcode of " + c1 + " is " + i1; String str2 = "Hashcode of " + c2 + " is " + i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } }
输出
让我们编译并运行上述程序,这将产生以下结果 -
Hashcode of z is 122 Hashcode of Z is 90
获取各种类型 Character 值的哈希码示例
下面可以看到此方法的另一个示例,它采用各种类型的字符值 -
package com.tutorialspoint; public class CharacterDemo { public static void main(String args[]) { Character c1 = new Character('A'); Character c2 = new Character('a'); Character c3 = new Character('7'); Character c4 = new Character('@'); int res = c1.hashCode(); System.out.println("The hash code of given character is " + res); res = c2.hashCode(); System.out.println("The hash code of given character is " + res); res = c3.hashCode(); System.out.println("The hash code of given character is " + res); res = c4.hashCode(); System.out.println("The hash code of given character is " + res); } }
输出
编译并运行上面给出的程序,其输出显示如下 -
The hash code of given character is 65 The hash code of given character is 97 The hash code of given character is 55 The hash code of given character is 64
获取 Character 对象的哈希码示例
以下示例显示了 Java Character hashCode(char ch) 方法的使用。在此示例中,我们创建了四个 Character 变量并为其分配了 Character 对象。使用 hashCode() 方法,打印每个 Character 对象的哈希码。
package com.tutorialspoint; public class Demo { public static void main (String args[]) { Character c1 = new Character('?'); int hc1 = Character.hashCode(c1); System.out.println("The hash code of Character is " + hc1); Character c2 = new Character('x'); int hc2 = Character.hashCode(c2); System.out.println("The hash code of Character is " + hc2); Character c3 = new Character('6'); int hc3 = Character.hashCode(c3); System.out.println("The hash code of Character is " + hc3); Character c4 = new Character('+'); int hc4 = Character.hashCode(c4); System.out.println("The hash code of Character is " + hc4); } }
输出
编译并运行上面的代码,我们将获得以下输出 -
The hash code of Character is 63 The hash code of Character is 120 The hash code of Character is 54 The hash code of Character is 43
java_lang_character.htm
广告