Typecasting in java
java
Solution
`Integer.parseInt` doesn't use casting but rather a simple algorithm to interpret the digits in the string as a number. Casting is done by the JVM directly on the primitive value or the compiler on the object reference. It can turn `4.5` into `4` (type conversion as it changes the underlying value) and `ArrayList` into `List` (reference casting as it doesn't modify the instance), but it cannot parse or format numbers natively.
Problem
``` int n ; n= (int)( javax.swing.JOptionPane.showInputDialog(null,"enter a 3 digit no.")); ``` Why does the above gives error[required int, found string] and the below one works fine ? ``` int n ; n= Integer.parseInt( javax.swing.JOptionPane.showInputDialog(null,"enter a 3 digit no.")); ```