When you assign value of one data type to another, the two types might not be compatible with each other. If the data types are compatible, then Java will perform the conversion automatically known as Automatic Type Conversion and if not then they need to be casted or converted explicitly. For example, assigning an int value to a long variable.
Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically converted. This happens when:
- The two data types are compatible.
- When we assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible with each other but no automatic conversion is supported from numeric type to char or boolean. Also, char and boolean are not compatible with each other.
Int value 100 Long value 100 Float value 100.0
Narrowing or Explicit Conversion
If we want to assign a value of larger data type to a smaller data type we perform explicit type casting or narrowing.
- This is useful for incompatible data types where automatic conversion cannot be done.
- Here, target-type specifies the desired type to convert the specified value to.
char and number are not compatible with each other. Let’s see when we try to convert one into other.
7: error: incompatible types: possible lossy conversion from int to char ch = num; ^ 1 error
How to do Explicit Conversion?
Double value 100.04 Long value 100 Int value 100
While assigning value to byte type the fractional part is lost and is reduced to modulo 256(range of byte).
Conversion of int to byte. i = 257 b = 1 Conversion of double to byte. d = 323.142 b = 67
Type promotion in Expressions
While evaluating expressions, the intermediate value may exceed the range of operands and hence the expression value will be promoted. Some conditions for type promotion are:
- Java automatically promotes each byte, short, or char operand to int when evaluating an expression.
- If one operand is a long, float or double the whole expression is promoted to long, float or double respectively.
Result = 626.7784146484375
Explicit type casting in Expressions
While evaluating expressions, the result is automatically updated to larger data type of the operand. But if we store that result in any smaller data type it generates compile time error, due to which we need to type cast the result.
NOTE- In case of single operands the result gets converted to int and then it is type casted accordingly, as in the above example.
This article is contributed by Apoorva singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Primitive data type vs. Object data type in Java with Examples
- Widening Primitive Conversion in Java
- ArrayList to Array Conversion in Java : toArray() Methods
- Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
- Image Processing in Java | Set 4 (Colored image to Negative image conversion)
- Image Processing in Java | Set 5 (Colored to Red Green Blue Image Conversion)
- Image Processing in Java | Set 6 (Colored image to Sepia image conversion)
- Conversion of Array To ArrayList in Java
- Data Conversion Using valueOf() method in Java
- Conversion of Java Maps to List
- Array to ArrayList Conversion in Java
- Base conversion in Java
- FileStore type() method in Java with Examples
- Comparison of boolean data type in C++ and Java
- Is an array a primitive type or an object in Java?
- Type Erasure in Java
- Local Variable Type Inference or LVTI in Java 10
- Basic Type Base64 Encoding and Decoding in Java
- How to create a Java HashMap of user defined class type?
- Finding Data Type of User Input using Regular Expression in Java
Improved By : vrk-19