除了处理外,Short和Character有什么区别? [英] What is the difference between Short and Character apart from processing?
问题描述
据我所知
- 按字节",看起来好像一样(它们都长2个字节); 但是,
-
Character
对其有更多处理(静态.isLetter()
方法及其他,等等).
- "bytewise", it looks like they are the same (they are both 2 bytes long);
Character
, however, has more processing to it (static.isLetter()
method and others, etc).
虽然我的问题听起来有些愚蠢,但它们是:
While my questions may sound dumb, here they are:
- 除非我的第一个假设是错误的,为什么为什么会有原始类型
char
和short
,因为它们具有相同的内部长度",并且总之就没有未签名的原始类型Java中的类型? -
Short
是final
,如果不是,Character
是否可以扩展Short
?
- unless my first assumption is wrong, why are there primitive types
char
andshort
since they have the same "internal length` and, anyway, there are no unsigned primitive types in Java? Short
isfinal
, if it weren't, couldCharacter
have extendedShort
?
编辑:给出了答案,但我错了:Java中有 个未签名的原始类型,即... char
.
EDIT: answer given and I was wrong: there is one unsigned primitive type in Java and that is... char
.
编辑2 :@PatriciaShanahan还提到,在算术操作中, char
的行为类似于无符号的16位整数,就像>短
.并且这包括左移,即符号位 随身携带,就像 short
一样.
EDIT 2: @PatriciaShanahan also mentions that in arithmetic operations, a char
behaves like an unsigned 16bit integer, just like a short
. And this includes left shifts, that is, the sign bit is carried along, just like for short
.
推荐答案
本质区别是 short
是带符号的, char
是未签名的.
The essential difference is that short
is signed, char
is unsigned.
public class CharVsShort {
public static void main(String[] args) throws Exception {
short ffShort = (short)0xFFFF;
char ffChar = (char)0xFFFF;
System.out.println("all-1s-short = " + (int)ffShort);
System.out.println("all-1s-char = " + (int)ffChar);
}
}
打印
all-1s-short = -1
all-1s-char = 65535
整数类型为
byte
,short
,int
和long
,其值均为8位,分别为16位,32位和64位带符号的两个补码整数和char
,其值为16位 unsigned >代表UTF-16代码单位的整数
The integral types are
byte
,short
,int
, andlong
, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, andchar
, whose values are 16-bit unsigned integers representing UTF-16 code units
(我的粗体).还将类型的范围明确指定为
(my bold). It also gives the types' ranges explicitly as
-
byte
,从-128到127,包括 -
short
,从-32768到32767,包括 -
int
,从-2147483648到2147483647(含) -
long
,从-9223372036854775808到9223372036854775807,包括 -
char
,从'\ u0000'到'\ uffff'(包括0到65535)
byte
, from -128 to 127, inclusiveshort
, from -32768 to 32767, inclusiveint
, from -2147483648 to 2147483647, inclusivelong
, from -9223372036854775808 to 9223372036854775807, inclusivechar
, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535
这篇关于除了处理外,Short和Character有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!