Java的数据类型
Java是一门强类型语言。也就是说,每个变量都必须声明类型。Java共有八种基本类型:
四种是整型,两种浮点数字型,一种字符型char.,用于真假的布尔类型(boolean)。
参考书籍:<<Java I/O>>2rd Edition
<<java2核心技术卷一>>
整型
int short long byte
The fundamental integer data type in Java is the int, a 4-byte, big-endian, two's complement integer.
byte是整型。
长整型后缀是L.
While the difference between an 8-bit byte and a 32-bit int is insignificant for a single number, it can be very significant when several thousand to several million numbers are read. In fact, a single byte still takes up four bytes of space inside the Java virtual machine, but a byte array occupies only the amount of space it actually needs. The virtual machine includes special instructions for operating on byte arrays but does not include any instructions for operating on single bytes. They're just promoted to ints. |
PS:证明java的byte基本类型在运算前已经全部转换成int类型的好例子: byte b1 = 22; byte b2 = 23; byte b3 = b1 + b2; 上面发生一个编译时错误 Error: Incompatible type for declaration. Explicit cast needed to convert int to short. |
Java has no short or byte literals. When you write the literal 42 or 24000, the compiler always reads it as an int, never as a byte or a short, even when used in the right-hand side of an assignment statement to a byte or short, like this: PS:当你写byte b = 42;对于编译器来讲,b还是4个字节,不过它的范围被限制在-128到127之间。 |
浮点型
float double
表示float类型数据时要在后面添加后缀F.
没有后缀总被认为是double类型。
所有浮点计算都遵从IEEE754规范。有三种特殊的浮点值。
字符型
Char
In Java, a char is a 2-byte, unsigned integerthe only unsigned type in Java.
Unicode具有从0到65536之间的编码.
前缀/u表示是Unicode值。/u2122是商标符号™.
PS:下面3个char是相同的字符 char c = 0x10; char c2 = '/u0010'; char c3 = 16; |
布尔型
boolean
Java中,布尔值和整数不能相互转换。
数字类型之间的转换
当使用二元操作符对两个值进行计算时(比如n + f,其中n是正整数, f是浮点值),在进行运算前,两个操作数都会转换成通用类型。 l 如果两个操作数中有一个是double类型的,则另一个将会转换成double类型。 l 否则,如果两个操作数中有一个是float类型的,另一个将会转换成float类型。 l 否则,如果两个操作数中有一个是long类型,另一个将会转换成long类型。 l 否则,两个操作数都将转换成int类型的。 |