You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67Lines changed: 67 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -342,6 +342,73 @@ class Sample{
342
342
343
343
Note : Further Explanation is been given in the code file above with name "var1.java".
344
344
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".
0 commit comments