关于在Java中不同数据类型的整数或实数的类型转换的问题。

huangapple go评论52阅读模式
英文:

Question about type conversion of different data types in integer or real number in Java

问题

I'm learning Java with Eclipse, but I have a question about data types.

在使用 Eclipse 学习 Java 时,我有一个关于数据类型的问题。

Among the error messages of the byte type, there is an error saying 'Type mismatch: cannot convert from int to byte', and to assign an integer to the long type, you must add 'L' to the end of the number. If you want to use a float type rather than a double type even for real numbers, you must add 'F' to the end of the decimal.

在 byte 类型的错误消息中,有一条错误消息说“类型不匹配:无法从 int 转换为 byte”,而要将整数分配给 long 类型,必须在数字末尾添加 'L'。如果您想在处理实数时使用 float 类型而不是 double 类型,您必须在小数末尾添加 'F'。

Do integers and real numbers check the data type of a variable and keep it int or change to another integer data type such as long after it is assigned as int and double type regardless of the size of the value and the data type specified before the variable name?

整数和实数是否会检查变量的数据类型,保持为 int 还是在分配为 int 和 double 类型后根据值的大小和变量名称之前指定的数据类型更改为其他整数数据类型,不受限于变量名称前指定的数据类型?

If so, if you use byte or long instead of int when writing an integer variable, the wasteful action of converting the type once occurs. Is that a very small action that can be ignored when creating a program?

如果是这样,如果在编写整数变量时使用 byte 或 long 代替 int,那么会发生一次类型转换的浪费操作。这是一个非常小的操作,可以在创建程序时忽略吗?

I wonder if I should always keep this in mind when creating integer and float variables.

我想知道在创建整数和浮点变量时是否应该时刻记住这一点。

英文:

I'm learning Java with Eclipse, but I have a question about data types.

Among the error messages of the byte type, there is an error saying 'Type mismatch: cannot convert from int to byte', and to assign an integer to the long type, you must add 'L' to the end of the number. If you want to use a float type rather than a double type even for real numbers, you must add 'F' to the end of the decimal.

Do integers and real numbers check the data type of a variable and keep it int or change to another integer data type such as long after it is assigned as int and double type regardless of the size of the value and the data type specified before the variable name?

If so, if you use byte or long instead of int when writing an integer variable, the wasteful action of converting the type once occurs. Is that a very small action that can be ignored when creating a program?

I wonder if I should always keep this in mind when creating integer and float variables.

答案1

得分: 0

代码示例中是否像这样输入值?

byte  bVal = 10;
short sVal = 10;
int   iVal = 10;
long  lVal = 10;

然后就没有“转换损失”了,您只是告诉编译器如何处理您输入的值,并且您确切地知道您正在使用的整数大小(8、16、32或64位)。对于double和float也是一样的。

编译器在编译时生成了正确的字节码。没有进行转换。

更多信息可以在Java语言规范中找到。

英文:

Are you entering the values in out code like this?

byte  bVal = 10;
short sVal = 10;
int   iVal = 10;
long  lVal = 10;

Then there is no "conversion loss", you are just telling the compiler how to treat the values you entered, and that you really know what integer size you are using (8, 16, 32 or 64 bits). The same applies to double and float.

The compiler generates the correct byte code during the compile. No conversion happens.

More information can be found in the Java Languange Specification.

huangapple
  • 本文由 发表于 2023年2月14日 01:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定