Java equals and==

await 2021-09-15 09:55:34

equals And == The difference between

== Whether two variables or instances point to the same memory space ,equals Is to determine whether the value of memory space pointed by two variables or instances is the same == Compare memory addresses , equals() Is a comparison of the contents of a string == It refers to whether the objects are the same , equals() Refers to whether the values are the same

 // Create two objects
String num1 = new String("Hello");
String num2 = new String("Hello");
//b1 yes num1 == num2
boolean b1 = num1 == num2;
//b2 yes num1.equals(num2)
boolean b2 = num1.equals(num2);
System.out.println("b1 = " + b1); //b1 = false
System.out.println("b2 = " + b2); //b2 = true
//== refer to ` object ` Are they the same? , equals() refer to ` value ` Are they the same? 

This article altogether 135 Number of words , Average reading time ≈ 1 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

Please bring the original link to reprint ,thank
Similar articles

2021-09-15