Wrapper classes in java
15:45:00
For each and every fundamental data type there exist a pre-defined
class, Such predefined class is known as wrapper class. The purpose of wrapper
class is to convert numeric string data into numerical or fundamental data.
Why use wrapper classes ?
We know that in java whenever we get input form user, it is in the
form of string value so here we need to convert these string values in
different different datatype (numerical or fundamental data), for this
conversion we use wrapper classes.
Why do we need wrapper classes in java?
Do we really need wrapper classes or wrapper data types in java? Can’t
we use primitive data types directly instead of wrapper classes? Actually we
can use primitive types in many places but not all. In some cases we can’t use
primitive values as is, so wrapper classes are needed/required. For example, if
we need to store numbers in a collection, we can’t use primitives because
collections such as List, Set, and Map need objects as their elements. In such
cases you must use wrapper classes.
What is the difference between primitive data types
and wrapper classes in java?
The main difference between primitive data types and
wrapper classes (wrapper types) in java is that the primitive types can be used
as raw data for operations such as arithmetic, logical, etc. and wrapper
classes acts as data holders for these primitive data types.
Important note: The primitive data type values will
be stored in Stack Memory whereas wrapper class objects (like any other java
objects) are stored in Heap Memory.
Use of wrapper class in java
Although, the primary purpose/use of wrapper classes
in java is to wrap primitive values, there are few other uses/advantages of
wrapper classes. One feature is to use constants defined by the wrapper class,
such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the
data type, and the other application is to use wrapper class methods for
converting values to and from other primitive data types, for converting to and
from strings, and for converting between number systems (decimal, octal,
hexadecimal, binary).
Are wrapper classes mutable in java?
A big “No”. Like Strings, wrapper classes in java
such as Integer, Float, Long, and Double are also “immutable”. Once created,
the numbers wrapped within the objects of wrapper classes in java.lang package
can’t be changed. Read Why Strings & Wrapper classes are immutable and
final in java for more details.
Real Usage of Wrapper Class in Java is Coversion Technoique.
To learn Java conversion Please click here
Real Usage of Wrapper Class in Java is Coversion Technoique.
To learn Java conversion Please click here
0 comments