需要在Byte和Short中进行铸造

raj*_*eev 1 java casting

Byte byte1=new Byte((byte) 20);
Short short1=new Short((short) 20);
Run Code Online (Sandbox Code Playgroud)

为什么我必须在Byte和Short中使用强制转换操作符,但我没有在其他DataType中使用强制转换操作符

Integer integer=new Integer(20);
Long long1=new Long(20);
Double double1=new Double(20);
Float float1=new Float(20);
Run Code Online (Sandbox Code Playgroud)

ars*_*jii 9

这是因为第二个片段导致根据JLS§5.1.2扩展原始转换:

对原始类型的19个特定转换称为扩展原始转换:

  • byte对short,int,long,float,或者double

  • short对int,long,float,或者double

  • char对int,long,float,或者double

  • int到long,float或double

  • long到float或double

  • float 至 double

而第一个没有; 请注意,没有转换int为short或byte.