java语言定义的变量包含一下四种类型
实例变量(Instance Variables),非静态变量,在Class中声明的field,未使用static声明;
类变量(Class Variables ),静态变量,在Class中使用static标识;
本地变量(Local Variables),在一个方法中声明的变量;
参数(Parameters),方法定义的形参;
命名
大小写敏感;
不限长度;
以字母、数字、下划线和“$”符号组成,不可以以数字开头;
不可以是Java保留字
保留字参考:
abstract
continue
for
new
switch
assert***
default
goto*
package
synchronized
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum****
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp**
volatile
const*
float
native
super
while
注: * not used
** java 1.2 后添加
*** java 1.4 后添加
**** java 5.0 后添加
约定
变量命名以小写字母开头,单词全拼,多个单词以驼峰形式命名,eg:String currentRatio。如果是常量,全大写,多个单词以“_”下划线分隔。eg:static final double PI = 3.1415926,static final String BAIDU_URL="xxx";
Java基本数据类型表
类型
长度(bit)
范围
byte
8
-128 ~ 127
short
16
-32,768 ~ 32,767
int
32
-231~ 231-1
long
64
-263~ 263-1
float
32
有点复杂,继续往下看...
double
64
同上
boolean
-
取值:ture或者false,官方说法占1bit,大小不精确定义
char
16
Unicode:'\u0000'~'\uffff'
浮点数范围可以用 s·m· 2(e - N + 1) 这个公式来表达
s:+1或-1
m是小于2N 的正整数
e是个整数,范围 betweenEmin= -(2K-1-2) andEmax= 2K-1-1
NKE取值参考表:
Parameter
float
float-extended-exponent
double
double-extended-exponent
N
24
24
53
53
K
8
≥ 11
11
≥ 15
Emax
+127
≥ +1023
+1023
≥ +16383
Emin
-127
≤ -1022
-1022
≤ -16382
也就是说通常情况下,浮点的范围是由以上表达式和表格(float列和double列)决定,我暂且叫他float值集和double值集。
这时你可能注意到了 float-extended-exponent或double-extended-exponent东西,这个我个人理解成是①float-extended-exponent值集和②double-extended-exponent值集。
①②这两个值集是Java语言有实现的另外两个值集,在一定情况下会使用这类值集(原文“These extended-exponent value sets may, under certain circumstances, be used instead of the standard value sets to represent the values of expressions of type floator double”,参考:https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3)。
这就引出了一个问题,在不同JVM环境中,会由于浮点范围导致运算结果有差异。为了解决程序可移植性,在不同平台浮点运算能有相同的结果,官方提供了一个Java关键字“strictfp”,使用这个关键字声明类、接口或方法java编译器已经jvm会按照IEEE-754标准来执行,这样就可以保证浮点运算一致性。(原文“Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats.”参考:https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.4 )