File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Primitive test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Primitive test" Watch "Primitive test" New topic

Primitive test

Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Can any one please let me know why compile time error is not generated when a long is assigned to a float ? Float is of size 32 bits but long is of size 64 bits.

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Annette Sovereign

Joined: Jul 12, 2005
Posts: 26
Hi there,

This is to do with primitive widening conversion (google-whack it).

You only lose precision (i.e. lose significant bits) and in Java, that's acceptable.

Hope this helps.


-- just trying to get one foot on the ladder
Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
float is 32 bits and long is 64 bits but
storage mechanism is not same



float f=Long.MAX_VALUE;
System.out.println(f); // exponent is 18
System.out.println(Float.MAX_VALUE); // exponent is 38

float uses IEEE754 storage mechanism hence can store number larger in magnitude than long.

word magnitude is more important.
hope this helps and correct me if I am wrong
I agree. Here's the link:
subject: Primitive test
It's not a secret anymore!