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.
implicit cast:
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
example:
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.
syntax :
may cause accuracy to disappear when performing display type conversions
( type name) value to be converted
example:
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?
Read More:
- Explicit and implicit conversion of Java data type
- [Solved] Mapreducer Class Conversion error: java.lang.ClassCastException
- How to compare the time in two TimeStamp format?
- JAVA: How to Read JSON Format Data (Web Game Development)
- [Solved] java.lang.IllegalAccessError: class org.springframework.data.redis.core.$ Proxy237 cannot access its superinterface org.springframework.data.redis.core.RedisConnectionUtils$RedisConnectionProxy
- Java.sql.sqlexception: unable to read more data from socket
- The problem of date format conversion between springboot and front end
- Resin Failed to Startup Error: ERROR: transport error 202: bind failed: Address already in use: JVM_Bind Listenin (Two Resins in One PC)
- Java uses class array to report error Exception in thread “main” java.lang.NullPointerException solution
- [Solved] error: invalid operands of types ‘QLabel*‘ and ‘void‘ to binary ‘operator>
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java Internal error in the mapping processor java.lang.NullPointerException
- [Solved] java: Internal error in the mapping processor: java.lang.NullPointerException
- C++ String case conversion and transform, tower, upper, usage
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- java: java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x590
- Mybatis Error: Error instantiating class bakou.entity.Person with invalid types () or value)
- The precision of decimal calculation with double and float in Java is not accurate
- Defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configuration.
- [Solved] Consider defining a bean of type ‘org.springframework.data.redis.core.RedisTemplate‘ in your configu