#jls-1# 保留字,整數(shù)和浮點數(shù)

鑒于基礎不牢固,之前教程選得不好。我換了官網(wǎng)上最新的入門教程,除了內(nèi)容有點多,其他都很好。

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編程語言是一種靜態(tài)類型語言,意思是每個變量和每個表達式都有一個在編譯時已知的類型。Java編程語言也是一種強類型語言,因為類型限制變量(§4.12)可以容納的值或表達式可以產(chǎn)生的值,限制這些值支持的操作,并確定其含義操作。強靜態(tài)類型有助于在編譯時檢測錯誤。

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. 普遍:非零數(shù)
  2. 五個特數(shù)值: NaN值,正零positive zero,負零negative zero,正無窮大positive infinity和負無窮大negative infinity。
  3. 有序排列:除NaN外,浮點值是有序的;排列從最小到最大的是:負無窮大,負有限非零值,正零和負零,正有限非零值和正無窮大。
  4. 正負零:賦值時候零都是正零;但直接計算時分正零和負零,java設定的零值其實是極小數(shù)(而不是數(shù)學定義的絕對零值),所以會計算出無窮。
//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。發(fā)散想一下,大約就是為什么無窮個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不可做除數(shù)”,也不是(當做自身相除)的1

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

理論上的數(shù)學公式, 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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,911評論 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,847評論 0 10
  • Lua 5.1 參考手冊 by Roberto Ierusalimschy, Luiz Henrique de F...
    蘇黎九歌閱讀 14,246評論 0 38
  • 一、少年不知愁滋味 4. 可我知,我深知,那人他永遠不會來 我拉著董凌走到河邊,這個季節(jié),水漲起來,剛剛沒過岸邊的...
    Scarlett_Ch閱讀 841評論 0 2
  • 人的一生會路過很多人,認識很多人,又錯過很多人,但是不會愛上很多人;我一直堅信,無論哪種愛情,來了,就不應該隨便說放手…
    凡豆琪閱讀 226評論 0 0

友情鏈接更多精彩內(nèi)容