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

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

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

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

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

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

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

"==", 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);
	}}

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;
	}}

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));
	}}

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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?What are different garbage collection algorithms in Java (Serial, Parallel, CMS, G1, ZGC)?Mar 14, 2025 pm 05:06 PM

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

What is the Java Virtual Machine (JVM) and how does it work internally?What is the Java Virtual Machine (JVM) and how does it work internally?Mar 14, 2025 pm 05:05 PM

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

How do I use Java's Nashorn engine for scripting with JavaScript?How do I use Java's Nashorn engine for scripting with JavaScript?Mar 14, 2025 pm 05:00 PM

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

How do I use Java's try-with-resources statement for automatic resource management?How do I use Java's try-with-resources statement for automatic resource management?Mar 14, 2025 pm 04:59 PM

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.

How do I use Java's enums to represent fixed sets of values?How do I use Java's enums to represent fixed sets of values?Mar 14, 2025 pm 04:57 PM

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),