java中的类型转换和最终变量有什么区别? [英] What is the difference between type casting and final variable in java?
问题描述
我被教过这个:
byte-> short-> int-> long-> float-> double
char-> int-> long-> float-> double
class HelloA {
public static void main(String args []){
final int a = 5; //更高的数据类型变量
byte b = a; //分配较高数据类型值的较低数据类型变量
变量
System.out.println(b);
}
}
输出:5
class Hello {
public static void main(String args []){
final byte b = 65;
char c = b; //这违反了byte-> short-> int
System.out.println(c);
}
}
输出:A
class Hello {
public static void main(String args []){
final char c ='A';
byte b = c; //这违反了char-> int
System.out.println(b);
}
}
输出:65
有人可以详细解释一下吗?
我尝试了什么:
我已经尝试过上述程序。
I have been taught this:
byte->short->int->long->float->double
char->int->long->float->double
class HelloA{
public static void main(String args[]){
final int a=5; //Higher data type variable
byte b=a; //Lower data type variable assigned a value of higher data type
variable
System.out.println(b);
}
}
Output: 5
class Hello{
public static void main(String args[]){
final byte b=65;
char c=b; //This violates byte->short->int
System.out.println(c);
}
}
Output: A
class Hello{
public static void main(String args[]){
final char c='A';
byte b=c; //This violates char->int
System.out.println(b);
}
}
Output: 65
Can someone please explain elaborately?
What I have tried:
I have tried the above programs.
推荐答案
这不是特定于Java,它是计算机存储值的方式,以及程序如何解释它们。
字符A
是ASCII整理表中的第65个字符。因此,如果将字符变量复制(或引用)到一个字节,编译器会将字节值视为数字,即65.如果将数字65复制到字符变量编译器,则反之亦然将其视为一个字符,在本例中为A. Google用于ASCII整理顺序,您将看到所有字符的数值。
This is not specific to Java, it is how computers store values, and how programs interpret them.
The characterA
is the 65th character in the ASCII collate table. So if you copy (or reference) a character variable to a byte the compiler treats the byte value as a number, which is 65. And vice versa if you copy the number 65 to a character variable the compiler treats it as a character, which in this case is A. Google for "ASCII collate sequence" and you will see the numeric values of all characters.
这篇关于java中的类型转换和最终变量有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!