1
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
乙像這樣)如果我做長的數字,如一個乘法:
long a=15;
long b=3;
long c=a*b;//will compile
int d=a*b;//will not compile
我假定那長的值不會被提升爲int,因爲 int更小,而且當我寫時
long a = 15;
的Java會解釋文字爲int,因爲沒有「L」與15的分配,要是Java的解讀是,INT爲什麼不能乘其解釋爲整型數據類型
int d=a*b;
爲什麼要給出錯誤?
在長變量的任何算術運算的情況下,java是否沒有考慮int值?
爲什麼java允許long a = 15;當我不能對int做任何操作時將它解釋爲int。
感謝您的答覆。只需添加到上面我假定以下不應該工作:float num1 = 4,num2 = 5; float result = num1 + num2;但它並不像在上述問題 –
中添加兩個短數字那樣,所以在這裏將會丟失數據。 –