Boxing and unboxing in java

  • The concept of representing the primitive data values in the form of its equivalent and corresponding wrapper class objects is known as "boxing"

                Integer x= new Integer(3);
               Double d= new Double(1.2);
  • package com.instanceofjavaforus;

    public class wraperDemo3 {
    public static void main(String args[]){
              String s1="12";
            Integer i=new Integer(s1);
            System.out.println(i);
     
        }
    }
     
  • The concept of getting back actual primitive data values present inside wrapper class objects into its corresponding primitive data value known as "unboxing"
          Integer x= new Integer(3);
          int y= x.intValue();
  • 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);
              }
              }
  • The concept of JVM automatically representing the primitive data values in the form of its equivalent and corresponding wrapper class objects is known as "Autoboxing".

                Integer x=3;
               Double d= 1.3;
  • package com.instanceofjavaforus;
    public class wraperDemo3 {
    public static void main(String args[]){
                  
                  Integer x=3;
                 Double d= 1.3;
                System.out.println(x);
                System.out.println(d);

                  }
            }
     
  • The concept of getting back actual primitive data values present inside wrapper class objects into its corresponding primitive data value automatically by JVM  known as "Autounboxing"
          Integer x= new Integer(3);
          int y= x;
  • package com.instanceofjavaforus;
    public class wraperDemo3 {
    public static void main(String args[]){
                  
                  Integer x= new Integer(3);
                     int y= x;
                System.out.println(x);
                System.out.println(y);
                  }
            }

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

Print numbers in pyramid shape


  1. package com.instanceofjava;
  2.  
  3. public class Pyramidshape {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.   int num=10;
  8.  
  9.   for (int i = 1; i < num; i++) {
  10.  
  11.    for (int j = 1; j<=num-i;j++) {
  12.  
  13.     System.out.print(" ");
  14.    } 

  15.   for (int k = 1; k <= i; k++) {
  16.    System.out.print(""+k+" ");
  17.   } 

  18.   for (int l =i-1; l >0; l--) {
  19.    System.out.print(""+l+" ");
  20.   } 

  21.    System.out.println();
  22.   }
  23.  
  24. }



 Output:

  1.  1
  2.  1 2 1 
  3.  1 2 3 2 1
  4.  1 2 3 4 3 2 1

Sort object using comparator

  1. package com.instanceofjava;
  2.  
  3. public class Employee 
  4. {
  5.  
  6.  int id;
  7.  String name;
  8.  
  9.  public Employee(int id, String name) {
  10.  
  11.   this.id=id;
  12.   this.name=name;
  13.  
  14.  }
  15.  
  16. public int getId() {
  17.  
  18. return id;
  19.  
  20. }
  21.  
  22.  public void setId(int id) {
  23.   this.id = id;
  24.  }
  25.  
  26.  public String getName() {
  27.   return name;
  28.  }
  29.  
  30. public void setName(String name) {
  31.  
  32.   this.name = name;
  33.  
  34. }
  35.  
  36. }


  1. class MyEmpComp implements Comparator<Employee >
  2.  
  3.  @Override
  4.  public int compare(Employee e1, Employee e2) {
  5.  
  6.         if(e1.getName() < e2.getName()){
  7.             return 1;
  8.         } else {
  9.             return -1;
  10.         }
  11.  
  12. }
  13.  
  14. }




  1. package com.oops;
  2.  
  3. import java.util.ArrayList;  
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. import java.io.*;  
  8.  
  9. class Main{  
  10.  
  11. public static void main(String args[]){  
  12.  
  13. ArrayList al=new ArrayList();  
  14.  
  15. al.add(new Employee(101,"Indhu"));  
  16. al.add(new Employee(106,"Sindhu"));  
  17. al.add(new Employee(105,"Swathi"));
  18.   
  19. Collections.sort(al,MyEmpComp );  
  20.  
  21. Iterator itr=al.iterator();  
  22.  
  23. while(itr.hasNext()){   
  24.  
  25. Employee st=(Employee)itr.next();  
  26. System.out.println(st.id +" "+st.name );  
  27.  
  28.   }
  29.  
  30.   
  31. }
  32.  
  33.  


Output:
  1. Indhu
  2. Swathi
  3. Sindhu



Sort object using comparable interface

  1. package com.instanceofjava;
  2.  
  3. import java.util.Comparable;
  4.  
  5. public class Employee implements Comparable<Employee> {
  6.  
  7.  int id;
  8.  String name;
  9.  
  10.  public Employee(int id, String name) {
  11. this.id=id; 
  12. this.name=name;
  13.  }
  14.  
  15.  public int getId() {
  16. return id;
  17.  }
  18.  
  19.  public void setId(int id) {
  20.   this.id = id;
  21.  }
  22.  
  23.  public String getName() {
  24. return name;
  25.  }
  26.  
  27.  public void setName(String name) {
  28.   this.name = name;
  29.  }
  30.  
  31.  @Override
  32.  public int compareTo(Employee e) {
  33.  
  34.  Integer i= this.getId();
  35.  Integer j=e.getId();
  36.  
  37.      if (this.getId() == e.getId())
  38.        return 0;
  39.       if (this.getId() < e.getId())
  40.         return 1;
  41.       if (this.getId() > e.getId())
  42.        return -1;
  43.       return 0;
  44. }
  45. }

  1. package com.oops;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Iterator;
  5. import java.io.*;
  6.   class Main{  
  7.  
  8. public static void main(String args[]){
  9.  
  10.  ArrayList al=new ArrayList();  
  11.  
  12.  al.add(new Employee(101,"Indhu"));
  13. al.add(new Employee(106,"Sindhu"));
  14.  al.add(new Employee(105,"Swathi"));  
  15. Collections.sort(al);
  16.  
  17. Iterator itr=al.iterator();   
  18.  
  19. while(itr.hasNext()){
  20.  
  21.    Employee st=(Employee)itr.next();
  22.    System.out.println(st.id +" "+st.name );   
  23.  
  24.   } 
  25. }  
  26.  
  27. }  
Output:
  1. Indhu 
  2. Swathi 
  3. Sindhu

 

Method overriding example program


  1. package com.oops; 
  2.  
  3. class A
  4.  
  5. void msg(){
  6.  
  7.  System.out.println("Hello");
  8.  
  9.  
  10. }

  1. class B extends A
  2. {
  3.  
  4.  void msg(){
  5.  
  6. System.out.println("Welcome");
  7.  
  8. }
  9.  
  10. public static void main(String args[])
  11.  
  12.     A a=new A();
  13.     B b=new B();
  14.      A obj=new B(); 
  15.  
  16.    System.out.println(a.msg());   //   Hello
  17.         System.out.println(obj.msg());  // Welcome
  18.         System.out.println(b.msg());   //Welcome
  19.  
  20. }  





Output:
  1. Hello
  2. Welcome 
  3. Welcome



Select Menu