Wrapper Classes

For each primitive data type there is Class and which is called as wrapper class.

Wrapper classes allows us to use of polymorphism.

Tutorial url: Wrapper Classes

Example Program:

public class WrapperClassExample {
    
    public void testPoly(Object s){
        if(s instanceof Integer){
            System.out.println("It is an Integer");
        }else if(s instanceof Character){
            System.out.println("It is a Character");
        }else if(s instanceof Boolean){
            System.out.println("It is a Boolean");
        }else if(s instanceof Float){
            System.out.println("It is a Float");
        }else if(s instanceof Double){
            System.out.println("It is a Double");
        }else if(s instanceof Long){
            System.out.println("It is a Long");
        }else if(s instanceof Short){
            System.out.println("It is a Short");
        }else if(s instanceof Byte){
            System.out.println("It is a Byte");
        }else{
            System.out.println("Given object is not a object of Wrapper class");
        }
    }
}


public class WrapperClassTest {
    public static void main(String[] args) {
        int a = 50;
        char b = 'G';
        long c = 100;
        boolean d = true;
        short e = 20;
        byte f = 10;
        float g = 10.5f;
        double h = 13.0308340;
        WrapperClassExample wce = new WrapperClassExample();
        wce.testPoly(a);
        wce.testPoly(b);
        wce.testPoly(c);
        wce.testPoly(d);
        wce.testPoly(e);
        wce.testPoly(f);
        wce.testPoly(g);
        wce.testPoly(h);
    }
}

OutPut:
It is an Integer
It is a Character
It is a Long
It is a Boolean
It is a Short
It is a Byte
It is a Float
It is a Double

short, int and long primitive data type

short, int and long are similar data types with different range.

Example Program: When we override a method which takes arguments short, int and long like below. Always by default int argument method will be called. if we want to call a method from other argument type like long or short we need to do type casting.

public class IntegerDataTypes {
    public void testInt(short s){
        System.out.println("short method is called "+s);
    }
    
    public void testInt(int i){
        System.out.println("int method is called "+i);
    }
    
    public void testInt(long l){
        System.out.println("long method is called "+l);
    }
}

 

public class TestIntegerDataType {
    public static void main(String args[]){
        IntegerDataTypes in = new IntegerDataTypes();
        int s = 25;
        in.testInt((short)s);
        in.testInt(s);
        in.testInt((long)s);
    }
}

Output:

short method is called 25
int method is called 25
long method is called 25

Autoboxing and Unboxing

Autoboxing: Converting primitive data type into Object of corresponding Wrapper class.

Unboxing: Converting Wrapper class object into corresponding primitive data type.

Example for Autoboxing:

AutoBoxing class: Define a method which receives an Integer arguement.

public class AutoBoxing {
    
    public void testAuto(Integer a){
        System.out.println("Wrapper class Integer "+ a);
    }
}

AutoAndUnBoxingTest class: When we call testAuto method with passing int argument compiler will convert it into Integer class.

public class AutoAndUnBoxingTest {
    public static void main(String args[]){
        AutoBoxing ab = new AutoBoxing();
        Integer a = 50000;
        int b = 50;
        ab.testAuto(a);
        ab.testAuto(b);
    }
}

Output:

Wrapper class Integer 50000
Wrapper class Integer 50

Example for Unboxing:

Unboxing Class: Define testAuto method which receives int argument.

public class Unboxing {
       public void testAuto(int a){
          System.out.println("Primitive type int "+ a);
      }
}

AutoAndUnBoxingTest Class: When we call testAuto method with passing Integer argument compiler will convert it into int.

public class AutoAndUnBoxingTest {
    public static void main(String args[]){
        Unboxing ub = new Unboxing();
        Integer a = 50000;
        int b = 50;
        ub.testAuto(a);
        ub.testAuto(b);
    }
}

Output:

Primitive type int 50000
Primitive type int 50

 

break and continue

break:

break is used for exit from the loop based on some condition.

break can be used with labeled and unlabeled.

Example:

public class BreakLearn {
    private static final String[] ARR = {"India","Pakistan", "Austrelia", "Srilanka", 
            "South Africa"};
    public static void main(String arg[]){
        Search:
        for(int i = 0; i <= ARR.length; i++){
            System.out.println(ARR[i]);
            if("Austrelia".equals(ARR[i])){
                System.out.println("Entered into if condition");
                break Search;
            }
        }
    }
}

Continue:

Continue is used for stop the current execution and start the execution from the start of the loop or from the label specified .

Example:

private static final String[] ARR = { "India", "Pakistan", "Austrelia", "Srilanka", "South Africa" };

    public static void main(String arg[]) {
//        Search: 
            for (int i = 0; i < ARR.length; i++) {
            System.out.println(ARR[i]);
            if ("Austrelia".equals(ARR[i])) {
                System.out.println("Entered into if condition");
                continue;
            }
            System.out.println("Santhosh");
        }
    }