0

例如

int a,b,c,d,e,f,g,h,.........................;
a=b=c=d=e=f=g=h=.............................=1;

那么,在java中可以实现多长时间......

4

1 回答 1

2

好吧,让我们试试吧!

代码生成.sh:

#!/bin/bash
# indentation deliberately weird, 
# so that the generated code is legible
N="$1"
echo "public class Chain$N {"
echo "    public static void main(String[] args) {"
for i in $(seq 1 $N)
do
echo "        int x$i;"
done
echo -n "        "
for i in $(seq 1 $N)
do
echo -n "x$i="
done
echo "42;"
echo "    }"
echo "}"

警告:不要在包含 StackOverflow 的所有 *.java 答案的目录中运行它!!!

测试.sh:

#!/bin/bash
rm Chain*.java    # WARNING! DON'T RUN IT IN NON-EMPTY DIRECTORIES!
rm Chain*.class
for n in 10 100 1000 2000 2025 2037 2050 2075 2100
do
  ./codegen.sh $n >> "Chain$n.java"
  (javac -Xmx1000M Chain$n.java 2> /dev/null) || echo "failed for $n"
  (java Chain$n 2> /dev/null) || echo "failed to run for $n"
done

示例 Chain10.java:

public class Chain10 {
    public static void main(String[] args) {
        int x1;
        int x2;
        int x3;
        int x4;
        int x5;
        int x6;
        int x7;
        int x8;
        int x9;
        int x10;
        x1=x2=x3=x4=x5=x6=x7=x8=x9=x10=42;
    }
}

通过简单的手动二等分,很容易找到它失败的点。在我的设置中,默认设置下 N=2075 失败。编译器崩溃并显示以下消息:

The system is out of resources.
Consult the following stack trace for details.
java.lang.StackOverflowError
        at com.sun.tools.javac.comp.Check.checkType(Check.java:533)
        at com.sun.tools.javac.comp.Attr$ResultInfo.check(Attr.java:482)
        at com.sun.tools.javac.comp.Attr.check(Attr.java:281)
        at com.sun.tools.javac.comp.Attr.checkIdInternal(Attr.java:3642)
        at com.sun.tools.javac.comp.Attr.checkId(Attr.java:3490)
        at com.sun.tools.javac.comp.Attr.visitIdent(Attr.java:3233)
        at com.sun.tools.javac.tree.JCTree$JCIdent.accept(JCTree.java:2011)
        at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)
        at com.sun.tools.javac.comp.Attr.visitAssign(Attr.java:2994)
        at com.sun.tools.javac.tree.JCTree$JCAssign.accept(JCTree.java:1686)
        at com.sun.tools.javac.comp.Attr.attribTree(Attr.java:576)
        at com.sun.tools.javac.comp.Attr.attribExpr(Attr.java:618)
        at com.sun.tools.javac.comp.Attr.visitAssign(Attr.java:2996)
        at com.sun.tools.javac.tree.JCTree$JCAssign.accept(JCTree.java:1686)

我认为您可以通过增加javac.

于 2018-01-22T13:55:49.997 回答