原始类型“short” - 在Java中投射 [英] Primitive type 'short' - casting in Java

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

问题描述

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



如果我有以下:

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

编译器不想编译 - 它说它无法从int转换为short建议我使用 short 进行投放,因此:

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

真的有效。但我的问题是为什么我需要施法? a和b的值在 short 的范围内 - 短值的范围为{-32,768,32767}。
当我要执行操作时,我还需要转换 - ,*,/(我没有检查其他人)。



对于原始类型 int ,我不需要将aa + bb转换为 int 。以下工作正常:

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



我在设计一个类时发现了这个问题,我需要添加两个类型为short的变量,而编译器想让我做一个演员。如果我这样做两个变量类型 int ,我不需要强制转换。



非常感谢您提前。



一个小小的注释:同样的事情也发生在原始类型 byte 。所以,这工作:

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

但不是:

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

对于 long float double int 仅适用于 short byte 值。

解决方案

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





您不能将更大存储大小的非数值类型隐式转换为短整型(请参阅Integral类型为整型存储大小的表)。例如,考虑以下两个短变量x和y:

  short x = 5,y = 12; 

以下赋值语句会产生编译错误

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

要解决这个问题, p>

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

有可能使用以下语句,目标变量相同的存储大小或更大的存储大小:

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






一个好的跟进问题是: p>

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



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



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


Java语言规范定义了整数以及如何评估整数算术表达式。这是Java的一个重要属性,因为此编程语言已设计为在Internet上的分布式应用程序中使用。 需要Java程序以独立于执行它的目标机器生成相同的结果。



相比之下,C(以及广泛使用的命令式和
面向对象编程语言)更加粗糙,并留下许多重要的特征。这种不准确的语言背后的意图
规范是清楚的。相同的C程序应该在16位,
32位或甚至64位架构上运行,通过实例化
的整数算术程序,源程序具有内置的算术运算目标处理器。这导致更多的高效代码,因为它可以直接使用可用的
机器操作。只要整数计算只处理
,而数字足够小,则不会出现不一致。



在这个意义上,C整数算术是一个占位符,它不是由编程语言规范完全定义的
,而是仅通过确定目标机器完全实例化。



Java精确定义如何表示整数以及如何计算整数运算。




  Java整数
------------ --------------
签名|未签名
--------------------------
long(64位)|
int(32位)|
short(16位)| char(16位)
字节(8位)|




Char是唯一的无符号整数类型。其值表示Unicode字符,从 \\\ \\\￿ ,即从0到2 16 -1。



如果整数运算符具有类型为long的操作数,则另一个操作数也将转换为类型long。否则,对int类型的操作数执行操作,如果必要,将较短的操作数转换为int


[电子笔记在理论计算机科学82第2期(2003年)

Blesner-Blech-COCV 2003: Sabine GLESNER , Jan Olaf BLECH,

FakultätfürInformatik,

Karlsruhe大学

Karlsruhe,德国]


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.

Thank you very much in advance.

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天全站免登陆