日期:2014-05-20  浏览次数:20727 次

一个很BT的简单问题,来解决!hoho
public   class   A   {
  public   static   int   test(){
      int   i=9;
      try{
        return   i;
      }finally{
        i=10;
      }
  }
  public   static   StringBuffer   test1(){
      StringBuffer     i=new   StringBuffer(9);
      try{
        return   i;
      }finally{
        i.append(10);
        i=null;
      }

  }
  public   static   void   main(){
      System.out.println(test());
    System.out.println(test1());

  }
}

这个结果是多少?可能你知道原因,也可能你会猜对,不过大家知道原因不?hoho,其实我以前也不知道,最近才看到的,共享出来大家娱乐一下

这里有具体讲解:
http://blog.csdn.net/lovingprince/archive/2007/05/16/1612206.aspx

------解决方案--------------------
java.lang.NoClassDefFoundError: Test
Exception in thread "main " !!
他说我有错误%%%%555555
------解决方案--------------------
9,10.
why?
------解决方案--------------------
Exception in thread "main " java.lang.NoSuchMethodError: main
------解决方案--------------------
嗯神奇
不过看了原理,应该这样更清晰
public class Abc {

public static int test(){
int i=9;
try{
return i;
}finally{
i=10;
}
}
public static StringBuffer test1(){
StringBuffer i=new StringBuffer( "9 ");
try{
return i;
}finally{
i.append( "10 ");
i=null;
}

}
public static void main(String[] arg){
System.out.println(test());
System.out.println(test1());

}

}
------解决方案--------------------
public class InnerBox {

public static int getI(){
int i;
try{
i=9;

}finally{
i=10;
}
return i;
}

public static StringBuffer getSb(){
StringBuffer sb=new StringBuffer( "123 ");
try{

}finally{
sb.append( "sb ");
sb=null;

}
return sb;
}



/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getI());
System.out.println(getSb());
}

}
这样效果就不一样了。
------解决方案--------------------
参数的传递~~
这个问题好象问的很多了在csdn中
------解决方案--------------------
这是finally代码块的执行问题~
------解决方案--------------------
详细解答如下:
public class Abc {

public static int test(){
int i=9;
try{
return i;
}finally{
i=10;
}
}
public static StringBuffer test1(){
StringBuffer i=new StringBuffer(9);
//注意StringBuffer(int i)是构造一个指定容量的StringBuffer变量;