What is the error in the following program? double a=12.5;
int b=a;
System.out.println (b);
Answers
Answered by
15
Given Code:
double a = 12.5;
int b = a;
System.out.println(b);
Corrected Code:
double a = 12.5;
int b = (int)a;
System.out.println(b);
Output: 12
- Here, we are assigning a value of larger data type to a smaller data type (i.e. double to int). So, error occurs while executing the program.
- We can use explicit type casting feature to remove the error.
Hierarchy of Data Types:
The hierarchy indicates the increasing order of data types. When two data of different types are combined, the result is converted to highest data type.
Example: Char + Int = Int.
Answered by
0
Explanation:
Answer is
double a = 12.5;
int b =(int) a ;
System.out.println(b);
OUTPUT : 12
Similar questions