Write about Wrapper Classes in Java?
https://www.computersprofessor.com/2016/11/write-about-wrapper-classes-in-java.html
Wrapper classes:
1. Vectors can’t handle primitive
data types like in , char , float , long , double.
2. vector handle only objects so
wrapper classes are used to convert primitive data types in to object type.
Simple type
int
char
double
float
long
|
Wrapper type
Integer
Character
Double
Float
Long
|
Converting Primitive Data type in to Objects:
Integer
x= new Integer(i) --> integer to integer object
Float
f= new Float(i) --> float to float object
Long
l=new Long(i) --> long to long object.
Ex :
int i=10;
Integer x =new Integer(i)
Where i=10 is integer and x is
object.
Converting Objects to Primitive Data type:
int I = IntVal . intValue (); object to primitive
integer
float f =FloatVal . floatValue ();
object to primitive float
long l= LongVal . longValue ();
object to primitive long
Ex :
Integer x= new Integer(10);
int i= x. intValue();
Converting numbers to string using to string( ):
S1 = Integer . toString
(i); primitive integer to string
S1 = Float . toString
(f); primitive float to string
Ex :
int i=10;
S= Integer . toString(i);
Converting String
Objects to Numeric Objects:
I= Integer. valueOf(s); convert
string to integer objects.
F= Float. valueOf (s); convert string to float object.
Converting Strings to primitive numbers : In this case we can use passing methods.
int i= Integer. parseInt(str); --> convert string to integer
float f= Float.parseFloat(str); -->convert string to float
long l=Long.parseLong(str); -->convert string to long