文章目录
两种数据类型
- Primitive Types
Built-in data types.
Wrappers - Reference Types
Classes(注意:String
类型是reference type)
Arrays
Interfaces
enum
一、primitive types原始类型
1.Built-in内置类型
boolean
, char (Unicode)
, byte
,short
, int
, long
, float
, double
, void
– not like C/C++: 0 doesn’t mean false !
– Size of each data type is machine independent!
– Portability & performance implications
– In a class, primitives get default values.
2.Wrappers内置类型包装
Boolean
, Character
, Byte
,Short
, Integer
, Long
, Float
, Double
,Void
.
- subclasses of the abstract class
java.lang.Number
自动封装/拆装:
Integer a = 5;
int b = a; //int b = new Integer(5);
3.Literals字面值
Literals are fixed values(固定的值) found in a program.
PS: we don’t use the term “constant” for this, we use “constant” for something else…
(1)布尔型Literals
true
和false
(2)整型Literals
范围: [ − 2147483648 ( − 2 31 ) , 2147483647 ( 2 31 − 1 ) ] [-2147483648 (-2^{31}) , 2147483647(2^{31}-1)] [−2147483648(−231),2147483647(231−1)],21亿
(3)长整型Literals
-
Literal has the suffix
l
orL
23L
,0100l
,0xABCDEF01234L
-
Without the trailing ‘l|L’ any integral literal is an integer type.
-
[ 9223372036854775808 ( − 2 63 ) , 9223372036854775807 ( 2 63 − 1 ) ] [9223372036854775808 (-2^{63}) , 9223372036854775807 (2^{63}-1)] [9223372036854775808(−263),9223372036854775807(263−1)],922亿亿
(4)浮点型Point Literals
- Decimal only (base 10).浮点数只有十进制
- Optional suffix:
f
orF
means floatd
orD
means double
例子:
12.5
必须是double,float a=12.5
报错,double a=12.5
才行12.5f
才是float,float a=12.5f
。1.5e4
和1.5e-4
是科学计数法,前者是 1.5 × 1 0 4 1.5\times 10^4 1.5×104,后者是 1.5 × 1 0 − 4 1.5\times 10^{-4} 1.5×10−4
(5)Character Literals
- 字符是用Unicode码,而不是c/c++中的ASCII码
- character literals look just like they do in C/C++
'a'
'b'
'C'
'\n'
'\t'
- anything that starts with \u is treated as hex encoding of the Unicode number (U+0000~U+FFFF)
'\u03df'
'\u003f'
- code point (32-bit int
0x10000
~0x10ffff
)
π(3.1415926) → 0x3c0→\u03c0
System.out.println('\u03c0');
//π
(6)Other Literals
- array literals (used only for initialization):
int a[] = {1,3,5,7,9};
(7)下划线(_)在数字Literals中
用来让数字显示得更清楚,加不加无所谓,只能用在数字和数字之间
(8)进制
进制:
-
十进制(默认)
0
,23
-
二进制(用
0b
或0B
开头) -
八进制(用数字
0
开头)
01
,023
-
十六进制(用
0x
或0X
开头。0
是数字不是字母,x
和十六进制中的字母大小写无所谓)
0x1
,0x1f
,0X1f
,0x1F
,0X1F
/*八进制*/
int a=011;
System.out.println(a);
//9
/*十六进制*/
int b=0x1f;
System.out.println(b);
//31
int c=0X1f;
System.out.println(c);
//31
int d=0x1F;
System.out.println(d);
//31
int e=0X1F;
System.out.println(e);
//31