[英]casting or data type conversion of primitive values
a)如果我在Java中编写类似的内容
short a = 15
short b = 3;
short c = a * b;//will not compile
I know it will not compile because short values are automatically
promoted to int with the resulting value of type int so this will compile:
int d=a*b ; //compile
B)如果我做长数的乘法,例如:
long a=15;
long b=3;
long c=a*b;//will compile
int d=a*b;//will not compile
我假设长整数值不会因为int较小而被提升为int,而且在我编写时
长a = 15;
Java会将字面量解释为int,因为赋值中不存在带15的“ l”,如果Java将其解释为int,为什么它不能将解释为int数据类型的值相乘
int d=a*b;
为什么要给出错误?
在对长变量进行任何算术运算的情况下,java是否不考虑int值?
为什么Java允许long a = 15; 当我无法对它进行任何操作时,将其解释为int。
这是由于java向上转换而不向下转换的事实。 通过增加空间,它将转换成更大的东西,但是通过减少空间,您可能会丢失数据,因此java不会这样做。 这样会使您的小数(例如15)变成一个长整数,但是如果您从不填充长时间使用的空间,这样做会浪费内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.