- Using wrapper classes we can wrap the concept of object on the top of the primitive data.
- And represent the primitive data values in the form of its equivalent objects.
- In order to represent the 8 primitive data values in the form of its equivalent objects we have 8 wrapper classes they are
- Byte
- Short
- Integer
- Long
- Double
- Float
- Character
- Boolean
Uses of wrapper classes:
- Using wrapper classes we can represent the primitive data values in form of its equivalent objects.
- many time it would be required to represent the primitive data values in the form of objects
ex: Collections, Session - Wrapper classes supports the functions using which we can get back the sequence of primitive data characters present inside the string class object back into its corresponding data value.
- It has two constructors
- Accepts the values of primitive data
- Accepts the object of String class
- Syntax:
Integer num1=new Integer(12);
Integer num2=new Integer("12"); - To get the value form it
int n=num2.intValue();
Integer wrapper class Example program:
package com.instanceofjavaforus;
public class wrapperDemo {
public static void main(String args[]){
public class wrapperDemo {
public static void main(String args[]){
int x=12;
Integer num1=new Integer(x);
System.out.println(num1);
num1=num1+1;
System.out.println(num1);
String str="12";
Integer num2= new Integer(str);
System.out.println(num2);
int num3=num2.intValue();
System.out.println(num3);
}
Integer num1=new Integer(x);
System.out.println(num1);
num1=num1+1;
System.out.println(num1);
String str="12";
Integer num2= new Integer(str);
System.out.println(num2);
int num3=num2.intValue();
System.out.println(num3);
}
}
Output:
12
13
12
12
13
12
12
Example program 2 on Integer wrapper class:
package com.instanceofjavaforus;
public class Wraperdemo2 {
public static void main(String args[]){
String s1="12";
Integer i=new Integer(s1);
int x=i.intValue();
System.out.println(x);
String s2="abc";
Integer i2=new Integer(s2);// no compilation error but run time error will come
String s3=" 123";
Integer i3= new Integer(s3);// run time error
i3= new Integer(s3.trim());// recommended to trim
}
public class Wraperdemo2 {
public static void main(String args[]){
String s1="12";
Integer i=new Integer(s1);
int x=i.intValue();
System.out.println(x);
String s2="abc";
Integer i2=new Integer(s2);// no compilation error but run time error will come
String s3=" 123";
Integer i3= new Integer(s3);// run time error
i3= new Integer(s3.trim());// recommended to trim
}
}
- String s2="abc"
Integer i2=new Integer(s2);// no compilation error but run time error will come. - It accepts String class object but the string should contain only integer otherwise the constructor dont know how to represent characters gives error.
- String a=" 123";
- here some times there may be a chance of having spaces in a string so it is recommended to trim the object
- i3= new Integer(s3.trim());// recommended to trim
Program 3 :
package com.instanceofjavaforus;
public class wraperDemo3{
public static void main(String args[]){
String s1="12";
Integer i=new Integer(s1);
int x=i.intValue();
System.out.println(x);
String s2="123";
int y= Integer.parseInt(s2.trim());
}
}
Output:
12
No comments