java Java中字符和整数的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5165895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:46:41  来源:igfitidea点击:

Conversion between character and int in Java

java

提问by shreyasva

You cannot convert from int to char, so this would be illegal int i = 88; char c = i;,

你不能从 int 转换为 char,所以这将是非法的 int i = 88; char c = i;

However this is allowed char c = 88;.

然而这是允许的char c = 88;

Isn't a plain number and intliteral? How is this allowed?

不是一个普通的数字和int文字吗?这是怎么允许的?

回答by Jon Skeet

charis effectively an unsigned 16-bit integer type in Java.

char实际上是 Java 中的无符号 16 位整数类型。

Like other integer types, you can perform an assignment conversion from an integer constant to any integer type so long as it's in the appropriate range. That's why

与其他整数类型一样,您可以执行从整数常量到任何整数类型的赋值转换,只要它在适当的范围内即可。这就是为什么

byte b = 10;

works too.

也有效。

From the JLS, section 5.2:

来自 JLS,第 5.2 节

In addition, if the expression is a constant expression (§15.28) of type byte, short, char or int :

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
  • A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
    • Byte and the value of the constant expression is representable in the type byte.
    • Short and the value of the constant expression is representable in the type short.
    • Character and the value of the constant expression is representable in the type char.

此外,如果表达式是 byte、short、char 或 int 类型的常量表达式(第 15.28 节):

  • 如果变量的类型是 byte、short 或 char,并且常量表达式的值可以在变量的类型中表示,则可以使用缩小原语转换。
  • 如果变量的类型是:
    • 字节和常量表达式的值可以在类型字节中表示。
    • Short 和常量表达式的值可以在类型 short 中表示。
    • 字符和常量表达式的值可以在类型 char 中表示。

回答by Michael Borgwardt

Actually, converting from intto charis legal, it just requires an explicit cast because it can potentially lose data:

实际上,从intto转换char是合法的,它只需要显式转换,因为它可能会丢失数据:

int i = 88; 
char c = (char) i;

However, with the literal, the compiler knowswhether it will fit into a charwithout losing data and only complains when you use a literal that is too big to fit into a char:

但是,对于文字,编译器知道它是否适合 achar而不会丢失数据,并且只有在您使用太大而无法放入 a 的文字时才会抱怨char

char c = 70000; // compiler error

回答by vinod bazari

Its because the literals for integer or smaller than int as byte ,short and char is int. Understand the following in this way.

这是因为整数或小于 int 作为 byte ,short 和 char 的文字是int。通过这种方式理解以下内容。

code:

代码:

  byte a = 10;//compile fine
  byte b= 11;//compile fine
  byte c = a+b;//compiler error[says that result of **a+b** is **int**]

the same happens for any mathematical operations as of 'Divide', 'multiply', and other arithmetic operation. so cast the result to get the literal in desired data type

对于任何数学运算,如“除法”、“乘法”和其他算术运算,都会发生同样的情况。所以转换结果以获得所需数据类型的文字

byte c = (byte)(a+b);

So that the same reason why the value int need to have primitive cast to change the value in char. Hope this make some sense.

因此,为什么值 int 需要进行原始类型转换以更改 char 中的值的原因相同。希望这有点道理。