Wrapper classes
---------------
A wrapper class is a class which is used to represent primitives as objects i,e we can convert primitives to objects using wrapper classes.
We need to convert primitives to objects because there are situations where we need to use only objects like while working with collections we should use only objects.
For each primitive we have one wrapper class in java.
Primitive wrapper
-------- -------
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
All the wrapper classes are available in java.lang.*
AutoBoxing and unboxing
-----------------------
Autoboxing
----------
The process of converting primitives to objects is known as boxing.
Converting primitive to object automatically is known as autoboxing.
Program
-------
class Test
{
public static void main(String args[])
{
int a=10;//primitive
Object obj=a;//object
System.out.println(obj);
}
}
Autounboxing
------------
converting object to primitive is known as unboxing.
Converting object to primitive automatically is known as Autoautoboxing.
Program
-------
class Test
{
public static void main(String args[])
{
Object obj=10;
int a=(int)obj;
System.out.println(a);
}
}
Methods of Wrappers
-------------------
parseXXX()
xxxValue()
valueOf()
toString()
Heirarchy of Wrappers
---------------------
Object
|
Number
|
Byte Short Integer Long Float Double
All warapper classes implements Serializable interface.
parseXXX()
----------
This method is used to convert a String type of data into required primitive.
Every Wrapper class is having one parseXXX() method.
public static byte parseByte(String);
public static short parseShort(String);
public static int parseInt(String);
public static float parseFloat(String);
public static double parseDouble(String);
public static long parseLong(String);
public static boolean parseBoolean(String);
mn
All the parseXXX() methods are static methods.
All the parseXXX() methods throws checked exception NumberFormatException.
CommandLine arguments
---------------------
These accepts values from command prompt.
The parameter to main() method in java is known as command line argument i,e String args[].
How to pass values from command prompt to command line arguments
----------------------------------------------------------------
syntax
------
java ClassName/fileName value1 value2 value3 .....
ex:
java Test 10 20 30
Here we should pass command line arguments from command prompt while executing a java program.
Here First parameter is copied to args[0].
Here second parameter is copied to args[1].
Here Third parameter is copied to args[2].
and so on.
i,e 10 is copied to args[0]
20 is copied to args[1]
30 is copied to args[2]
The default size of command line argument is zero.
i,e the size of String args[] is zero(0) by default.
When we pass values from command prompt automatically size increases and the size of String args[] will be no of values we are passing from command prompt.
Program
-------
class Test
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
System.out.println(a);
}
}
save as Test.java
compile
-------
javac Test.java
Run
---
java Test 10.
Program
-------
class Test
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println(c);
}
}
Note:we should pass proper string while passing to parse method otherwise it leads to NumberFormatException.
xxxValue()
----------
This method is used to convert a wrapper object to required primitive.
Methods
-------
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public boolean booleanValue()
public char charValue();
These methods are used to convert a wrapper object to its equivalent primitive automatically.They perform auto unboxing.
Program
-------
class Test
{
public static void main(String args[])
{
Integer i=new Integer(10);//Autoboxing
int j=i.intValue();//Autounboxing
System.out.println(i);
System.out.println(j);
}
}
valueOf()
---------
This method is used to perform autoboxing i,e it converts a primitive to its equivalent wrapper object.
It is represented in the following forms
public static Integer valueOf(int);
This method converts a primitive(int) to wrapper(Integer).
public static Integer valueOf(String);
This method converts a integer string to wrapper(Integer).
similarly every wrapper class has valueOf() method as shown below
public static Byte valueOf(byte);
public static Byte valueOf(String);
public static Short valueOf(short);
public static Sort valueOf(String);
public static Long valueOf(long);
public static Long valueOf(String);
public static Float valueOf(float);
public static Float valueOf(String);
public static Double valueOf(double);
public static Double valueOf(String);
public static Boolean valueOf(boolean);
public static Boolean valueOf(String);
Program
-------
class Test
{
public static void main(String args[])
{
double d=10.5;
Double val=Double.valueOf(d);
System.out.println(val);
}
}
Program
-------
class Test
{
public static void main(String args[])
{
String d="10.5";
Double val=Double.valueOf(d);
System.out.println(val);
}
}
toString()
----------
This method is used to convert any wrapper to String type
syntax
------
public String toString();
Program
-------
class Test
{
public static void main(String args[])
{
Boolean b=true;
String s=b.toString();
System.out.println(s);
}
}
Dont use methods of wrapper convert primitive to wrapper.
Note:
we can also convert primitive to wrapper object or a numeric String to a warpper by using constructors of Wrapper classes.
Constructors summary
--------------------
Byte class
Byte(byte)
Byte(String)
Short class
Short(short)
Short(String)
Integer class
Integer(int)
Integer(String)
Long class
Long(Long)
Long(String)
Float class
Float(float);
Float(double);
Float(String);
Double class
Double(double)
Double(String)
Boolean class
Boolean(boolean)
Boolean(String)
Character class
Character(char)
---------------
A wrapper class is a class which is used to represent primitives as objects i,e we can convert primitives to objects using wrapper classes.
We need to convert primitives to objects because there are situations where we need to use only objects like while working with collections we should use only objects.
For each primitive we have one wrapper class in java.
Primitive wrapper
-------- -------
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
All the wrapper classes are available in java.lang.*
AutoBoxing and unboxing
-----------------------
Autoboxing
----------
The process of converting primitives to objects is known as boxing.
Converting primitive to object automatically is known as autoboxing.
Program
-------
class Test
{
public static void main(String args[])
{
int a=10;//primitive
Object obj=a;//object
System.out.println(obj);
}
}
Autounboxing
------------
converting object to primitive is known as unboxing.
Converting object to primitive automatically is known as Autoautoboxing.
Program
-------
class Test
{
public static void main(String args[])
{
Object obj=10;
int a=(int)obj;
System.out.println(a);
}
}
Methods of Wrappers
-------------------
parseXXX()
xxxValue()
valueOf()
toString()
Heirarchy of Wrappers
---------------------
Object
|
Number
|
Byte Short Integer Long Float Double
All warapper classes implements Serializable interface.
parseXXX()
----------
This method is used to convert a String type of data into required primitive.
Every Wrapper class is having one parseXXX() method.
public static byte parseByte(String);
public static short parseShort(String);
public static int parseInt(String);
public static float parseFloat(String);
public static double parseDouble(String);
public static long parseLong(String);
public static boolean parseBoolean(String);
mn
All the parseXXX() methods are static methods.
All the parseXXX() methods throws checked exception NumberFormatException.
CommandLine arguments
---------------------
These accepts values from command prompt.
The parameter to main() method in java is known as command line argument i,e String args[].
How to pass values from command prompt to command line arguments
----------------------------------------------------------------
syntax
------
java ClassName/fileName value1 value2 value3 .....
ex:
java Test 10 20 30
Here we should pass command line arguments from command prompt while executing a java program.
Here First parameter is copied to args[0].
Here second parameter is copied to args[1].
Here Third parameter is copied to args[2].
and so on.
i,e 10 is copied to args[0]
20 is copied to args[1]
30 is copied to args[2]
The default size of command line argument is zero.
i,e the size of String args[] is zero(0) by default.
When we pass values from command prompt automatically size increases and the size of String args[] will be no of values we are passing from command prompt.
Program
-------
class Test
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
System.out.println(a);
}
}
save as Test.java
compile
-------
javac Test.java
Run
---
java Test 10.
Program
-------
class Test
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println(c);
}
}
Note:we should pass proper string while passing to parse method otherwise it leads to NumberFormatException.
xxxValue()
----------
This method is used to convert a wrapper object to required primitive.
Methods
-------
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public boolean booleanValue()
public char charValue();
These methods are used to convert a wrapper object to its equivalent primitive automatically.They perform auto unboxing.
Program
-------
class Test
{
public static void main(String args[])
{
Integer i=new Integer(10);//Autoboxing
int j=i.intValue();//Autounboxing
System.out.println(i);
System.out.println(j);
}
}
valueOf()
---------
This method is used to perform autoboxing i,e it converts a primitive to its equivalent wrapper object.
It is represented in the following forms
public static Integer valueOf(int);
This method converts a primitive(int) to wrapper(Integer).
public static Integer valueOf(String);
This method converts a integer string to wrapper(Integer).
similarly every wrapper class has valueOf() method as shown below
public static Byte valueOf(byte);
public static Byte valueOf(String);
public static Short valueOf(short);
public static Sort valueOf(String);
public static Long valueOf(long);
public static Long valueOf(String);
public static Float valueOf(float);
public static Float valueOf(String);
public static Double valueOf(double);
public static Double valueOf(String);
public static Boolean valueOf(boolean);
public static Boolean valueOf(String);
Program
-------
class Test
{
public static void main(String args[])
{
double d=10.5;
Double val=Double.valueOf(d);
System.out.println(val);
}
}
Program
-------
class Test
{
public static void main(String args[])
{
String d="10.5";
Double val=Double.valueOf(d);
System.out.println(val);
}
}
toString()
----------
This method is used to convert any wrapper to String type
syntax
------
public String toString();
Program
-------
class Test
{
public static void main(String args[])
{
Boolean b=true;
String s=b.toString();
System.out.println(s);
}
}
Dont use methods of wrapper convert primitive to wrapper.
Note:
we can also convert primitive to wrapper object or a numeric String to a warpper by using constructors of Wrapper classes.
Constructors summary
--------------------
Byte class
Byte(byte)
Byte(String)
Short class
Short(short)
Short(String)
Integer class
Integer(int)
Integer(String)
Long class
Long(Long)
Long(String)
Float class
Float(float);
Float(double);
Float(String);
Double class
Double(double)
Double(String)
Boolean class
Boolean(boolean)
Boolean(String)
Character class
Character(char)
No comments:
Post a Comment