最近在找工作啊,笔试的时候总会碰到一些啃爹的题目啊,发给大家参考看看啊,说不定以后大家换工作的时候也会用到啊,别说我玩大家啊
字符串相等问题
public class StringTest { public static void main(String[] args) { String a1 = "abcd";
String a2 = "abcd";
System.out.println(a1 == a2);
a2.toUpperCase();
System.out.println(a1 == a2); String b1 = "abcd";
String b2 = "ab" + "cd";
System.out.println(b1 == b2);

String c1 = "abcd";
String c21 = "cd";
String c2 = "ab" + c21;
System.out.println(c1 == c2);

String d1 = new String("abcd");
String d2 = new String("abcd");
System.out.println(d1 == d2);
}}
javastring

解决方案 »

  1.   

    public class StringTest {
     
        public static void main(String[] args) {
     
            String a1 = "abcd";
            String a2 = "abcd";
            System.out.println(a1 == a2);
            a2.toUpperCase();
            //a2没有变
            System.out.println(a2);
            System.out.println(a1 == a2);
     
            String b1 = "abcd";
            String b2 = "ab" + "cd";
            System.out.println(b1 == b2);
             
            String c1 = "abcd";
            String c21 = "cd";
            String c2 = "ab" + c21;
            //只有字符串常量是共享的
            System.out.println(c1 == c2);
             
            String d1 = new String("abcd");
            String d2 = new String("abcd");
            //只有字符串常量是共享的
            System.out.println(d1 == d2);
        }
    有些要研究Java虚拟机了,但是平常就要养成习惯字符串比较不能用==
      

  2.   

    true,
    false,
    true,
    false,
    false
    不知道的对不对
      

  3.   

    常量池
    T
    T
    T
    F
    F
      

  4.   

    true
    true
    true
    false
    false
      

  5.   

    true,true,false false false
      

  6.   

    我错了,第二个应该是true,字符串是不会变得
      

  7.   

    ttt,ff我觉得是这样的!第一,二个是对同一个地址的对象操作的,所有是一样的,其它剩下的对象都不是同一个!
      

  8.   

    http://www.2cto.com/kf/201206/136505.html
    这个链接可以看一下!
      

  9.   

    s1 == s2输出true,对于String s1 = "abcd"这种字符串声明方式,先在字符串池中寻找是否有内容为:“abcd”的字符串对象,如果不存在,就会新建一个内容为:“abcd”的字符串的引用,如果已经存在,就会把内容为:“abcd”引用赋值给s2,因此s1 == s2为true,而s2.tuUpperCase()这个方法只是改变了s2里面的内容,所以也为trueb1 == b2 输出true字符串的拼接c1 == c2 输出falsed1 == d2为false