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
div>
Read More:
- Conversion of two data types in Java
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- [Solved] Mapreducer Class Conversion error: java.lang.ClassCastException
- Defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configuration.
- Java Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu
- JAVA: How to Read JSON Format Data (Web Game Development)
- [Solved] Scala error: type mismatch; found : java.util.List[?0] required: java.util.List[B]
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- [Solved] swagger Docmentation Access Error: Illegal DefaultValue 1024 for parameter type integer, java.lang.NumberFormatException
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- Java back end receives localdatetime type parameter
- Java.sql.sqlexception: unable to read more data from socket
- The problem of date format conversion between springboot and front end
- [Solved] Java.lang.IllegalStateException: Type handler was null on parameter mapping for property ‘__frch_it
- [Solved] No validator could be found for constraint ‘javax.validation.constraints.NotBlank’ validating type ‘java.lang.String’
- C# Bug Fixed InvalidCastException: Cannot cast from source type to destination type.
- C++ String case conversion and transform, tower, upper, usage
- [Solved] Hibernate Error: java.lang.StackOverflowError at java.lang.Integer.toString(Integer.java:402)
- [Solved] Spring Error: This application has no explicit mapping for /error