-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStringFunctionCompare.java
More file actions
35 lines (35 loc) · 1.46 KB
/
StringFunctionCompare.java
File metadata and controls
35 lines (35 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class StringFunctionCompare {
public static void main(String[] args) {
String s1 = "tony";
String s2 = "tony";
String s3 = new String("tony");
if(s1==s2){
System.out.println("Strings s1 and s2 are equal");
}
else{
System.out.println("Strings s1 and s2 are not equal");
}
if(s1==s3){
System.out.println("Strings s1 and s3 are equal");
}
else{
System.out.println("Strings s1 and s3 are not equal");
}
// --------------------------------
// //the output :
// Strings s1 and s2 are equal
// Strings s1 and s3 are not equal
// this is the output because when we dont use the new keyword both s1 and s2 act as pointers and they point to the data "tony" stored at a
// certain (location)
// Here basically both s1 and s2 are pointing to same location so result is true;
// But for s1 and s3 it is false because new keyword creates a new location and stores data
// Key Point to observe is that:
// "==" sign compares that locaation address and not the data int
// Inorder to compare the data irrespective of location we use ".equals()" function.
// --------------------------------
if(s1.equals(s3))
System.out.println("Strings s1 and s3 are equal");
else
System.out.println("Strings s1 and s3 are not equal");
}
}