原始类型“short" - 在 Java 中进行转换 [英] Primitive type 'short' - casting in Java

查看:43
本文介绍了原始类型“short" - 在 Java 中进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Java 中原始类型 short 的问题.我正在使用 JDK 1.6.

如果我有以下几点:

short a = 2;短 b = 3;短 c = a + b;

编译器不想编译 - 它说它无法从 int 转换为 short"并建议我转换为 short,所以:

short c = (short) (a + b);

确实有效.但我的问题是为什么我需要投射?a 和 b 的值在 short 的范围内 - short 值的范围是 {-32,768, 32767}.当我要执行操作时,我也需要强制转换 -、*、/(其他的我没有检查过).

如果我对原始类型 int 做同样的事情,我不需要将 aa+bb 转换为 int.以下工作正常:

int aa = 2;国际BB = 3;int cc = aa +bb;

我在设计一个类时发现了这一点,我需要添加两个类型为 short 的变量,编译器希望我进行强制转换.如果我使用 int 类型的两个变量来执行此操作,则不需要强制转换.

一个小说明:同样的事情也会发生在原始类型 byte 上.所以,这是有效的:

byte a = 2;字节 b = 3;字节 c = (字节) (a + b);

但这不是:

byte a = 2;字节 b = 3;字节 c = a + b;

对于longfloatdoubleint,不需要强制转换.仅适用于 shortbyte 值.

解决方案

short C#(也适用于其他语言编译器,如 Java)

有一个预定义的从 short 到 int、long、float、double 或 decimal 的隐式转换.

您不能将较大存储大小的非文字数字类型隐式转换为 short(有关整数类型的存储大小,请参阅整数类型表).例如,考虑以下两个短变量 x 和 y:

short x = 5, y = 12;

以下赋值语句将产生编译错误,因为赋值运算符右侧的算术表达式默认计算为 int.

short z = x + y;//错误:没有从 int 到 short 的转换

要解决此问题,请使用演员表:

short z = (short)(x + y);//OK:显式转换

虽然可以使用以下语句,其中目标变量具有相同的存储大小或更大的存储大小:

int m = x + y;长 n = x + y;

<小时>

一个很好的后续问题是:

为什么赋值运算符右侧的算术表达式默认计算为 int"?

可以在以下位置找到第一个答案:

分类并正式验证整数常量折叠

<块引用>

Java 语言规范准确定义了整数的表示方式以及整数算术表达式的计算方式.这是 Java 的一个重要特性,因为这种编程语言旨在用于 Internet 上的分布式应用程序.Java 程序需要独立于执行它的目标机器产生相同的结果.

相比之下,C(以及大多数广泛使用的命令式和面向对象的编程语言)更加草率,并且留下了许多重要的特性.这种不准确语言背后的意图规格明确.相同的 C 程序应该在 16 位上运行,32 位,甚至 64 位架构,通过实例化整数运算具有内置在目标处理器中的算术运算的源程序.这会导致更高效的代码,因为它可以使用可用的机器直接操作.只要整数计算只处理如果数字足够小",就不会出现不一致的情况.

从这个意义上说,C 整数算术是一个未准确定义的占位符由编程语言规范,但只有通过确定目标机器才能完全实例化.

Java 精确定义了整数的表示方式以及整数运算的计算方式.

 Java 整数--------------------------签名 |未签名--------------------------长(64 位) |整数(32 位)|短(16 位) |字符(16 位)字节(8 位)|

<块引用>

Char 是唯一的无符号整数类型.它的值代表 Unicode 字符,从 u0000uffff,即从 0 到 216−1.

如果一个整数运算符有一个 long 类型的操作数,那么另一个操作数也被转换为 long 类型.否则,操作将在 int 类型的操作数上执行,如有必要,较短的操作数将转换为 int.转换规则完全指定.

[摘自计算机理论电子笔记82第2期(2003)
Blesner-Blech-COCV 2003:Sabine GLESNER、Jan Olaf BLECH,
Fakultät für Informatik,
卡尔斯鲁厄大学
德国卡尔斯鲁厄]

I have a question about the primitive type short in Java. I am using JDK 1.6.

If I have the following:

short a = 2;
short b = 3;
short c = a + b;

the compiler does not want to compile - it says that it "cannot convert from int to short" and suggests that I make a cast to short, so this:

short c = (short) (a + b);

really works. But my question is why do I need to cast? The values of a and b are in the range of short - the range of short values is {-32,768, 32767}. I also need to cast when I want to perform the operations -, *, / (I haven't checked for others).

If I do the same for primitive type int, I do not need to cast aa+bb to int. The following works fine:

int aa = 2;
int bb = 3;
int cc = aa +bb;

I discovered this while designing a class where I needed to add two variables of type short, and the compiler wanted me to make a cast. If I do this with two variables of type int, I don't need to cast.

A small remark: the same thing also happens with the primitive type byte. So, this works:

byte a = 2;
byte b = 3;
byte c = (byte) (a + b);

but this not:

byte a = 2;
byte b = 3;
byte c = a + b;

For long, float, double, and int, there is no need to cast. Only for short and byte values.

解决方案

As explained in short C# (but also for other language compilers as well, like Java)

There is a predefined implicit conversion from short to int, long, float, double, or decimal.

You cannot implicitly convert nonliteral numeric types of larger storage size to short (see Integral Types Table for the storage sizes of integral types). Consider, for example, the following two short variables x and y:

short x = 5, y = 12;

The following assignment statement will produce a compilation error, because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.

short z = x + y;   // Error: no conversion from int to short

To fix this problem, use a cast:

short z = (short)(x + y);   // OK: explicit conversion

It is possible though to use the following statements, where the destination variable has the same storage size or a larger storage size:

int m = x + y;
long n = x + y;


A good follow-up question is:

"why arithmetic expression on the right-hand side of the assignment operator evaluates to int by default" ?

A first answer can be found in:

Classifying and Formally Verifying Integer Constant Folding

The Java language specification defines exactly how integer numbers are represented and how integer arithmetic expressions are to be evaluated. This is an important property of Java as this programming language has been designed to be used in distributed applications on the Internet. A Java program is required to produce the same result independently of the target machine executing it.

In contrast, C (and the majority of widely-used imperative and object-oriented programming languages) is more sloppy and leaves many important characteristics open. The intention behind this inaccurate language specification is clear. The same C programs are supposed to run on a 16-bit, 32-bit, or even 64-bit architecture by instantiating the integer arithmetics of the source programs with the arithmetic operations built-in in the target processor. This leads to much more efficient code because it can use the available machine operations directly. As long as the integer computations deal only with numbers being "sufficiently small", no inconsistencies will arise.

In this sense, the C integer arithmetic is a placeholder which is not defined exactly by the programming language specification but is only completely instantiated by determining the target machine.

Java precisely defines how integers are represented and how integer arithmetic is to be computed.

      Java Integers
--------------------------
Signed         |  Unsigned
--------------------------
long  (64-bit) |
int   (32-bit) |
short (16-bit) |  char (16-bit)
byte  (8-bit)  |

Char is the only unsigned integer type. Its values represent Unicode characters, from u0000 to uffff, i.e. from 0 to 216−1.

If an integer operator has an operand of type long, then the other operand is also converted to type long. Otherwise the operation is performed on operands of type int, if necessary shorter operands are converted into int. The conversion rules are exactly specified.

[From Electronic Notes in Theoretical Computer Science 82 No. 2 (2003)
Blesner-Blech-COCV 2003: Sabine GLESNER, Jan Olaf BLECH,
Fakultät für Informatik,
Universität Karlsruhe
Karlsruhe, Germany]

这篇关于原始类型“short" - 在 Java 中进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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