简体   繁体   English

Java类型转换示例

[英]Java type-casting examples

I have an exam soon and I want someone to check my explanations why some conversions are allowed by the compiler, and some aren't. 我很快就要考试了,我希望有人检查一下我的解释,为什么编译器允许进行某些转换,而有些则不允许。

byte--> short --> int --> long --> float --> double 字节->短->整数->长->浮点->双精度

So I can always cast (unexplicitly) from small to big ie from byte to long for example. 因此,我总是可以(不明确地)从小到大(例如,从字节到长)进行转换。 But to go in this direction <-- I have to explicitly typecast. 但是要朝这个方向前进,我必须明确地进行类型转换。 So far so good, but questions at the exam are still kinda tricky. 到目前为止还算不错,但是考试中的问题仍然有些棘手。

int value1 = (double) 10;

Not allowed. 不允许。 Eclipse says cannot convert from double to int. Eclipse说不能从double转换为int。 But isn't 10 an integer already?? 但是10已经不是整数了吗? I don't really get it. 我真的不明白。

byte value2 = (int) 120.0;

Allowed. 允许的。 So the 120.0 gets cut to 120 which is then an integer which is within the range of a byte. 因此120.0会减少到120,这是一个整数,在一个字节的范围内。 But for example the follwing 但例如以下

byte value3 = (int) 33500.0;

is not allowed. 不被允许。 Because it's not within the range of a byte? 因为它不在一个字节范围内?

double value4 = (int) 13.2;

Allowed. 允许的。 13.2 gets cut to 13 which is an integer. 13.2减为13,是整数。 You can always assign small to big. 您始终可以将大小分配为小。

int value = 10f;

not allowed. 不允许。 Because you cannot convert from float to int, but however 因为您不能从float转换为int,但是

int value = (int) 10f

is allowed, since now it's casted explicitly. 允许,因为现在已将其显式转换。

double someValue = (int) 12.3;

Allowed, I guess. 我想是允许的。 But this doesn't make a lot of sense, does it? 但这没有多大意义,不是吗? Since 12.3 is already a double. 由于12.3已经是两倍。

short v1 = (int) 42.0;

Allowed. 允许的。 But Why?? 但为什么?? I thought you cannot go in this <-- direction. 我以为你不能朝这个方向走。

This is described in JLS Sec 5.2 : JLS Sec 5.2中对此进行了描述:

Assignment contexts allow the use of one of the following: 分配上下文允许使用以下之一:

  • an identity conversion (§5.1.1) 身份转换(第5.1.1节)
  • a widening primitive conversion (§5.1.2) 不断扩大的原始转换(第5.1.2节)

... ...

In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: 此外,如果该表达式是类型为byte,short,char或int的常量表达式(第15.28节):

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. 如果变量的类型为byte,short或char,并且常量表达式的值可表示为变量的类型,则可以使用缩窄的原始转换。

Your examples can all be explained with these rules: 您的示例都可以通过以下规则进行解释:

 double value4 = (int) 13.2; 

(int) 13.2 is of type int , so a widening primitive conversion is applied. (int) 13.2的类型为int ,因此应用了扩展的原始转换。

 int value = (int) 10f; 

(int) 10f is of type int , so an identity conversion is applied. (int) 10f的类型为int ,因此应用了身份转换。

 double someValue = (int) 12.3; 

(int) 12.3 is of type int - its value is 12 - so a widening primitive conversion is applied. (int) 12.3的类型为int其值为12因此应用了扩展的原始转换。 The fact that 12.3 is a double is irrelevant here - you're not assigning 12.3 to the variable, you're assigning (int) 12.3 == 12 . 12.3是双精度的事实在这里是不相关的-您没有将12.3分配给变量,而是分配了(int) 12.3 == 12

 byte value2 = (int) 120.0; short v1 = (int) 42.0; 

These are allowed because the expressions are constants of int type, and their values can be represented in byte and short respectively, so the narrowing primitive conversion case applies. 允许使用这些表达式是因为表达式是int类型的常量,并且它们的值可以分别用byteshort表示,因此适用缩窄的原始转换情况。

 int value1 = (double) 10; int value = 10f; 

These isn't allowed because it would require a narrowing conversion from floating point types to int. 不允许使用这些方法,因为这将需要从浮点类型到int的缩小转换。 Only some constant integer values can be implicitly narrowed (not long ). 只能隐式地缩小一些常量整数值(不long )。

 byte value3 = (int) 33500.0; 

This isn't allowed, despite the expression being of type int , because (int) 33500.0 == 33500 is outside the range of byte . 尽管表达式的类型为int ,这还是不允许的,因为(int) 33500.0 == 33500byte范围之外。

There are two types of casting: 有两种类型的转换:

  1. Implicit Type Casting (widening conversion) 隐式类型转换(扩展转换)
  2. Explicit Type Casting (narrowing conversion) 显式类型转换(缩小转换)

The sequence you mentioned byte--> short --> int --> long --> float --> double is for Implicit Type Casting. 您提到的字节->短->整数->长->浮点->双重的顺序适用于隐式类型转换。

Example:
short v1 = 4;
int i = v1;
System.out.print(i);

Output: 4 输出4

The one in which we use (datatype) ahead of the value to change it to the other is known as Explicit Type Casting.With this we can assign larger data type values to the smaller ones. 我们在值前使用(数据类型)将其更改为另一个值的方法称为显式类型转换(Explicit Type Casting),这样我们就可以将较大的数据类型值分配给较小的值。

    Example:
    short v1 = (int)42.0;
    System.out.print(v1);

Output: 42 输出:42

Hope you got all your answers. 希望您能得到所有答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM