Explicit and implicit conversion of Java data type

implicit conversion:
two byte data type operation, the result is an int data type.


(byte, short) < int< long< float< Double;
an operation involving only a byte, short, the result will be converted to an int;
if other operations are involved, convert to a large data type according to the relational result above.
below are some code examples

public class TypeWay {
    public static void main(String[] args) {
        byte b1 = 1, b2 = 2;
        System.out.println(getType(b1+b2));
    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

运行结果:class java.lang.Integer

a byte data type and a short data type operation result in an int data type.

public class TypeWay {
    public static void main(String[] args) {
        byte b1 = 1;
        short b2 = 2;
        System.out.println(getType(b1+b2));
    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果为:class java.lang.Integer

the following code means: an int and long data type operation, the result of the long data type.

public class TypeWay {
    public static void main(String[] args) {
        int b1 = 1;
        long b2 = 2;
        System.out.println(getType(b1*b2));
        System.out.println("go and try");
    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果为:class java.lang.Long

an int data type and a double data type operation result doule data type.

public class TypeWay {
    public static void main(String[] args) {
        int b1 = 1;
        double b2 = 2.0;
        System.out.println(getType(b1*b2));
        System.out.println("go and try");
    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果为:class java.lang.Double

: a float and a double operation results in a double operation.

public class TypeWay {
    public static void main(String[] args) {
        float b1 = 1.0f;
        double b2 = 2.0;
        System.out.println(getType(b1*b2));
        System.out.println("go and try");
    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果为:class java.lang.Double

conversion
directly precedes the data with the corresponding function of your conversion data type. However, there is no support for converting high-level data types to low-level data types.

here are some examples:
high data types cannot be converted to high data types.

public class TypeWay {
    public static void main(String[] args) {
        int b1 = 1;
        int b2 = 2;
        System.out.println(getType((byte)b1*b2));

    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果是:class java.lang.Integer

low data type converted to high data type:

public class TypeWay {
    public static void main(String[] args) {
        int b1 = 1;
        int b2 = 2;
        System.out.println(getType((long)b1*b2));

    }
    public static String getType(Object a){
        return a.getClass().toString();
    }
}

结果是:class java.lang.Long

Read More: