[TOC]
-
原始数据类型和默认值(Primitive Data Types)
数据类型 位数(bit) 大小 默认值 byte 8 -128~127(inclusive) 0 short 16 -32,768~32,767 (inclusive) 0 int 32 -2^31~2^31-1 0 long 64 -2^63~2^63-1 0L float 32 0.0f double 64 0.0d boolean false char 16 0~65,535(inclusive) '\u0000' -
整型(Integer Literals)
An integer literal is of type
long
if it ends with the letterL
orl
; otherwise it is of typeint
. It is recommended that you use the upper case letterL
because the lower case letterl
is hard to distinguish from the digit1
.-
如果结尾是L或者是l,则是long类型,否则是int。
-
推荐使用大写字母L,因为和1很难区分开来。
-
-
(浮点类型)Floating-Point Literals
A floating-point literal is of type
float
if it ends with the letterF
orf
; otherwise its type isdouble
and it can optionally end with the letterD
ord
.- 如果结尾是F或者f,则是浮点类型,否则是double类型
-
(字符和字符串)Character and String Literals
Literals of types
char
andString
may contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a "Unicode escape" such as'\u0108'
(capital C with circumflex), or"S\u00ED Se\u00F1or"
(Sí Señor in Spanish). Always use 'single quotes' forchar
literals and "double quotes" forString
literals. Unicode escape sequences may be used elsewhere in a program (such as in field names, for example), not just inchar
orString
literals.The Java programming language also supports a few special escape sequences for
char
andString
literals:\b
(backspace),\t
(tab),\n
(line feed),\f
(form feed),\r
(carriage return),\"
(double quote),\'
(single quote), and\\
(backslash).There's also a special
null
literal that can be used as a value for any reference type.null
may be assigned to any variable, except variables of primitive types. There's little you can do with anull
value beyond testing for its presence. Therefore,null
is often used in programs as a marker to indicate that some object is unavailable.Finally, there's also a special kind of literal called a class literal, formed by taking a type name and appending "
.class"
; for example,String.class
. This refers to the object (of typeClass
) that represents the type itself.- 单引号用char,双引号用String
- 字符转义,转义序列:\ b(退格),\ t(制表符),\ n(换行),\ f(换页),\ r(回车), \“(双引号),'(单引号)和\(反斜杠)。
问题
-
为什么最大值是2位次方-1,而不是直接是2的次方,比如byte最大值是2的8次方-1
因为Java最低位是符号位,0是正数,1是负数。
参考资料