Home > Java > JavaBase > body text

You must know the difference between '==' and equals() in java

coldplay.xixi
Release: 2021-01-18 17:20:37
forward
2717 people have browsed it

Today we discuss the difference between "==" and equals() in Java

You must know the difference between '==' and equals() in java

Learn Recommended: java basics

==: Relational operator

   在基本数据类型中比较两个值的内容是否相等
   
   在引用类型型中比较的是两个对象的地址是否相等
Copy after login

equals() is the Object class The specific usage of the method

1.基本数据类型无法使用equals()方法

2.在引用类型中若是没有重写Object类时,则默认使用Object类的equals方法,则仍然
利用“==”比较两个对象的内存地址,若是重写Object类的equals方法,则调用子类重写后		  
的方法可以进行内容或值的比较

例如字符串中,equals()方法重写Object类的equals()方法,从而可以直接比较字符串的
内容。(可以自行观察字符串equals()源代码,此处不细讲)
Copy after login

"==", the code is as follows:

package Test01;//"=="的具体实例public class Demo02 {
	public static void main(String[] args) {
		int a1=10;
		int a2=10;
		String str1=new String("aaa");  //在堆中开辟了新的空间,从而内存地址不相等
		String str2=new String("aaa");
		//比较基本数据类型
		System.out.println(a1==a2);
		//引用数据类型
		System.out.println(str1==str2);
	}}
Copy after login

Result:
You must know the difference between == and equals() in java


The usage of equals, the code As follows:
1. Create an object, call the equals() method of the Object class, and compare the memory address

package Test01;//equals()的具体实现package Test01;//Object类的equals()方法public class Demo02{
	public static void main(String[] args) {
		Demo03 demo=new Demo03("aaa");
		Demo03 demo1=new Demo03("aaa");
		System.out.println(demo.equals(demo1));
		
	}}class Demo03 {
	public String str;
	public Demo03(String str){
		this.str=str;
	}
	public String getStr() {
		return str;
	}}
Copy after login

The result is:
You must know the difference between == and equals() in java
2. For example, in a string, equals()The method overrides the equals() method of the Object class, so that the contents of strings can be directly compared.

package Test01;//equals()的在字符串中的方法重写public class Demo02{
	public static void main(String[] args) {
		String str1=new String("aaa");
		String str2=new String("aaa");
		System.out.println(str1.equals(str2));
	}}
Copy after login

The result is:
You must know the difference between == and equals() in java
Summary:

1. "==" compares values ​​in basic data types and compares memory addresses in reference types

2, equals()Cannot be used in basic data types
Reference type: If objects are compared directly, the equals() method in the Object class is called. If you want to compare contents, You can override the equals() method of the Object class.
(equals() method of String class)

Today is what I want to share with you. If there are any mistakes, please correct me, thank you

Related Free learning recommendations: java basic tutorial

The above is the detailed content of You must know the difference between '==' and equals() in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template