» » Wrapper Classes


  • 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
    1. Byte
    2. Short
    3. Integer
    4. Long
    5. Double
    6. Float
    7. Character
    8. 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
    1. Accepts the values of primitive data
    2. 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[]){
    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);
}
}
Output:
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
    }
}

  • 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

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu