-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperClasses.java
More file actions
127 lines (101 loc) · 5.42 KB
/
Copy pathWrapperClasses.java
File metadata and controls
127 lines (101 loc) · 5.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Topic: Wrapper Classes in Java
// Notes reference: 08-wrapper-classes.md
import java.util.ArrayList;
public class WrapperClasses {
public static void main(String[] args) {
// ===========================
// PRIMITIVE → WRAPPER MAPPING
// ===========================
// byte → Byte | short → Short | int → Integer | long → Long
// float → Float | double → Double | char → Character | boolean → Boolean
System.out.println("=== Wrapper Class Basics ===");
int i = 10;
Integer intObj = Integer.valueOf(i); // primitive → wrapper (manual)
System.out.println("int: " + i + " → Integer: " + intObj);
float f = 13.6f;
Float floatObj = Float.valueOf(f);
System.out.println("float: " + f + " → Float: " + floatObj);
char c = 'a';
Character charObj = c; // autoboxing
System.out.println("char: " + c + " → Character: " + charObj);
// ===========================
// AUTOBOXING (primitive → object, automatic)
// ===========================
System.out.println("\n=== Autoboxing ===");
int num = 25;
Integer numObj = num; // autoboxing: int → Integer
System.out.println("Autoboxed: " + numObj);
ArrayList<Integer> list = new ArrayList<>();
list.add(25); // autoboxing: int → Integer (Collections require objects)
System.out.println("ArrayList add with autoboxing: " + list.get(0));
// ===========================
// UNBOXING (object → primitive, automatic)
// ===========================
System.out.println("\n=== Unboxing ===");
Integer boxed = 100;
int unboxed = boxed; // unboxing: Integer → int
System.out.println("Unboxed: " + unboxed);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(84);
int n = list2.get(0); // unboxing: Integer → int
System.out.println("Unboxed from list: " + n);
// ===========================
// WRAPPER CLASS METHODS
// ===========================
System.out.println("\n=== Wrapper Methods ===");
// 1. parseInt / parseXxx — String → primitive
int parsed = Integer.parseInt("100");
System.out.println("parseInt(\"100\"): " + parsed); // 100 (primitive)
// 2. valueOf(String) — String → wrapper object
Integer valueOfStr = Integer.valueOf("100");
System.out.println("valueOf(\"100\"): " + valueOfStr + " (object)");
// 3. valueOf(primitive) — primitive → wrapper object
Integer valueOfPrim = Integer.valueOf(10);
System.out.println("valueOf(10): " + valueOfPrim);
// 4. xxxValue() — wrapper → primitive
Integer obj = 50;
int intVal = obj.intValue();
System.out.println("intValue(): " + intVal);
// 5. toString() — wrapper → String
String s = Integer.toString(10);
System.out.println("toString(10): " + s); // "10"
// 6. compareTo() — compares two wrapper objects
Integer a = 10, b = 20;
System.out.println("10.compareTo(20): " + a.compareTo(b)); // -1 (a < b)
System.out.println("20.compareTo(10): " + b.compareTo(a)); // 1 (b > a)
System.out.println("10.compareTo(10): " + a.compareTo(a)); // 0 (equal)
// 7. equals() — compares values (not references)
Integer x = 100, y = 100;
System.out.println("\n100.equals(100): " + x.equals(y)); // true
// 8. hashCode()
Integer h = 100;
System.out.println("hashCode of 100: " + h.hashCode()); // 100 (for Integer, hashCode = value)
// 9. min, max, sum
System.out.println("\nmin(10, 20): " + Integer.min(10, 20)); // 10
System.out.println("max(10, 20): " + Integer.max(10, 20)); // 20
System.out.println("sum(10, 20): " + Integer.sum(10, 20)); // 30
// 10. decode — decimal, hex, octal string → Integer
System.out.println("\ndecode(\"0XA\"): " + Integer.decode("0XA")); // 10 (hex)
System.out.println("decode(\"010\"): " + Integer.decode("010")); // 8 (octal)
System.out.println("decode(\"10\"): " + Integer.decode("10")); // 10 (decimal)
// 11. parseBoolean
System.out.println("\nparseBoolean(\"true\"): " + Boolean.parseBoolean("true")); // true
System.out.println("parseBoolean(\"false\"): " + Boolean.parseBoolean("false")); // false
System.out.println("parseBoolean(\"Hello\"): " + Boolean.parseBoolean("Hello")); // false (anything != "true" is false)
// 12. isNaN, isInfinite
double val1 = 0.0 / 0.0;
double val2 = 10.0 / 0.0;
System.out.println("\nisNaN(0.0/0.0): " + Double.isNaN(val1)); // true
System.out.println("isInfinite(10/0.0): " + Double.isInfinite(val2)); // true
// ===========================
// INTEGER CACHE (-128 to 127)
// ===========================
System.out.println("\n=== Integer Cache Behaviour ===");
Integer p = 100, q = 100;
Integer r = 200, t = 200;
System.out.println("100 == 100: " + (p == q)); // TRUE (cached, same object)
System.out.println("200 == 200: " + (r == t)); // FALSE (outside cache, new objects)
System.out.println("200.equals(200): " + r.equals(t)); // TRUE (compares values)
// RULE: Use == for primitives, .equals() for objects
}
}