1

It is defined in the type promotion rule for java that long data can be casted into float data type. how it is possible??

for example :

 class casting{

    void show(float a){
   System.out.println("float");
   }


 public static void main(String args[]){
   casting obj = new casting();
  obj.show(10l);
  }

}

but here output is flaot How the long data which is 8 byte are converted into float data(4 byte)?

Cs ツ
  • 515
  • 4
  • 16
  • What is your question. There is no "?" mark in your posting. – Jayamohan Mar 11 '13 at 06:18
  • my question is how this conversion is possible. in float type date(4 byte) long data(8 byte) is stored how..?? – Cs ツ Mar 11 '13 at 06:20
  • This [reply](http://stackoverflow.com/a/1293836/1060037) should tell you why. – Jayamohan Mar 11 '13 at 06:24
  • 1
    can you help yourself to follow java naming conventions? Use pascal case while writing class name and use camel case while writing method names. – AmitG Mar 11 '13 at 06:25
  • refer this post http://stackoverflow.com/questions/1293819/why-does-java-implicitly-without-cast-convert-a-long-to-a-float this should make it clear. – kunal Mar 11 '13 at 06:28
  • It's possible because floating-point types are inherently inexact, and Java doesn't mind losing the precise value when you do that type of conversion. The conversion preserves the magnitude, however. – Boann Mar 11 '13 at 10:48

1 Answers1

0

I modified your program as follows. long is a 64bit signed datatype and the maximum value is 9223372036854775807l. when you convert that to float, it prints as 9.223372E18. float is 32 bit single precision data type. Hope this helps

public class casting {

    void show(float a) {
        System.out.println("float is " + a);
    }

    public static void main(String args[]) {
        casting obj = new casting();
        obj.show(9223372036854775807l);
    }

}
Joe2013
  • 1,007
  • 1
  • 9
  • 24