zoukankan      html  css  js  c++  java
  • Java中判断String字符串是否相等

    一定要注意一下几点:

    • 只要使用引用变量 a 来加一个常量池内容"xxx"或者引用变量,都是放在堆里
    • intern()返回的是常量池中字符串的引用,而不是堆中字符串的引用
    首先看第一个字符串比较的例子
    public static void main(String[] args){
    
            String a = "a";
            String b = "b";
            // 这里的a + b应该是放在堆里的对象,常量池没有
            String c = a + b;
            // 这里的a + b应该是放在常量池里面的
            String d = "a" + "b";
            // 经测试,发现只要有使用引用变量a加上一个常量池内容"xxx"或者引用变量,都会放在堆里
            String e = a + "b";
        	// 注意intern()返回的是常量池中字符串的引用,而不是堆中字符串的引用
            String f = c.intern();
            String g = "ab";
            String h = new String("ab");
    
            System.out.println(a + b == "ab");  // false
            System.out.println(c == "ab");      // false
            System.out.println(c == a + b);     // false
            System.out.println(c == h);         // false
            System.out.println(c == f);         // false
            System.out.println(d == e);         // false
            System.out.println(d == f);         // true
            System.out.println(d == g);         // true
            System.out.println(f == g);         // true
        
    }
    
    下面看另外一个关于创建了几个对象的例子:
    public static void main(String[] args){
    
    	String a = "abc";
        String b = "abc";
        String c = new String("abc");
        /*
        	注意:这个虽然看起来似乎要在常量池新建三个字符串对象:"ab","c",和拼接生成的"abc"
            但是结果是内存中仅有生成的,前面的两个算是过程变量。这反编译得出来的结论!
            这样做实际上是一种优化,避免了创建多余的字符串对象,也没有发生字符串拼接问题
        */
        String d = "ab" + "c";
    
        System.out.println("a == b" + " " + (a == b));  // true 
        System.out.println("a == c" + " " + (a == c));  // false
        System.out.println("a == d" + " " + (a == d));  // true
        System.out.println("b == c" + " " + (b == c));  // false
        System.out.println("b == d" + " " + (b == d));  // true
        System.out.println("c == d" + " " + (c == d));  // false
    }
    

      这个例子主要强调了类似于String d = "ab" + "c";String d = new String("ab" + "c");创建了几个对象的问题。答案:前者1个(常量池创建),后者2个(常量池、堆都要创建)。

  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/flyingrun/p/13662085.html
Copyright © 2011-2022 走看看