-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperExamples.java
More file actions
115 lines (86 loc) · 3.37 KB
/
WrapperExamples.java
File metadata and controls
115 lines (86 loc) · 3.37 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
package com.nishith.java.wrapper;
public class WrapperExamples {
public static void main(String[] args) {
// Boolean,Byte,Character,Double,Float,Integer,Long,Short
// boolean,byte,char ,double,float,int ,long,short
// Wrapper classes are final
Integer number = new Integer(55);// int
Integer value = 10;
value = value + 1;
Integer number2 = new Integer("55");// String
Float number3 = new Float(55.0);// double argument
Float number4 = new Float(55.0f);// float argument
Float number5 = new Float("55.0f");// String
Character c1 = new Character('C');// Only char constructor
// Character c2 = new Character(124);//COMPILER ERROR
Boolean b = new Boolean(true);
// "true" "True" "tRUe" - all String Values give True
// Anything else gives false
Boolean b1 = new Boolean("true");// value stored - true
Boolean b2 = new Boolean("True");// value stored - true
Boolean b3 = new Boolean("False");// value stored - false
Boolean b4 = new Boolean("SomeString");// value stored - false
b = false;
// Wrapper Objects are immutable (like String)
int i = 5;
i = 6;
i = 7;
//Immutability
//Wrapper Classes and String
Integer integer = 6;
integer = 7;
//#########
//# 6 #
//#########
//#########
//# 7 #
//#########
// valueOfMethods
// Provide another way of creating a Wrapper Object
Integer seven = Integer.valueOf("111", 2);// binary 111 is converted to
// 7
Integer hundred = Integer.valueOf("100", 10);// 100 is stored in
// variable
// xxxValue methods help in creating primitives
//Integer integer = Integer.valueOf(57);
int primitive = seven.intValue();// 57
float primitiveFloat = seven.floatValue();// 57.0f
Float floatWrapper = Float.valueOf(57.0f);
int floatToInt = floatWrapper.intValue();// 57
float floatToFloat = floatWrapper.floatValue();// 57.0f
// parseXxx methods are similar to valueOf but they
// return primitive values
int sevenPrimitive = Integer.parseInt("111", 2);// binary 111 is
// converted to 7
int hundredPrimitive = Integer.parseInt("100");// 100 is stored in
// variable
//Creates new Integer object
Integer wrapperEight = new Integer(8);
// Normal static toString method
System.out.println(Integer.toString(wrapperEight));// String Output : 8
// Overloaded static toString method : 2nd parameter : radix
System.out.println(Integer.toString(wrapperEight, 2));// String Output :
// 1000
// static toXxxString methods. Xxx can be Hex,Binary,Octal
System.out.println(Integer.toHexString(wrapperEight));// String Output
// :8
System.out.println(Integer.toBinaryString(wrapperEight));// String
// Output
// :1000
System.out.println(Integer.toOctalString(wrapperEight));// String Output
// :10
// Auto Boxing
Integer ten = new Integer(10);
ten++;// allowed. Java does had work behing the screen for us
// Two wrapper objects created using new are not same object
Integer nineA = new Integer(9);
Integer nineB = new Integer(9);
System.out.println(nineA == nineB);// false
System.out.println(nineA.equals(nineB));// true
// Two wrapper objects created using boxing are same object
Integer nineC = 9;
Integer nineD = 9;
System.out.println(nineC == nineD);// true
System.out.println(nineC.equals(nineD));// true
}
}