首页 > 代码库 > Java 语法 索引 ----- 变量-----数据类型

Java 语法 索引 ----- 变量-----数据类型

数据类型

类型

bits/byte

范围

默认值

byte 8/1 -128
+127
0
short 16/2 -32,768
+32,767
0
int 32/4 -2,147,483,648 = -231
+2,147,483,647 = 231-1
0
long 64/8 -9,223,372,036,854,775,808 = -263
+9,223,372,036,854,775,807 = 263-1
0L
float 32/4 1.40129846432481707e-45  = 2-149
3.40282346638528860e+38  = (2-2-23)·2127
0.0f
double 64/8 4.94065645841246544e-324d = 2-1074
1.79769313486231570e+308d = (2-2-52)·21023
0.0d
char 16/2 0 = U+0000
65,535 = U+10FFFF
0
boolean 1 true or false false

 

float 和 double 都不能用于表示精确值,推荐用 Java.Math.BigDecimal

声明变量和赋值

// normal statementbyte myInt8 = 2; short myInt16 = 1; int myInt32 = 0; long myInt64 = -1; //using octal or hexadecimal notationint myHex = 0xF; // hexadecimal (base 16)int myOct = 07; // octal (base 8)//double 类型double myDouble = 3.14; double myDouble2 = 3e2;//float 如果你没有节省空间的需求,还是用double吧float myFloat = 3.14; //errorfloat myFloat = (float)3.14;float myFloat = 3.14F; //char typechar myChar = ‘A‘char myChar = ‘\u0000‘;//boolean typeboolean myBool = false;