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());
  }
}

转载于:https://www.cnblogs.com/lanzhi/p/6467121.html

你可能感兴趣的:(java)