Write one expression each showing Implicit and Explicit type casting in Java. Tell fast anyone
Answers
Answer:
Type conversion in Java with Examples
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.
Widening or Automatic Type Conversion
Example:
class Test
{
public static void main(String[] args)
{
int i = 100;
// automatic type conversion
long l = i;
// automatic type conversion
float f = l;
System.out.println("Int value "+i);
System.out.println("Long value "+l);
System.out.println("Float value "+f);
}
}
Output:
Answer:
Programming is playing around with data. In Java, there are many data types. Most of the times while coding, it is necessary to change the type of data to understand the processing of a variable and this is called Type Casting. In this article, I will talk about the fundamentals of Type Casting in Java.