双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

3.2 Primitive Types

Let us make a couple quick notes about a couple of the data types as shown in table 3.1 before we continue. The char type is a Unicode character in Java, rather than an ASCII character, as most people are used to seeing. That means that any character (regardless of language) can be represented with a char variable. That’s why a char variable requires 2 bytes of storage rather than the 1 byte that is required for all ASCII characters. Also, you’ll notice in the sample initialization of the float variable the extra ‘f’ character. This is required because, by default, the Java compiler views all decimal literals as doubles. In order to place a double into a float, you need to do a cast. I’ll look at the reason for this next.

Java中字符类型属于unicode编码,每个字符占据2字节的存储空间。

Table 3.1 Main primitive types in Java