-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparison.java
More file actions
22 lines (17 loc) · 1.16 KB
/
Copy pathComparison.java
File metadata and controls
22 lines (17 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package Strings;
public class Comparison {
public static void main(String[] args) {
/*
== (Reference comparison) => Checks if both variables point to the same memory location
.equals() (Content comparison) => Checks if the actual string values are equal
*/
String a = "Apple";
String b = "Apple";
System.out.println(a == b); // checks whether a & b points to the same memory
String c = new String("Apple"); // we explicitly create a new reference variable with the same value of object
System.out.println(c == a); // now when we check whether a & c points to the same reference it returns false even if they have same value
System.out.println(c == b); // now when we check whether a & c points to the same reference it returns false even if they have same value
System.out.println(a.equals(b)); // .equals() check the value at the reference variable if it is same no matter the memory address
System.out.println(a.equals(c)); // .equals() check the value at the reference variable if it is same no matter the memory address
}
}