For what I know:
Character, however, has more processing to it (static .isLetter() method and others, etc).While my questions may sound dumb, here they are:
char and short since they have the same "internal length` and, anyway, there are no unsigned primitive types in Java?Short is final, if it weren't, could Character have extended Short?EDIT: answer given and I was wrong: there is one unsigned primitive type in Java and that is... char.
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.
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);
}
}
prints
all-1s-short = -1
all-1s-char = 65535
The Java Language Specification section 4.2 states that
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, from -128 to 127, inclusive short, from -32768 to 32767, inclusive int, from -2147483648 to 2147483647, inclusive long, from -9223372036854775808 to 9223372036854775807, inclusive char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With