2017-03-04 92 views
0

我們如何在Java中編寫浮點數的二進制,八進制和十六進制文字?無論是科學還是正常的表現。我們如何在Java中編寫浮點數的二進制,八進制和十六進制文字?

class First 
{ 
     public static void main(String[] args) 
     { 
       double b = 0x12p3; 
       System.out.println(b); 
     } 
} 

上述程序導致144.0

class First 
{ 
     public static void main(String[] args) 
     { 
       double b = 0x12e3; 
       System.out.println(b); 
     } 
} 

上述程序導致4835.0

class First 
{ 
      public static void main(String[] args) 
      { 
        double b = 0x12e3; 
        System.out.println(b); 
      } 
} 

上述程序導致4835.0

class First 
{ 
      public static void main(String[] args) 
      { 
        double b = 012e3; 
        System.out.println(b); 
      } 
} 

上述程序導致12000

請解釋以上輸出。

+0

不是你最後一個問題中的選民,而是[這個問題和答案](https://stackoverflow.com/questions/39805105/where-does-the-constructor-invocation-get-stored-stack-or-heap)是相關的。請注意,[詢問之前先搜索]總是一個好主意(https://www.google.com/#q=java+does+calling+a+constructor+use+stack+memory) –

回答

1

第一個例子是一個HexadecimalFloatingPointLiteral,(16 + 2)* 8 = 144

第二和第三個例子實際上是指定給一個雙變量HexIntegerLiteral,1 * 16 * 16 * 16 + 2 * 16 * 16 + 14 * 16 + 3 = 4835.注意, 'e' 的僅僅是十六進制數字爲14

最後一個例子是一個DecimalFloatingPointLiteral,12 * 1000

對於所有的細節,參見JLS:https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2

相關問題