example of a explicit conversion type in java
Answers
Answered by
5
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
mark it as a brainliest answer
follow me also
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
mark it as a brainliest answer
follow me also
kanika58:
dis is vomit
Answered by
2
Explicit type conversion in Java:
It is performed explicitly That means if you are performing the typecasting then it is called as Explicit conversion.
Here is the example without Typecasting:
public static void main(String[] args)
{
int i = 100;
byte b;
b = i;
System.out.println("Demo");
}
Output: Compilation Error.
Because: Datatype mismatch
Now,
Here is the example of Explicit typecasting:
public static void main(String[] args)
{
int i = 100;
byte b;
b=(byte)i;
System.out.println(("Demo");
}
Hope this helps!
It is performed explicitly That means if you are performing the typecasting then it is called as Explicit conversion.
Here is the example without Typecasting:
public static void main(String[] args)
{
int i = 100;
byte b;
b = i;
System.out.println("Demo");
}
Output: Compilation Error.
Because: Datatype mismatch
Now,
Here is the example of Explicit typecasting:
public static void main(String[] args)
{
int i = 100;
byte b;
b=(byte)i;
System.out.println(("Demo");
}
Hope this helps!
Similar questions
Accountancy,
8 months ago
English,
8 months ago
Math,
8 months ago
Math,
1 year ago
Geography,
1 year ago