Java Puzzle – Magic of Auto-boxing

 

Auto-boxing is introduced in JDK 5.0.  it converts primitive data variable to corresponding wrapper classes and vice versa without any explicit efforts.

For example int to Integer and Integer to int.

Here is the one program using auto-boxing feature

  1: class AutoBoxingTest 
  2: { 
  3:     public static void main(String[] args) 
  4:     {    
  5:         boolean b1=true, b2=true; 
  6:         System.out.println("boolean Result  = "+ compare(b1,b2)+"\n");        
  7:         System.out.println("Int Result  = "+ compare(10,10)+"\n"); 
  8:         System.out.println("String Result  = "+ compare("ABC","ABCE")+"\n"); 
  9:         System.out.println("float Result  = "+ compare(10.00f,10f)+"\n"); 
 10:         System.out.println("Mixed Result  = "+ compare(10l,10l)+"\n"); 
 11:         System.out.println("Mixed[long,int] Result  = "+ compare(10l,10)+"\n"); 
 12:         System.out.println("Mixed[float,int] Result  = "+ compare(10.00f,10)+"\n"); 
 13:     } 
 14: 
 15:     public static boolean compare(Object obj1, Object obj2){ 
 16:         if(obj1!=null && obj2!=null){ 
 17:             System.out.println("Auto boxing class = [ "+ obj1.getClass().getName()+", " +obj2.getClass().getName()+" ]"); 
 18:             return obj1.equals(obj2); 
 19:         } 
 20:         return false; 
 21:     } 
 22: }
 23: 
 24: 


What do you think, what would be output or compilation error ?





Output :



Auto boxing class = [ java.lang.Boolean, java.lang.Boolean ]

boolean Result  = true



Auto boxing class = [ java.lang.Integer, java.lang.Integer ]

Int Result  = true



Auto boxing class = [ java.lang.String, java.lang.String ]

String Result  = false



Auto boxing class = [ java.lang.Float, java.lang.Float ]

float Result  = true



Auto boxing class = [ java.lang.Long, java.lang.Long ]

Mixed Result  = true



Auto boxing class = [ java.lang.Long, java.lang.Integer ]

Mixed[long,int] Result  = false



Auto boxing class = [ java.lang.Float, java.lang.Integer ]

Mixed[float,int] Result  = false



Output completed (0 sec consumed)

Comments

  1. ---------- Output ----------
    Auto boxing class = [ java.lang.Boolean, java.lang.Boolean ]
    boolean Result = true

    Auto boxing class = [ java.lang.Integer, java.lang.Integer ]
    Int Result = true

    Auto boxing class = [ java.lang.String, java.lang.String ]
    String Result = false

    Auto boxing class = [ java.lang.Float, java.lang.Float ]
    float Result = true

    Auto boxing class = [ java.lang.Long, java.lang.Long ]
    Mixed Result = true

    Auto boxing class = [ java.lang.Long, java.lang.Integer ]
    Mixed[long,int] Result = false

    Auto boxing class = [ java.lang.Float, java.lang.Integer ]
    Mixed[float,int] Result = false


    Output completed (0 sec consumed)

    ReplyDelete

Post a Comment

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example