美文网首页
#jls-1# 保留字,整数和浮点数

#jls-1# 保留字,整数和浮点数

作者: LeeMin_Z | 来源:发表于2018-07-05 21:13 被阅读1次

    鉴于基础不牢固,之前教程选得不好。我换了官网上最新的入门教程,除了内容有点多,其他都很好。

    The Java Language Specification-java10 se

    3.9 Keywords

    1. 保留关键字 reserved keywords

    Keyword:
    (one of)
    abstract continue for new switch
    assert default if package synchronized
    boolean do goto 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
    _ (underscore)

    1. not keywords

    除了提到了保留关键字外,以下的是识别码。
    A variety of character sequences are sometimes assumed, incorrectly, to be keywords:
    true and false are not keywords, but rather boolean literals (§3.10.3).
    null is not a keyword, but rather the null literal (§3.10.7).
    var is not a keyword, but rather an identifier with special meaning as the type of a local
    variable declaration (§14.4, §14.14.1, §14.14.2, §14.20.3).

    1. 限制性关键词 restricted keywords

    open, module,
    requires, transitive, exports, opens, to, uses, provides, and with

    4. Types, Values, and Variables

    Java编程语言是一种静态类型语言,意思是每个变量和每个表达式都有一个在编译时已知的类型。Java编程语言也是一种强类型语言,因为类型限制变量(§4.12)可以容纳的值或表达式可以产生的值,限制这些值支持的操作,并确定其含义操作。强静态类型有助于在编译时检测错误。

    4.2.2 Integer Operations

    其实不用记,不同类型包含的大小都是可以查的,记得关键字和tab就好了。以下是jshell的一段溢出错误代码:

    //Example 4.2.2-1. Integer Operations
    
    jshell> int i = 1000000;
    i ==> 1000000
    
    jshell> i*i
    $18 ==> -727379968      //wrong output
    
    //why, check max value
    jshell> Integer.MAX_VALUE
    $23 ==> 2147483647
    
    jshell> long longi = i
    longi ==> 1000000
    
    jshell> longi * longi
    $20 ==> 1000000000000     //correct output 
    
    //why, check max value 
    
    jshell> Long.MAX_VALUE
    $24 ==> 9223372036854775807
    
    //cannnot divide zero example 
    jshell> 55555/(longi - i)
    |  java.lang.ArithmeticException thrown: / by zero
    |        at (#28:1)
    

    4.2.3 Floating-Point Types, Formats, and Values

    a. 特殊值

    1. 普遍:非零数
    2. 五个特数值: NaN值,正零positive zero,负零negative zero,正无穷大positive infinity和负无穷大negative infinity。
    3. 有序排列:除NaN外,浮点值是有序的;排列从最小到最大的是:负无穷大,负有限非零值,正零和负零,正有限非零值和正无穷大。
    4. 正负零:赋值时候零都是正零;但直接计算时分正零和负零,java设定的零值其实是极小数(而不是数学定义的绝对零值),所以会计算出无穷。
    //Positive zero and negative zero compare equal;
    jshell> float a = +0
    a ==> 0.0
    
    jshell> float b = -0
    b ==> 0.0
    
    //positive zero, negative comparison 
    jshell>  +0.0 == -0.0
    $1 ==> true
    
    jshell>  +0.0 > -0.0
    $2 ==> false
    
    //zeros and Infinity values 
    jshell> 1.0/0.0
    $3 ==> Infinity
    
    jshell> 1.0/-0.0
    $4 ==> -Infinity
    
    1. NaN is unordered
      简单记,大部分布尔运算中含有NaN,返回false;只有!=中有NaN会返回true。
      • The numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.20.1).In particular, (x<y) == !(x>=y) will be false if x or y is NaN.
      • The equality operator == returns false if either operand is NaN.
      • The inequality operator != returns true if either operand is NaN (§15.21.1).In particular, x!=x is true if and only if x is NaN.

    4.2.4 Floating-Point Operations

    • 为什么会有不精确的结果?
      Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to nearest.

    • java中的零值实际是大约值
      The Java programming language uses round toward zero when converting a floating value to an integer (§5.1.3)

    Example 4.2.4-1. Floating-point Operations

    1. 在java里,超过double就会被标记为“无穷”。
    // An example of overflow:
    jshell> Double.MAX_VALUE
    $29 ==> 1.7976931348623157E308
    
    jshell> double d = 1e308
    d ==> 1.0E308
    
    jshell> d*10
    $31 ==> Infinity
    
    1. 小于一定值,就会被标记为0。发散想一下,大约就是为什么无穷个0相加等于1了。
    // An example of gradual underflow:
    void a() {
    for (int i = 0; i < 4; i++)
    System.out.print(" " + (d /= 100000)+"\n");}
    
    jshell> d = Math.PI * 1e-305
    d ==> 3.141592653589793E-305
    
    jshell> a()
     3.1415926535898E-310
     3.141592653E-315
     3.142E-320
     0.0
    
    1. 其他比较神奇的输出。

    0.0/0.0既不是抛出错误“0不可做除数”,也不是(当做自身相除)的1

    // An example of NaN:
    jshell> d = 0.0/0.0
    d ==> NaN
    

    理论上的数学公式, x / y * y = x;在计算上不一定成立。

    // An example of inexact results and rounding:
    void b(){
    for (int i = 0; i < 100; i++) {
    float z = 1.0f / i;
    if (z * i != 1.0f)
    System.out.print(" " + i);}
    }
    
    jshell> b()
     0 41 47 55 61 82 83 94 97
    
    jshell> float z = 1.0f
    z ==> 1.0
    
    jshell> z/41 * 41
    $56 ==> 0.99999994
    
    jshell> z/47 * 47
    $57 ==> 0.99999994
    
    1. 取整是向着零点取整
    // An example of the cast to integer rounding:
    
    jshell> d = 12345.6
    d ==> 12345.6
    
    jshell> (int) d
    $64 ==> 12345
    
    jshell> (int) (-d)
    $65 ==> -12345
    

    学习进度
    The Java Language Specification-java10 se
    P 0 - P 68

    2018.7.5

    相关文章

      网友评论

          本文标题:#jls-1# 保留字,整数和浮点数

          本文链接:https://www.haomeiwen.com/subject/zuoeuftx.html