您的位置:首页 > 编程语言 > Java开发

Learn Java - Chapter 1 变量(Variables)-基本类型

2015-06-01 10:16 627 查看
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 doif 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 superwhile
注: * 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)范围
byte8-128 ~ 127
short16-32,768 ~ 32,767
int32-231 ~ 231-1
long64-263 ~ 263-1
float32有点复杂,继续往下看...
double64同上
boolean-
取值:ture或者false,官方说法占1bit,大小不精确定义
char16Unicode:'\u0000'~'\uffff'
float、double的范围和浮点数的定义有关,先参考specification来看看:https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3

浮点数范围可以用 s · m · 2(e - N + 1) 这个公式来表达

s:+1或-1

m是小于2N 的正整数

e是个整数,范围 between Emin = -(2K-1-2) and Emax = 2K-1-1

NKE取值参考表:

Parameter float float-extended-exponent doubledouble-extended-exponent
N24245353
K8≥ 1111≥ 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 float or 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

转载请注明出处:/article/2175619.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: