String常见

来源:互联网 发布:淘宝ios客户端 编辑:程序博客网 时间:2024/06/05 15:02
15,对于代码String s1 = new String(“a”);一共创建了几个对象?
 A.0
 B.1
 C.2
 D.3

 E.4

正确答案:C

解析:http://blog.csdn.net/yishao_20140413/article/details/23598113

16,

package day01;
/*

*/
public class StringDemo1 {
 public static void main(String[] args) {
  String s1="123abc";
  String s2="123abc";
  String s3=new String("123abc");  
  String s4=new String("123abc");
  System.out.println("s1==s2:"+(s1==s2));//true
  System.out.println("s1==s3:"+(s1==s3));//false
 System.out.println("s3==s4:"+(s3==s4));//false
  System.out.println(s1.equals(s2));//true
  System.out.println(s1.equals(s3));//true
  
  s1+="!";
  System.out.println(s1);//123abc!
  /*
   * 编译器在编译程序时,若发现一个计算表达式计算符
   * 两边都是字面量(直接量)时,会直接计算
   * 并将结果编译到.class文件中
   */
  
  String s4="123"+"abc";
  System.out.println("s2==s4:"+(s2==s4));//s2==s4:true
  
  String s="123";
  String s5=s+"123";
  System.out.println("s1==s5:"+(s1==s5));//s1==s5:false
 }
}

原创粉丝点击