- 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 compareTo() 方法
描述
Java 的 Character compareTo() 方法用于数值比较两个 Character 对象。
Java Character 类中的这个内置方法接受一个 Character 对象作为参数,并被调用到另一个需要比较的 Character 对象上。
如果参数 Character 大于当前 Character,则该方法将产生一个负值;但如果参数 Character 小于当前 Character,则该方法返回一个正值。如果两个对象具有相同的引用,则产生零。
注意 - 这严格是一个数值比较;它不依赖于区域设置。
语法
以下是 Java Character compareTo() 方法的语法
public int compareTo(Character anotherCharacter)
参数
anotherCharacter − 要比较的 Character。
返回值
如果参数 Character 等于当前 Character,则此方法返回 0 值;如果当前 Character 数值小于 Character 参数,则返回负值;如果当前 Character 数值大于 Character 参数,则返回正值。
比较两个 Character 对象示例
以下示例演示了 Java Character compareTo() 方法的用法。我们创建了两个 Character 变量并为其分配了 Character 对象。现在使用 compare() 方法,我们对它们进行了比较,并打印结果。
以下示例演示了 Java Character compareTo() 方法的用法。
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('a'); c2 = new Character('b'); // create an int type int res; // compare c1 with c2 and assign result to res res = c1.compareTo(c2); String str1 = "Both values are equal "; String str2 = "First character is numerically greater"; String str3 = "Second character is numerically greater"; if( res == 0 ) { System.out.println( str1 ); } else if( res > 0 ) { System.out.println( str2 ); } else if( res < 0 ) { System.out.println( str3 ); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Second character is numerically greater
比较包含数字的两个 Character 对象示例
在这个例子中,让我们用数字作为 char 值来实例化 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('9'); c2 = new Character('1'); int result; // compare c1 with c2 and assign to result result = c1.compareTo(c2); if( result == 0 ) { System.out.println("Both values are equal "); } else if( result > 0 ) { System.out.println("First character is numerically greater"); } else if( result < 0 ) { System.out.println("Second character is numerically greater"); } } }
输出
一旦我们编译并运行上面的代码,该方法就会比较两个输入的数字字符,并打印输出为:
First character is numerically greater
比较包含符号的两个 Character 对象示例
下面的例子将符号作为字符对象,并比较这些对象的 Unicode 代码点以返回数值上更大的符号。
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('*'); c2 = new Character('/'); int result; // compare c1 with c2 and assign to result result = c1.compareTo(c2); if( result == 0 ) { System.out.println("Both values are equal "); } else if( result > 0 ) { System.out.println("First character is numerically greater"); } else if( result < 0 ) { System.out.println("Second character is numerically greater"); } } }
输出
编译后,执行代码以获得以下输出:
Second character is numerically greater
使用 compareTo 方法比较两个 char 示例
我们必须理解,Java Character compareTo() 方法只比较 Character 对象,这与 char 数据类型完全不同。但是,如果给程序的输入是一个 char 变量,则会提示编译时错误。
public class CharacterDemo { public static void main(String[] args) { char c1, c2; c1 = 'A'; c2 = 'B'; int result; result = c1.compareTo(c2); if( result == 0 ) { System.out.println("Both values are equal "); } else if( result > 0 ) { System.out.println("First character is numerically greater"); } else if( result < 0 ) { System.out.println("Second character is numerically greater"); } } }
编译时错误
执行上述代码将导致编译时错误,因为 char 变量不能引用方法。只有使用该方法所在的类的对象才有可能。
CharacterDemo.java:9: error: char cannot be dereferenced result = c1.compareTo(c2); ^ 1 error