---
title: "测试对象的等价性和等值性"
author: "windy_star"
date: "2010-12-28T14:42:29Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-12-28T15:24:15Z"
id: 853875
url: "https://windy-star.iteye.com/blog/853875"
---

```java

public class qwe{
public static void main(String[] args){
Integer n1=new Integer(47);
Integer n2=new Integer(47);
System.out.println(n1==n2);
System.out.println(n1!=n2);
}
}

```



结果:
false
true

解:因为2个Integer 对象都是相同的.但是尽管对象相同然而对象的引用却不相同,而==和!=比较的是对象的引用.

```java

public class qwe{
public static void main(String[] args){
Integer n1=new Integer(47);
Integer n2=new Integer(47);
System.out.println(n1.equals(n2));
}
}

```


结果:true
解:值的对比!

"=="比较的是2个对象的地址,"equals"比较的是2个对象的内容.