forked from rahulXbarnwal/JavaTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBasics.java
More file actions
36 lines (28 loc) · 1.11 KB
/
StringBasics.java
File metadata and controls
36 lines (28 loc) · 1.11 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
36
package learnString;
public class StringBasics {
public static void main(String[] args) {
String name = "Rahul";
String sameName = "Rahul";
String newName = new String("Rahul");
System.out.println(name);
System.out.println(newName);
if(name == sameName) {
System.out.println("Both are same");
}
if(name == newName) {
System.out.println("Both are same");
} else {
System.out.println("Both are not same");
}
// while using ==, it's not the object values which gets checked,
// it's references which gets checked
// in primitive data types, == checks the values even if the values are getting stored separately
// in non-primitive, == checks if references are pointing to same address or not
// so the question is how do we check values
// .equals() function
// .equalsIgnoreCase() -> ignores the case of the string and then compare
if(name.equals(newName)) {
System.out.println("name and newName have same values");
}
}
}