2017年9月24日 关于String的几道面试题

题目1   用== 比较String
/*
* ==比较引用类型时,比较的是虚地址 * String类型比较特殊,当使用=赋值时,相同内容的字符串本质是同一个对象,虚地址相同,而使用new赋值 * 的时候,每次都会创建一个新的对象 * */ public class Demo1 { public static void main(String[] args) { String str1="abc"; String str2="abc"; String str3=new String("abc"); String str4=new String("abc"); System.out.println(str1==str2); //true System.out.println(str3==str4); //false System.out.println(str1==str3); //false } }

 

题目2  String类用==赋值

/*
 * String是引用类型,是比较特殊的类,是只读对象,对String 有任何改变,都将返回一个新的对象
 * */

public class Demo2 {
//    private String str;
    public static void change(String str){
        str ="abc";
        System.out.println("change():"+str);  //输出abc
    }
    public static void main(String[] args) {
        String str="123";
        change(str);
        System.out.println(str);  //输出123
    }
}

 

题目3  用equals比较String

 

 1 public class Demo3 {
 2       public static void main(String[] args) {
 3             String str1="abc";
 4             String str2="abc";
 5             
 6             String str3=new String("abc");
 7             String str4=new String("abc");
 8             
 9             System.out.println(str1.equals(str2));   //true
10             System.out.println(str3.equals(str4));   //true
11             System.out.println(str1.equals(str3));   //true
12         }
13 }

 

看一下equals的源码,最后比较的是anotherString.value,字符序列的值,而不是地址了

 1  public boolean equals(Object anObject) {
 2         if (this == anObject) {
 3             return true;
 4         }
 5         if (anObject instanceof String) {
 6             String anotherString = (String)anObject;
 7             int n = value.length;
 8             if (n == anotherString.value.length) {
 9                 char v1[] = value;
10                 char v2[] = anotherString.value;
11                 int i = 0;
12                 while (n-- != 0) {
13                     if (v1[i] != v2[i])
14                         return false;
15                     i++;
16                 }
17                 return true;
18             }
19         }
20         return false;
21     }

 

题目4 判断一个String是不是空值

 

public class Deno4 {
  /*
   * 判断一个字符串是否为空
   * */
    public static void main(String[] args) {
        //当要判断的String 为null时,使用equals方法会出现空指针异常
        String str ="abc";
        if(str!=null&&!str.equals(""))
            System.out.println(str+"不为空");
    }
}

 

题目5 字符串的不可变性质

 

/*字符串的不可变性质*/
public class Demo5 {
    public static void main(String[] args) {
        String str="abc";
        str.toUpperCase();
        System.out.println(str);   //输出abc,还是小写,不能改变字符串的值,还是原来的
        str=str.toUpperCase();     //只能用新对象来接收才行,原来的abc还保存在内存里
        System.out.println(str);   //输出ABC  
    }
}

 

 

题目6 字符串和基本类型的转换

/*
 * 需要把字符串转换成整型或浮点型的数值进行运算,Integer、Double等包装类都定义了parseXXX方法
 * 可以进行转换
 * */
public class Demo6 {
    public static void main(String[] args) {
        String str="123";
        int i = Integer.parseInt(str);
        System.out.println(i);
    }
}

 

 

题目7  使用new创建字符串对象

/*使用new创建字符串对象*/
public class Demo7 {

    public static void main(String[] args) {
        // 问:这个语句创建了几个对象?
        String str = new String("abc");
        /*
         * 答:一个或者二个
         * JVM中存在一个字符串常量池,保存了大量的字符串对象,并共享使用。使用new String("abc")
         * 创建对象时,如果常量池中没有abc,首先会在对象池中创建字符串abc,然后在堆中创建字符串对象,赋值
         * 给str,如果常量池中有abc,则只创建一个对象
         */
    }
}

 

 题目8    用==比较字符串常量

 

/*用==比较字符串常量*/
public class Demo8 {
   public static void main(String[] args) {
       
    System.out.println("Micheal"+"Scofield"=="MichealScofield");  //true
    
}
}

 

题目9  把基本类型转换为String类型

 

/*把基本类型转换为String类型*/
public class Demo9 {
    public static void main(String[] args) {
        int i =123;
        String str=String.valueOf(i);
        System.out.println(str);
    }
}

 

 

 

题目10 比较String 字典顺序

 1 /*如何比较String的字典顺序*/
 2 public class Demo10 {
 3    public static void main(String[] args) {
 4        
 5        /***
 6         * a b c d e f g h i j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
 7         * 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 8         */
 9      String str1="kobe";
10      String str2="james";
11      String str3 ="kill";
12      int i1=str1.compareTo(str2);
13      System.out.println(i1);//返回1  正数为str1大于str2
14      int i2 =str1.compareTo(str3);
15      System.out.println(i2);//返回6  首字母相同则比较第2个字母  o=15 i=9 所以返回6 
16 }
17 }

 

题目11 String 和StringBuffer的区别

 1 /**
 2  * String 和StringBuffer区别
 3  * 1、String总是会创建新对象,不可变类,一修改则创建新对象,StringBuffer是可变的
 4  * 2、StringBuffer效率高
 5  * */
 6 public class Demo11 {
 7   public static void main(String[] args) {
 8     String str="abc";
 9     str.replace("a", "x");
10     System.out.println(str);  //返回abc
11     
12     StringBuffer sbf = new StringBuffer("abc");
13     sbf.replace(0, 1, "x");
14     System.out.println(sbf);  //返回xbc    
15 }
16 }

题目12 StringBuffer和StringBuilder的区别

 1 /*StringBuffer和StringBuilder的区别
 2  * 1、StringBuilder效率略高  
 3  * 2、StringBuilder 线程不安全  StringBuffer是线程安全
 4  * 
 5  * */
 6 public class Demo12 {
 7 public static void main(String[] args) {
 8     long time1 =System.currentTimeMillis();
 9     StringBuffer sbf = new StringBuffer("");
10     for (int i = 0; i < 5000000; i++) {
11         sbf.append("boom");
12     }
13     long time2 =System.currentTimeMillis();
14     long times=time2-time1;
15     System.out.println("StringBuffer所用毫秒:"+times);
16     
17     long time3 =System.currentTimeMillis();
18     StringBuilder sbd = new StringBuilder("");
19     for (int i = 0; i < 5000000; i++) {
20         sbf.append("boom");
21     }
22     long time4 =System.currentTimeMillis();
23      times=time4-time3;
24     System.out.println("StringBuilder所用毫秒:"+times);  
25 }
26 }

 

 

 

posted on 2017-09-24 15:45  Loseheart  阅读(151)  评论(0编辑  收藏  举报