2

i need your help in following code snap.

public class Ex1 {
    public static void main(String[] args) {
        Number number = new Float(1000000.211);
        System.out.println(number);
        System.out.println(number.byteValue());
        System.out.println(number.shortValue());
        System.out.println(number.intValue());
        System.out.println(number.longValue());
        System.out.println(number.floatValue());
        System.out.println(number.doubleValue());
    }
}

output:

1000000.2

64

16960

1000000

1000000

1000000.2

1000000.1875

Question: So I create instance of Float and store into Number class reference variable and also able to call methods of Number class. when I call doubleValue() method that time output value is changed after fraction.

I also read out implementation of doubleValue() method in Float class.

public double doubleValue() {
    return (double)value;
}

They just convert type cast the value into double then why output value is differ.

Please give your suggestions and views.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436

0 Answers0