Skip to content

Commit 68c194a

Browse files
committed
Wrapper Concept Added
1 parent 30c42b0 commit 68c194a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,73 @@ class Sample{
342342

343343
Note : Further Explanation is been given in the code file above with name "var1.java".
344344

345+
## Wrapper Class
346+
347+
A Wrapper class in Java is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
348+
349+
### Needs of Wrapper Class :
350+
351+
- They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
352+
- The classes in java.util package handles only objects and hence wrapper classes help in this case also.
353+
- Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
354+
- An object is needed to support synchronization in multithreading.
355+
356+
### Major Concepts :
357+
358+
- Autoboxing
359+
- Unboxing
360+
361+
#### Autoboxing
362+
363+
The automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double, etc.
364+
365+
Syntax Code :
366+
367+
```
368+
import java.util.*;
369+
370+
class Sample{
371+
372+
public static void main(String[] a){
373+
374+
char ch1 = 'a'; // variable declaration
375+
Character c1 = ch1; // autoboxing
376+
377+
ArrayList<Integer> arrlist = new ArrayList<Integer>();
378+
arrayList.add(22); // autoboxing
379+
380+
System.out.println(arrayList.get(0));
381+
}
382+
}
383+
```
384+
385+
#### Unboxing
386+
387+
It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc.
388+
389+
Syntax Code :
390+
391+
```
392+
import java.util.*;
393+
394+
class Sample{
395+
396+
public static void main(String[] a){
397+
398+
Character c1 = 'a'; // variable declaration
399+
char ch1 = c1; // unboxing
400+
401+
ArrayList<Integer> arrList = new ArrayList<Integer>();
402+
arrList.add(22);
403+
404+
int num = arrList.get(22);
405+
System.out.println(num);
406+
}
407+
}
408+
```
409+
410+
Note : Proper Code will be given in the file name as "wrap.java".
411+
345412
### Social Media Links :
346413

347414
- [Instagram Page](https://www.instagram.com/coding.needs/)

wrap.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
3+
// custom wrapper class
4+
class numbers{
5+
6+
// some private members
7+
private int num1 = 0;
8+
private int num2 = 0;
9+
10+
// member methods
11+
public void insert(int n1){
12+
this.num2++;
13+
if(n1 <= this.num1)
14+
return;
15+
this.num1 = n1;
16+
}
17+
18+
public int first(){
19+
return this.num1;
20+
}
21+
22+
public int second(){
23+
return this.num2;
24+
}
25+
}
26+
27+
class wrap{
28+
public static void main(String[] a){
29+
30+
// Wrapper Classes and their objects
31+
char ch = 'a';
32+
33+
// Conversion [Autoboxing]
34+
Character z = ch;
35+
36+
// Checking in the output
37+
System.out.println("Character Wrapper class : " + z);
38+
System.out.println("Character normal wrapper class : " + ch);
39+
40+
ArrayList<Integer> arrayList = new ArrayList<Integer>();
41+
arrayList.add(20); // autoboxing
42+
43+
// Printing the values now
44+
System.out.println(arrayList.get(0));
45+
46+
// Unboxing using the get() method
47+
int num = arrayList.get(0);
48+
System.out.println("Value of it after unboxing : " + num);
49+
50+
// Using the classes here
51+
numbers num11 = new numbers();
52+
num11.insert(12);
53+
num11.insert(11);
54+
num11.insert(23);
55+
56+
// Method names and uses here
57+
System.out.println("Top or first level number : " + num11.first());
58+
System.out.println("Last or second level number : " + num11.second());
59+
60+
}
61+
}

0 commit comments

Comments
 (0)