java中可能有多少链式赋值?

时间:2018-01-22 12:59:07

标签: chained-assignment

例如

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

所以,在java中有多长时间......

1 个答案:

答案 0 :(得分:0)

好吧,让我们试一试!

codegen.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答案!!!

test.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的堆栈大小来轻松纠正这个问题。