Java - String == (Reference Comparison) vs .equals() (Value Comparison)

Published: 13 April 2025
on channel: Khan's Tech Lab
21
2

In Java, == and .equals() serve different purposes:
== (Reference Comparison): This checks whether two variables point to the same memory location. If two objects have the same reference, == returns true.
.equals() (Value Comparison): This method is used to compare the actual content of objects. For example, when comparing two String objects, .equals() checks whether their text values are the same.

Example:
String a = new String("hello");
String b = new String("hello");

System.out.println(a == b); // false (different memory locations)
System.out.println(a.equals(b)); // true (same content)

Subscribe for more such videos.


On this page of the site you can watch the video online Java - String == (Reference Comparison) vs .equals() (Value Comparison) with a duration of hours minute second in good quality, which was uploaded by the user Khan's Tech Lab 13 April 2025, share the link with friends and acquaintances, this video has already been watched 21 times on youtube and it was liked by 2 viewers. Enjoy your viewing!