[Java] Difference between == and equals

Programming study diary

November 10, 2020 A brief memorandum on how to use the equals method for comparing strings and ==.

Difference between == and epuals

I will get into the main subject immediately. Use the == operator when comparing whether two values are equal in a primitive type such as int or char. In case of reference type such as String type, compare with equals method. When comparing with == in the case of reference type such as String type, compare whether the reference destination is the same </ b>, and not compare whether the reference destination value is the same. Let's actually run it with the sample code.

Sample code


public static void main(String[] args) {
	//Initialize the String type variables str1 and str2 with the same string
	String str1 = "hello";
	String str2 = "hello";

	if(str1 == str2)
		System.out.println("str1=str2 (==Compare with) ");
	else
		System.out.println("str1≠str2 (==Compare with) ");

	//Add the same string
	str1 += "!";
	str2 += "!";

	if(str1 == str2)
		System.out.println("str1=str2 (==Compare with) ");
	else
		System.out.println("str1≠str2 (==Compare with) ");
	
	if(str1.equals(str2))
		System.out.println("str1=str2 (Compare with equals) ");
	else
		System.out.println("str1≠str2 (Compare with equals) ");
}

Execution result


str1=str2 (==Compare with) 
str1≠str2 (==Compare with) 
str1=str2 (Compare with equals) 

When str1 and str2 of String type variables are initialized with the same string and the same string is added, false is returned when compared with==, and true is returned when compared with the equals method. I'm returning. The String type is treated as a pseudo primitive type at the time of declaration and initialization, but is used as a reference type when a character string is added. Therefore, even if the values are the same, the reference destination is different, so comparing with == returns false.

References

Comparison of basic type and comparison of reference type [Quick learning Java] Difference between ”==” and “equals” (explains how to deny)

Recommended Posts

[Java] Difference between == and equals
[Java] Difference between Hashmap and HashTable
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Difference between final and Immutable in Java
[For beginners] Difference between Java and Kotlin
Difference between vh and%
[Java] Difference between Intstream range and rangeClosed
Difference between i ++ and ++ i
Difference between int and Integer in Java
[Java] Understand the difference between List and Set
Difference between next () and nextLine () in Java Scanner
[Java] Difference between "final variable" and "immutable object"
Difference between product and variant
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
[Java] HashCode and equals overrides
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
java Generics T and? Difference
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
== and equals
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] Difference between equals and == in a character string that is a reference type
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Differences between "beginner" Java and Kotlin
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[Java beginner] == operator and equals method
[Java] Relationship between H2DB and JDBC
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Understanding equals and hashCode in Java
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())