-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringsNotes.java
More file actions
49 lines (34 loc) · 1.44 KB
/
StringsNotes.java
File metadata and controls
49 lines (34 loc) · 1.44 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
37
38
39
40
41
42
43
44
45
46
47
48
49
public class StringsNotes {
public static void main(String[] args) {
//Reference Types (such as String) store the address of their object
//Comparing two String objects using == compares their address, not
//not their contents and you rarely if ever want to compare two String addresses
String example = "demo"; //Example of a String literal
//Use String .equals() and equalsIgnoreCase() methods to compare contents
String a = "Dog";
String b = "Cat";
if(a.equals(b)) { // EXAMPLE
System.out.println("Srings are equal");
} else {
System.out.println("String are not equal");
}
if(a.equalsIgnoreCase(b)) { // EXAMPLE, but ignores case
System.out.println("Srings are equal");
} else {
System.out.println("String are not equal");
}
//null = is a String reference type (no object)
//null String is NOT the same as ""
//A NullPointerException will occur by attempting to call methods on a null String reference
String s = null;
System.out.println(s.toUpperCase()); // NullPointerException
String m = "";
System.out.println(s.toUpperCase()); // Prints ""; no problem)
//to call the address of the String m
System.out.println(Integer.toHexString(m.hashCode()));
String name = "Brady"; //String name = new String("Brady")
name = "Lily"; //name = new String("Lily")
String nameNew = "Brady";
String nom = "Brady"; // String nom = nameNew ("Brady"), same memory address
}
}