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

java语言定义的变量包含一下四种类型html

  1. 实例变量(Instance Variables),非静态变量,在Class中声明的field,未使用static声明; java

  2. 类变量(Class Variables ),静态变量,在Class中使用static标识; express

  3. 本地变量(Local Variables),在一个方法中声明的变量; oracle

  4. 参数(Parameters),方法定义的形参; jvm

命名 this

  1. 大小写敏感; spa

  2. 不限长度; .net

  3. 以字母、数字、下划线和“$”符号组成,不能够以数字开头; code

  4. 不能够是Java保留字 orm

保留字参考: 

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'

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   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 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  )



转载请注明出处:http://my.oschina.net/hassan/blog/423247 

相关文章
相关标签/搜索