forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjects.java
More file actions
59 lines (43 loc) · 1.65 KB
/
Objects.java
File metadata and controls
59 lines (43 loc) · 1.65 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
50
51
52
53
54
55
56
57
58
59
/**
* Demonstrates uses of objects and wrappers.
*/
public class Objects {
public static void main(String[] args) {
// primitives vs objects
int number = -2;
char symbol = '!';
char[] array = {'c', 'a', 't'};
String word = "dog";
String word1 = new String("dog"); // creates a string object
String word2 = "dog"; // implicitly creates a string object
// the null keyword
String name0 = null;
int[] combo = null;
// System.out.println(name0.length()); // NullPointerException
// System.out.println(combo[0]); // NullPointerException
// Strings are immutable
String name = "Alan Turing";
String upperName = name.toUpperCase();
name.toUpperCase(); // ignores the return value
System.out.println(name);
name = name.toUpperCase(); // references the new string
System.out.println(name);
String text = "Computer Science is fun!";
text = text.replace("Computer Science", "CS");
// Wrapper classes
Integer i = Integer.valueOf(5);
System.out.println(i.equals(5)); // displays true
Integer x = Integer.valueOf(123);
Integer y = Integer.valueOf(123);
if (x == y) { // false
System.out.println("x and y are the same object");
}
if (x.equals(y)) { // true
System.out.println("x and y have the same value");
}
String str = "12345";
int num = Integer.parseInt(str);
num = 12345;
str = Integer.toString(num);
}
}