java中可能有多少链式赋值?
Posted
技术标签:
【中文标题】java中可能有多少链式赋值?【英文标题】:How much chained assignment possible in java? 【发布时间】:2018-07-01 02:50:49 【问题描述】:例如
int a,b,c,d,e,f,g,h,.........................;
a=b=c=d=e=f=g=h=.............................=1;
那么,在java中它可以用多长时间......
【问题讨论】:
问题很清楚,但是……“为什么?” xD 【参考方案1】:好吧,让我们试试吧!
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 ""
警告:不要在包含 *** 的所有 *.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.***Error
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
的堆栈大小来轻松纠正此问题。
【讨论】:
感谢 Andrey Tyukin 的解决方案。以上是关于java中可能有多少链式赋值?的主要内容,如果未能解决你的问题,请参考以下文章