Java中的short和char类型 [英] short and char type in Java

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

问题描述

根据Java标准,short和char类型都使用2个字节,所以当我编码类似

according to the Java standard the short and char types both use 2 bytes so when i code something like

char ch = 'c';
short s = ch;

有一个错误说可能会失去精确度。
我在这里缺少什么。

There is an error saying "possible loss of precision". What am i missing here.

推荐答案

Char是无符号的,short是签名的。

Char is unsigned, short is signed.

因此,虽然它们都是2字节长,但它们将第16位用于不同目的。

So while they are both 2-byte long, they use the sixteenth bit for different purposes.

char类型的范围是0至2 ^ 16 - 1(0至65535)。

The range of the char type is 0 to 2^16 - 1 (0 to 65535).

短程为-2 ^ 15至2 ^ 15 - 1(-32,768至32,767)。

The short range is -2^15 to 2^15 - 1 (−32,768 to 32,767).

这篇关于Java中的short和char类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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