Explain the importance of wrapper classes in computer java language??
Answers
Answer:
Wrapper class-
As the name suggests, wrapper classes wraps the datatype and gives it an object
appearance.
It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
Explanation:
Importance of wrapper classes-
1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
2) To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
Example-
Observe the following conversion.
int k = 100;
Integer it1 = new Integer(k);
The int data type k is converted into an object, it1 using Integer class. The it1 object can be used in Java programming wherever k is required an object.
The following code can be used to unwrap (getting back int from Integer object) the object it1.
int m = it1.intValue();
System.out.println(m*m); // prints 10000
intValue() is a method of Integer class that returns an int data type.
Wrapper class is a special class in Java.
Hli