Home > Java > javaTutorial > What does == mean in java

What does == mean in java

下次还敢
Release: 2024-05-07 04:09:18
Original
677 people have browsed it

In Java, the == operator is used to compare objects for equality, it checks whether two objects refer to the same memory location: Value comparison: Basic types compare values. Reference comparison: Reference types compare memory addresses. Self-reference: Two references to the same object return true. Null values: Two null values ​​return true, otherwise false.

What does == mean in java

Meaning of == in Java

In Java, the == operator is used to compare two Object equality. It checks whether the two operands point to the same memory location, i.e. whether they refer to the same object.

Detailed explanation:

  • Value comparison: If the two objects are basic types (such as int, double, boolean), then == compares their values.
  • Reference comparison: If two objects are reference types (such as String, Object), then == compares the memory addresses they point to.
  • Self-reference: If two objects are two references to the same object, == returns true.
  • null value: If both objects are null, == returns true. Otherwise, == returns false as long as one object is not null.

Example:

<code class="java">int a = 5;
int b = 5;
System.out.println(a == b); // 输出 true:值相等

String name1 = "John";
String name2 = "John";
System.out.println(name1 == name2); // 输出 true:引用同一对象

String name3 = new String("John");
System.out.println(name1 == name3); // 输出 false:引用不同的对象

Object obj1 = null;
Object obj2 = null;
System.out.println(obj1 == obj2); // 输出 true:都是 null</code>
Copy after login

Note: The

  • == operator only checks the object's memory addresses, their contents are not compared.
  • To compare the actual contents of objects, you can use the equals() method.
  • Avoid using == for complex objects that require comparing contents rather than memory addresses.

The above is the detailed content of What does == mean in java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template