Implicitly Typecasting in JavaThe process of converting one type of object and variable into another type is referred to as Typecasting. When the conversion automatically performs by the compiler without the programmer's interference, it is called implicit type casting or widening casting. In implicit typecasting, the conversion involves a smaller data type to the larger type size. For example, the byte datatype implicitly typecast into short, char, int, long, float, and double. The process of converting the lower data type to that of a higher data type is referred to as Widening. In the same expression, when two different types of variables are involved:
There are certain rules which are used by the compiler to determine which variable's datatype needs to be converted. The rules which are followed by the compiler are as follows:
The automatic type conversion is possible of one type of numeric data type into other types. The automatic conversion from numeric to char or Boolean is not possible or can say not compatible. Let's take an example to understand how implicit typecasting is done in Java: ImplicitTypecastingExample.java Output: Explanation In the above code, we have taken a variable p of type byte and initialized 12 into it. We then use the print statement to print the value of p. We perform the implicit typecasting by passing the value of a variable into a different type of variable.
Next TopicLegacy Class in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India