Computer Science, asked by umeshmehar682, 5 hours ago

What is the error in the following program? double a=12.5;
int b=a;
System.out.println (b);​

Answers

Answered by anindyaadhikari13
15

\textsf{\large{\underline{Solution}:}}

Given Co‎de:

double a = 12.5;

int b = a;

System.out.println(b);

Corrected Co‎de:

double a = 12.5;

int b = (int)a;

System.out.println(b);

Output: 12

\textsf{\large{\underline{Explanation}:}}

  • 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.

\textsf{\large{\underline{Learn More}:}}

Hierarchy of Data Types:

\begin{array}{|c}\rm byte\\ \rm char\\ \rm short\\ \rm int\\ \rm long\\ \rm float\\ \rm double\end{array}\downarrow

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 jayadityarajani
0

Explanation:

Answer is

double a = 12.5;

int b =(int) a ;

System.out.println(b);

OUTPUT : 12

Similar questions