从int到short的可能有损转换 [英] possible lossy conversion from int to short

查看:1369
本文介绍了从int到short的可能有损转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将数组gx,数组arr定义为短类型。但是为什么左边的操作可能会以int类型结束,我必须把它简化为什么?编译器错误可能是从int到short的有损转换。

I have defined array gx, array arr to be short type. but why the operation at left may end up with int type and I must cast it into short? the compiler error is possible lossy conversion from int to short.

这是我的代码。

public PixImage sobelEdges() {

short gy=0;
for(int x=1;x<width-1;x++){
    for(int y=1;y<height-1;y++){
       // if(x=){
            for(int z=0;z<3;z++){
            gx[x][y][z]=arr[x-1][y-1][z]-arr[x+1][y-1][z]+2*arr[x-1][y][z]-2*arr[x+1][y][z]+arr[x-1][y+1][z]-arr[x+1][y+1][z];

            }
       // }    
    }     
}   
return this;
// Don't forget to use the method mag2gray() above to convert energies to
// pixel intensities.

}

这是因为如此叫unbox?所以这意味着我每次进行操作都需要进行投射?

Is that because the so called unbox? so that means every time I made a operation I need to cast?

推荐答案

这不是拆箱;这是二进制数字促销。 JLS第5.6.2节状态:

It's not unboxing; it's "binary numeric promotion". Section 5.6.2 of the JLS states:


当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示一个值可转换为数字类型,以下规则适用:

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order:


  1. 如果任何操作数属于引用类型,则为进行拆箱转换(第5.1.8节)。

  1. If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).

应用扩展基元转换(第5.1.2节)来转换由两者指定的操作数。遵循以下规则:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:


  • 如果任一操作数的类型为double,则另一个操作数转换为double。

  • If either operand is of type double, the other is converted to double.

否则,如果任一操作数的类型为float,则另一个操作数转换为float。

Otherwise, if either operand is of type float, the other is converted to float.

否则,如果操作数的类型为long,另一个转换为long。

Otherwise, if either operand is of type long, the other is converted to long.

否则,两个操作数都是转换为类型int



对某些运营商的操作数执行二进制数字促销:

Binary numeric promotion is performed on the operands of certain operators:


  • 乘法运算符*,/和%(§15.17)

  • The multiplicative operators *, /, and % (§15.17)

添加和数字类型的减法运算符+和 - (§15.18.2)

The addition and subtraction operators for numeric types + and - (§15.18.2)

数值比较运算符<,< =,>和> =(§ 15.20.1)

The numerical comparison operators <, <=, >, and >= (§15.20.1)

数字相等运算符==和!=(§15.21.1)

The numerical equality operators == and != (§15.21.1)

整数位运算符&,^和| (§15.22.1)

The integer bitwise operators &, ^, and | (§15.22.1)

在某些情况下,条件运算符? :(§15.25)

In certain cases, the conditional operator ? : (§15.25)

(强调我的)

当这些值被添加/相乘时,它们会在数学运算完成之前提升为 int 。最后,您可以在分配回数组之前转回 short

When those values are added/multiplied, they are promoted to int before the math is done. At the end, you can cast back to short before assigning back to the array.

gx[x][y][z] = (short) (arr[x-1][y-1][z]-arr[x+1][y-1][z]+2*arr[x-1][y][z]
    -2*arr[x+1][y][z]+arr[x-1][y+1][z]-arr[x+1][y+1][z]);

每次使用小于<$的原始数据类型时,都需要将其强制转换回来c $ c> int ,例如在你的示例中。

You will need to cast it back every time you operate with primitive data types that are smaller than int, such as in your short example.

这篇关于从int到short的可能有损转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆