需要在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个特定转换称为扩展原始转换:

  • byteshort,int,long,float,或者double

  • shortint,long,float,或者double

  • charint,long,float,或者double

  • intlong,floatdouble

  • longfloatdouble

  • floatdouble

而第一个没有; 请注意,没有转换intshortbyte.