how data types in Java are converted
data type casting is the process of changing a value from one type to another. For example, String data 456 can be converted to a numeric type, and any type of data can be converted to String.
if you convert from a low-precision data type to a high-precision data type, you will never overflow and will always succeed. However, the conversion of high-precision data type to low-precision data type will inevitably result in information loss and possibly failure.
data type conversion can be done in two ways, implicit and explicit.
the first type is implicit conversion, which is the conversion from a low-level type to a high-level type without any operation, the system will perform automatically. This type conversion is called implicit conversion. The following basic data types involve data conversions, which do not include logical types or character types. These types are classified in order of accuracy from low to high:
byte< short< int< long< float< Double
int x = 100;
float y = x; The output y value is 100.0
implicit casting requires certain rules to resolve when to convert one type of data to another.
the general rule of implicit type conversion below:
shows type conversion:
the second type is display type conversion, also known as display type conversion to cast. Display type conversion must be used when assigning the value of a high-precision variable to a low-precision variable.
may cause accuracy to disappear when performing display type conversions
( type name) value to be converted
int a = (int)45.23;
int a = (int)45.23;
long b = (long)45.6f;
int c = (int) ‘d’;
output result a=45;
b = 45;
c = 100; Note: when assigning integers to a byte short int long type of variable, the values of these types must not be exceeded, or you must enter a cast. For example:
byte a = (byte)129;
reference: what are the two types of data conversion in Java?
- Explicit and implicit conversion of Java data type
- The precision of decimal calculation with double and float in Java is not accurate
- C# Bug Fixed InvalidCastException: Cannot cast from source type to destination type.
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘；
- JAVA 8: How to Convert List to Map
- [How to Fix]java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x98\x82\xF0\x9F…’
- How to get the current time in java time string
- Twitter’s distributed self increasing ID algorithm snowflake (Java version)
- [Javac compilation exception] javac compilation prompts that the package in jdk cannot be found error: package jdk.internal.org.objectweb.asm does not exist and error: cannot find symbol
- Tensorflow Error TypeError: Fetch argument XXXX has invalid typeXXXX，must be a string or Tensor
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- JAVA: Controller exception handling
- Maven project running servlet jump JSP error: HTTP status 500 – unable to compile class for JSP
- Java Running Error: Could not find or load main class
- Exception information: jsonmappingexception: out of start_ ARRAY token
- [Solved] Specified key was too long; max key length is 767 bytes
- keytool error: java.lang.Exception: Input not an X.509 certificate
- Java error: unable to find or load main class (package name in source file)
- Java parsing xml file encounters special symbols & will be abnormal solutions
- Idea error: (44,22) Java: constant string too long