ClassCastException, casting Integer to Double

后端 未结 10 1972
我寻月下人不归
我寻月下人不归 2020-12-24 05:33
ArrayList marks = new ArrayList();
Double sum = 0.0;
sum = ((Double)marks.get(i));

Everytime I try to run my program, I get a ClassCastException th

10条回答
  •  温柔的废话
    2020-12-24 05:46

    Changing an integer to a double

    int abc=12; //setting up integer "abc"
    
    System.out.println((double)abc); 
    

    The code will output integer "abc" as a double, which means that it will display as "12.0". Notice how there is a decimal place, indicating that this precision digit has been stored.

    Same with double if you want to change it back,

    double number=13.94;
    
    System.out.println((int)number); 
    

    This code will print on one line, "number" as an integer. The output will be "13". Notice that the value has not been rounded up, the data has actually been omitted.

提交回复
热议问题