昨天路上想到一个有意思的题目
public class Test {
public static void main(String[] args){
${place_hlod}
System.out.println("Hello World");
}
}
在${place_hold}替换一段代码,让这段程序不能显示出“Hello World”。
你有多少种解法?
抛砖引玉:
System.exit(1);//强制退出死法
String str = null;
str.concat("");//空指针死法
这个是疏忽了,感谢trainee_C同学指出
while(true){} //死循环死法
byte[] bs = new byte[1];
bs[2] = 1; //数组越界死法
int a = 7/0; //除零死法 ,Leon.Wood提供
throw new RunTimeException(""); //抛异常死法,houxinyou提供
if(0==1) //强大的逻辑死法,taoge2121提供
System.out.println("Hello World");
try { //反射死,lbfhappy提供
Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
char[] cs = (char[]) field.get("Hello World");
cs[6] = 'H';
cs[7] = 'a';
cs[8] = 'd';
cs[9] = 'e';
cs[10] = 's';
} catch (Exception ex) {
ex.printStackTrace();
}
main(null); //递归死法,ziguopu提供
}
private void anotherMethod(){
//语法死,gch_h提供