java中String字符串==的疑问?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/czh500/article/details/85340210

java中String字符串==的疑问?谁能帮忙解释和理解一下!顺便说一下底层的原理和运行规则?
直接po代码和截图

public class TestString {

	/** 
	* 测试String
	*/
	public static void main(String[] args) {
		String a = "hello2";
		final String b = getStr2();
		String c = b + 2;
		String d = "hello2";
		String e = new String("hello2");
		String f = new String("hello2");
		String h = "hello";
		String j = h + 2;
		StringBuffer sb = new StringBuffer("hello2");
		StringBuilder sb2 = new StringBuilder("hello2");
		String k = getStr2();
		String m = k + 2;
		System.out.println("c结果" + (a == c));
		System.out.println("d结果" + (a == d));
		System.out.println("e结果" + (a == e));
		System.out.println("f结果" + (a == f));
		System.out.println("f.toString()结果" + (a == f.toString()));
		System.out.println("j结果" + (a == j));
		System.out.println("sb.toString()结果" + (a == sb.toString()));
		System.out.println("sb2.toString()结果" + (a == sb2.toString()));
		System.out.println("m结果" + (a == m));
	}
	
	public static String getStr() {
		String text = "hello";
		return text;
	}
	
	public static String getStr2() {
		return "hello";
	}

}

public static void main(String[] args) {
		String a = "hello2";
		final String b = "hello";
		String d = "hello";
		String c = b + 2;
		String e = d + 2;
		System.out.println((a == c));
		System.out.println((a == e));

	}

猜你喜欢

转载自blog.csdn.net/czh500/article/details/85340210