为什么int类型方法允许字符类型作为返回值? [英] Why int type method is allowing character type as return value?
问题描述
这是我的代码但是我没有得到如何 int getValue()
方法接受 char
键入为返回值。它是如何工作的?我可以解释一下这个方法是如何工作的吗?
Here is my code but I am not getting how int getValue()
method accepting char
type as return value. How is it working? could any body please explain me how this method is working?
public class CharToInteger {
private static Scanner input;
public static int getValue(char character){
return character;
}
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Enter a character to get value of it:");
String inputString=(String) input.next();
char inputCharacter=inputString.charAt(0);
System.out.println(getValue(inputCharacter));
}
}
提前致谢:)
推荐答案
好的,首先要做的事情是:
OK, so first things first:
这是一个加宽的原语类型转换,所以这是合法的。您可以:
This is a widening primitive type conversion, so this is legal. You can:
int foo() { return 'a' /* character constant */ };
long foo() { return 3; /* int constant */ }
但你不能这样做:
char foo() { return 1; /* int constant */ }
int foo() { return 1L; /* long constant */ }
第二:它返回的不是全部的ASCII代码。 Java做Unicode。
Second: what it returns is NOT THE ASCII CODE AT ALL. Java does Unicode.
恰好在创建Java时,Unicode只定义了符合16位的代码点;因此 char
被创建为一个2字节的无符号基本类型(它是Java中唯一的无符号基本类型),与当时称为UCS-2的字符编码匹配(1到1) 1编码和代码点之间的映射。)
It just happens that when Java was created, Unicode only defined code points fitting 16 bits; hence char
was created as a 2 byte, unsigned primitive type (it is the only unsigned primitive type in Java) matching the then-called UCS-2 character coding (a 1 to 1 mapping between the coding and code points).
然而,之后,Unicode变为宽,并且出现了BMP之外的代码点(即,大于U + FFFF);从那时起,UCS-2变成了UTF-16,并且BMP之外的代码点需要两个 char
s用于一个代码点(一个主要的代理和一个尾随的代理;在以前的Unicode版本中) ,在Java API中,那些被称为resp。高和低代理)。因此, char
现在是一个UTF-16代码单元。
However, afterwards Unicode went "wide" and code points outside the BMP (ie, greater than U+FFFF) appeared; since then UCS-2 became UTF-16, and code points outside the BMP require two char
s for one code point (a leading surrogate and a trailing surrogate; in previous Unicode versions, and in the Java API, those were called resp. high and low surrogate). A char
is therefore now a UTF-16 code unit.
然而,对于代码来说仍然如此BMP中的点, char
值与代码点完全匹配。
It is still true, however, that for code points in the BMP, the char
value exactly matches the code point.
现在,为了修复你的程序以准确显示每个可能条目的字符值,即代码点,你会这样做(Java 8):
Now, in order to "fix" your program to accurately display the "character value", ie the code point, for each possible entry, you would do that (Java 8):
public static void main(String[] args) {
final Scanner input = new Scanner(System.in);
System.out.println("Enter a character to get value of it:");
String inputString = input.next();
// Print -1 on an empty input
final OptionalInt codepoint = inputString.codePoints().findFirst();
System.out.println(codepoint.isPresent() ? codepoint.get() : -1);
}
这也将处理BMP之外的代码点。
This will also handle code points outside the BMP.
这篇关于为什么int类型方法允许字符类型作为返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!