---
title: "Java中String的相等判断，==和.equals()总结"
author: "Quanta1990"
date: "2015-02-05T15:37:44Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java-string-equals"
draft: false
lastmod: "2015-02-05T16:07:13Z"
id: 2183729
url: "https://quanta1990.iteye.com/blog/2183729"
---

今天偶然看到stackoverflow上关于==和.equals()的分析，记录一下

 [点我查看原链接](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)

 

==            测试引用相等。

.equals()  测试值相等。

 

所以呢，

如果你真的想看这两个string是不是有相同的值，那就用.equals()(除了一些特殊的情况，你得保证这两个有着相同value的string是用同一种对象表现的，例如String interning)

==呢，是用来测试两个字符串是否是同一个对象

 

看看一下代码吧！(格式略微有些乱)

```
public static void main(String[] args) throws IOException, MemfisException{
	String t1 = new String("T");
	String t2 = t1;
	
	// These two have the same object
	System.out.println("1.	t1 == t2  -->  "+(t1 == t2)); 
	
	// These two have the same value
	System.out.println("2.	new String(\"test\").equals(\"test\")  -->  "+new String("test").equals("test"));  

	// ... but they are not the same object
	System.out.println("3.	new String(\"test\") == \"test\"  -->  "+(new String("test") == "test")); 

	// ... neither are these
	System.out.println("4.	new String(\"test\") == new String(\"test\")  -->  "+(new String("test") == new String("test"))); 

	// ... but these are because literals are interned by 
	// the compiler and thus refer to the same object
	System.out.println("5.	\"test\" == \"test\"  -->  "+("test" == "test")); 

	// concatenation of string literals happens at compile time,
	// also resulting in the same object
	System.out.println("6.	\"test\" == \"te\" + \"st\"  -->  "+("test" == "te" + "st"));
	
	// but .substring() is invoked at runtime, generating distinct objects
	System.out.println("7.	\"test\" == \"!test\".substring(1)  -->  "+("test" == "!test".substring(1)));
	
	// interned strings can also be recalled by calling .intern()
	System.out.println("8.	\"test\" == \"!test\".substring(1).intern()  -->  "+("test" == "!test".substring(1).intern()));  
	
}	
```

 

**执行结果**：

1.t1 == t2  --&gt;  true

2.new String("test").equals("test")  --&gt;  true

3.new String("test") == "test"  --&gt;  false

4.new String("test") == new String("test")  --&gt;  false

5."test" == "test"  --&gt;  true

6."test" == "te" + "st"  --&gt;  true

7."test" == "!test".substring(1)  --&gt;  false

8."test" == "!test".substring(1).intern()  --&gt;  true

 

在把String中的.equals()方法拿出来看一下，瞬间释然了

```
public boolean equals (Object object) {
	if (object == this) return true;
	if (object instanceof String) {
		String s = (String)object;
		if (count != s.count ||
			(hashCode != s.hashCode && hashCode != 0 && s.hashCode != 0))
				return false;
		return regionMatches(0, s, 0, count);
	}
	return false;
}
```

 原来.equals()方法这么高达上，已经兼容到了 == 的功能了哦～～

 

 找了一下intern()这个方法，代码如下

```
public native String intern();
```

 

我擦，这是个神马啊！！赶紧找找这个native是个毛！ 找到相关内容，[点击跳转](http://stackoverflow.com/questions/6101311/what-is-the-native-keyword-in-java-for)

 

以下是我理解的native，这个native，它是一个用其它语言来实现的一个方法？！和Java Native Interface一起执行。

这个native的用途就是把一个string，先从table中搜索，看看有没有和这个相同的对象，如果有，就换成是那个对象，如果没有，就是一个新的对象，这样就可以解释为什么最后那句代码"test" == "!test".substring(1).intern()是true了！