Java初级面试题

//1.请问执行下面的程序大致会输出类似什么内容?
public class TestThisToString{
  public String toString(){
    return "my memory location is :" + this;
  }
  public static void main(String[] args)  {
    System.out.println(new TestThisToString());
  }
}
//2.myTest1() 与 myTest2() 方法,哪个的效率相对较高?为什么
public class testBuilder{

  public static String myTest1(){
return "a"+"b"+"b"+"b"+"b"+"b"+"b"
+"c" +"c" +"c" +"c" +"c" ;
  }
  public static String myTest2(){
StringBuilder builder = new StringBuilder();
builder.append("a").append("b").append("b")
.append("b").append("b").append("b").append("b"
.append("c").append("c").append("c").append("c").append("c") ;
return builder.toString();
  }


  public static void main(String[] args){
System.out.println(myTest1());
System.out.println(myTest2());
  }
}

posted on 2013-07-11 21:04  岚之山  阅读(95)  评论(0编辑  收藏  举报

导航