Quote:
Java type casting error
It is a runtime exception that occurs when the application code attempts to cast an object of class type A into another class type B.
But, in your code above, I don't see that concern. We can convert
int
to
double
in java using assignment operator.
Read[
^].
Quote:
Java performs implicit conversion or widening conversion when both data types involved in conversion are compatible, or the value of a smaller data type is assigned to a bigger data type. Double is bigger than int as it requires more storage space; hence, int values are implicitly converted to double by Java.
public static void main(String []args){
System.out.println("Hello, World!");
int myInt = 56;
double myDouble = myInt;
System.out.println (myInt);
System.out.println (myDouble);
}
If you reverse the datatypes, you would surely get a incompatible types error. Example:
double myInt = 56;
int myDouble = myInt;
System.out.println (myInt);
Another
example[
^]:
Object o = new Object();
String s = (String)o;