Write a logic to determine and convert given primitive type to its wrapper class and return result

Category : Java | Sub Category : Data type, Variable and Arays | By Runner Dev Last updated: 2020-10-01 13:25:29 Viewed : 193


Write a logic to determine and convert given primitive type to its wrapper class and return result

public class WrapperTest {

 

            /**

             *write a logic to determine and convert given primitive type to its wrapper class and return result

             */

           public static void main(String[] args) {

                        WrapperTest test = new WrapperTest();

                        // byte data type

                        byte a = 1;

                        // short data type

                        short s = 5;

                        // int data type

                        int i = 10;

                        // long data type

                        long l = 100L;

                        // float data type

                        float f = 3.1415f;

                        // double data type

                        double d = 50.25d;

                          test.convertToWrapper(c); 

            }

public Object convertToWrapper(Object input) {

                        Object obj = input.getClass().getName();

                        System.out.println("Input ===> " + input);

                        System.out.println("Input Data Type===> " + obj);

                        if (input instanceof Byte) {

                                    // wrapping Byte object

                                    Byte byteObj = new Byte((byte) input);

                                    System.out.println("wrapping   Byte object==>" + byteObj);

                                    return byteObj;

                        }

 

                        if (input instanceof Integer) {

                                    // wrapping Integer object

                                    Integer intObj = new Integer((int) input);

                                    System.out.println("wrapping   Integer object==>" + intObj);

                                    return intObj;

                        }

                        if (input instanceof Short) {

                                    // wrapping Short object

                                    Short shortObj = new Short((short) input);

                                    System.out.println("wrapping   Short object==>" + shortObj);

                                    return shortObj;

                        }

                        if (input instanceof Long) {

                                    // wrapping Long object

                                    Long longObj = new Long((long) input);

                                    System.out.println("wrapping   Long object==>" + longObj);

                                    return longObj;

                        }

                        if (input instanceof Boolean) {

                                    // wrapping Boolean object

                                    Boolean booleanObj = new Boolean((boolean) input);

                                    System.out.println("wrapping   Boolean object==>" + booleanObj);

                                    return booleanObj;

                        }

 

                        if (input instanceof Float) {

                                    // wrapping Float object

                                    Float floatObj = new Float((float) input);

                                    System.out.println("wrapping   Float object==>" + floatObj);

                                    return floatObj;

                        }

                        if (input instanceof Character) {

                                    // wrapping char object

                                    Character charObj = new Character((char) input);

                                    System.out.println("wrapping   Char object==>" + charObj);

                                    return charObj;

                        }

                        return obj;

            }

 

}

Output:

 Input ===> A

Input Data Type===> java.lang.Character


wrapping   Char object==>A

Search
Related Articles

Leave a Comment: