Why int can't be passed to wrapper type Byte constructor?
Let's take a look at the following code:
class Program {
public static void main (String[] args) {
byte a = 1;
Byte b = new Byte(primitiveByte);
Byte c = new Byte(1);
Byte d = new Byte((byte)1);
System.out.print(b.byteValue() + c.byteValue());
}
}
Why does the line "byte a = 1;"compile without error? and Why does the "Byte c = new Byte(1);"has a compile-time error?. Both of them use the integer literal value 1 within the range of byte. So why explicit casting "(byte)1"is needed?
The assignment conversion (such as "byte a = 1;") is different than the method invocation conversion (such as "Byte c = new Byte(1);"). They are two subsections in Java Language Specification 3rd Edition:
Assignment Conversion
Please read the above link for the detail about Assignment Conversion in Java. For our example:
byte a = 1;
The assignment will not generate the compiler error because at any time if an integral value is assigned to any of the primitive types (byte, char, short), and the right hand side value is within the range of the left hand side data type, it will not generate an error. Hence as 1 is within the range of byte (-128 to 127), it is not generating compiler error. For example:
byte a = 129; //Compile-time error
When you assign an intergal value to any primitive type variable, the compiler will do a range checking to see if this value is in the range of the left side data type. If it is NOT within the range of data type, a compile-time error occurs.
If the righ-hand of assignment is a variable or expression, can narrowing primitive conversions still work (5.1.3 Narrowing Primitive Conversions in Java Language Specification 3rd Edition)?
For example:
int i1=1;
final int i2 = 127;
final int i3 = 245;
byte b1 = i1; //Compile-time error, not final variable
byte b2 = i2; //OK, compile time constant
byte b3 = i3; //Compile-time error, over range
byte b4 = (i2 + 2); //OK, compile time constant expression
byte b5 = (i1 + 2); //Compile-time error, not final variable
Method Invocation Conversion
Please read the above link for the detail about the Method Invocation Conversion in Java. For our example:
Byte c = new Byte(1);
A compile-time error will occurs because you are passing an integer to the constructor of the Wrapper class Byte, the compiler will not do the implicit casting here.
The method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion. The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matching resolution process.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.