2017-03-04 60 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

相关问题