Repository navigation
Expand file tree
/
Copy pathunit2_1_1.java
More file actions
executable file
·48 lines (38 loc) · 2.01 KB
/
Copy pathunit2_1_1.java
File metadata and controls
executable file
·48 lines (38 loc) · 2.01 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
public class unit2_1_1 {
public static void main(String[] args) {
// Example 1
String a = "bicycle";
String b = a;
System.out.println("a: " + a + ", b: " + b);
// Example 2
String c = new String("bicycle");
String d = c;
System.out.println("c: " + c + ", d: " + d);
System.out.println("a ref: " + a.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(a)));
System.out.println("b ref: " + b.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(b)));
System.out.println("c ref: " + c.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(c)));
System.out.println("d ref: " + d.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(d)));
// Print null values
String color = null;
System.out.println("color: " + color);
// Changing String (or object)
String word1 = "Ball";
System.out.println("word1: " + word1 + ", word1 ref: " + a.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(word1)));
String word2 = word1 + "oon";
System.out.println("word2: " + word2 + ", word2 ref: " + a.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(word2)));
// Compare Strings
System.out.println("a == b? : " + (a == b));
System.out.println("a == c? : " + (a == c));
System.out.println("c == d? : " + (c == d));
System.out.println("a.equals(b)? : " + (a.equals(b)));
System.out.println("a.equals(c)? : " + (a.equals(c)));
System.out.println("c.equals(d)? : " + (c.equals(d)));
// Substring
// Examples:
// "hamburger".substring(4, 8) returns "urge" =: 4 < index < 8
// "smiles".substring(1, 5) returns "mile" =: 1 < index < 5
//
System.out.println("a.substring(0,3): " + a.substring(0, 3));
System.out.println("a.substring(3): " + a.substring(3));
}
}