Java char到字节转换 [英] Java char to byte casting
本文介绍了Java char到字节转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在测试char cast,我经历了这个:
I have been testing the char casting and I went through this:
public class Test {
public static void main(String a[]) {
final byte b1 = 1;
byte b2 = 1;
char c = 2;
c = b1; // 1- Working fine
c = b2; // 2 -Compilation error
}
}
任何人都可以解释为什么会这样当我在字节中添加最后一个时工作正常吗?
Can anyone explain why it's working fine in 1 when I added a final to the byte?
推荐答案
当变量是 final
,编译器自动内联其值为1.此值可表示为 char
,即:
When the variable is final
, the compiler automatically inlines its value which is 1. This value is representable as a char
, i.e.:
c = b1;
相当于
c = 1;
事实上,根据关于最终
变量的这一部分, b1
被视为常量:
In fact, according to this section on final
variables, b1
is treated as a constant:
基本类型或类型的变量
String
,即final
并使用编译时常量表达式(第15.28节)初始化,称为常量变量。
A variable of primitive type or type
String
, that isfinal
and initialized with a compile-time constant expression (§15.28), is called a constant variable.
这篇关于Java char到字节转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文