I have a method which takes object as an input and if the input is instanceOF Long then converting the value to double value. Below is the code :
public static void main(String[] args) {
Long longInstance = new Long(15);
Object value = longInstance;
convertDouble(value);
}
static double convertDouble(Object longValue){
double valueTwo = (double)longValue;
System.out.println(valueTwo);
return valueTwo;
}
but when I am executing the above code I am getting below exception :
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Double
at com.datatypes.LongTest.convertDouble(LongTest.java:12)
at com.datatypes.LongTest.main(LongTest.java:8)
Kindly let me know why its giving me exception.
But if directly try to cast Long object into double then there is no Exception of classCast is coming.
Long longInstance = new Long(15);
double valueOne = (double)longInstance;
System.out.println(valueOne);
This is confusing.