您的位置:首页 > 编程语言 > Java开发

Java基础 - 数据类型

2006-11-23 18:12 681 查看
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:证明javabyte基本类型在运算前已经全部转换成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个字节,不过它的范围被限制在-128127之间。
浮点型
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:下面3char是相同的字符
char c = 0x10;
char c2 = '/u0010';
char c3 = 16;
Unicode
Latin-1 suffices for most Western European languages (with the notable exception of Greek), but it doesn't have anywhere near the number of characters required to represent Cyrillic, Greek, Arabic, Hebrew, or Devanagari, not to mention pictographic languages like Chinese and Japanese. Chinese alone has over 80,000 different characters. To handle these scripts and many others, the Unicode character set was invented. Unicode has space for over one million different possible characters. Only about 100,000 are used in practice, the rest being reserved for future expansion. Unicode can handle most of the world's living languages and a number of dead ones as well.
The first 256 characters of Unicode are identical to the characters of the Latin-1 character set. Thus 65 is ASCII A and Unicode A; 66 is ASCII B and Unicode B, and so on.
Unicode is only a character set. It is not a character encoding. That is, although Unicode specifies that the letter A has character code 65, it doesn't say whether the number 65 is written using one byte, two bytes, or four bytes, or whether the bytes used are written in big- or little-endian order. However, there are certain standard encodings of Unicode into bytes, the most common of which are UTF-8, UTF-16, and UTF-32.
UTF-32 is the most naïve encoding. It simply represents each character as a single 4-byte (32-bit) int.
UTF-16 represents most characters as a 2-byte, unsigned short. However, certain less common Chinese characters, musical and mathematical symbols, and characters from dead languages such as Linear B are represented in four bytes each. The Java virtual machine uses UTF-16 internally. In fact, a Java char is not really a Unicode character. Rather it is a UTF-16 code point, and sometimes two Java chars are required to make up one Unicode character.
Finally, UTF-8 is a relatively efficient encoding (especially when most of your text is ASCII) that uses one byte for each of the ASCII characters, two bytes for each character in many other alphabets, and three-to-four bytes for characters from Asian languages. Java's .class files use UTF-8 internally to store string literals.
布尔型
boolean

Java,布尔值和整数不能相互转换。

数字类型之间的转换
当使用二元操作符对两个值进行计算时(比如n + f,其中n是正整数, f是浮点值),在进行运算前,两个操作数都会转换成通用类型。
l 如果两个操作数中有一个是double类型的,则另一个将会转换成double类型。
l 否则,如果两个操作数中有一个是float类型的,另一个将会转换成float类型。
l 否则,如果两个操作数中有一个是long类型,另一个将会转换成long类型。
l 否则,两个操作数都将转换成int类型的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: