English 中文(简体)
异构转让应当造成错误。
原标题:Numeric assignment should throw error

我正在对贾瓦的一些数字转换和投放物进行一些测试,我发现这种奇怪的行为(对我来说)。

class Test {
public static void main(String[] args) {
    //int y = 100000000000000; //does not compile
    int x = 100000 * 1000000000; //compile (why?)
    System.out.println(x); //x is 276447232
   }
}

基本上x,而且应该有相同的编号:它为什么汇编?

最佳回答

Java的过度流入没有发现,因此,倍增可奏效。 然而,你所说的字面太大,因此是一种汇编错误。

我猜测,尽管绝大多数的 Java编者在汇编时将预先计算联合后勤系统不需要的价值。 因此,这不能成为错误,因为那时不同的汇编者可能会编纂成文法或产生错误,而这种错误正是如此。

Sun编辑实际上进行计算,因为拆解显示:

Compiled from "x.java"
class x extends java.lang.Object{
x();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   ldc     #2; //int 276447232
   2:   istore_1
   3:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;
   6:   iload_1
   7:   invokevirtual   #4; //Method java/io/PrintStream.println:(I)V
   10:  return

这里需要指出的是,为了所有意图和目的,结果必须与在运行期间计算的结果相同。 因此,这里不可能出现编辑错误。

问题回答

Java处理过度注入的愤怒,不是例外,而是向消极价值倾斜,继续上升。

举例来说,这部法典载有:

int x = Integer.MAX_VALUE + 1;
System.out.println(x);

Java尽其所能。 这些珍贵产出:

-2147483648

它是Integer的价值。 MIN_VALUE。 当你处理数量确实大的问题时,你必须小心。 为什么 Java有BigInteger,处理超出惯犯限额的任意大数。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签