Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Wrapper classes in JAVA

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 187
    Comment on it

    Wrapper classes are used to convert any data type into an object.
    a wrapper class gets enclose around a data type and appears as an object.

    for example: int k = 100; Integer it1 = new Integer(k);
    The int data type k is converted into an object, it1 using Integer class.
    to unwrap the object it1.
    int m = it1.intValue(); System.out.println(m*m); // prints 10000

    And now some programs

    (1) Wrapper class Example: Primitive to Wrapper:

    public class WrapperExample1{  
    public static void main(String args[]){  
    //Converting int into Integer  
    int a=20;  
    Integer i=Integer.valueOf(a); 
    Integer j=a;  
    
    System.out.println(a+" "+i+" "+j);  
    }}  
    

    OUPUT
    20 20 20
    (2) Wrapper class Example: Wrapper to Primitive:

    public class WrapperExample2{    
    public static void main(String args[]){    
    //Converting Integer to int    
    Integer a=new Integer(3);    
    int i=a.intValue();
    int j=a;
    System.out.println(a+" "+i+" "+j);    
    }}    
    <b>OUPUT</b><br/>
    

    3 3 3

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: