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 Float Primitive Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float Primitive" Watch "Float Primitive" New topic

Float Primitive

Kanchan K Bulbule

Joined: Jul 19, 2005
Posts: 20
When we assign float f=5.00 We get a compiler error as default type of floating point number is double and cast is neccessary .

Y a cast is not needed when we say float f = 35/6 ;

Some help needed on this..

Thanks in advance.

Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Kanchan,

In the above stmt only integer division will be carried out not float division.bcoz both 35 & 6 will be taken as integers & result will be
35 /6 = 5(digits after decimal point will be truncated).so no cast is needed here.

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hello Kanchan,

I hope that answer by priya helps you..! To add more..!

The resultant of the division is integer that is 5 which can be assigned to float.
If the question was like 35.0/6.0 then u needed a explicit cast as the the default type of answer is 5.0 double in this case.


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Karthik Hariharan
Ranch Hand

Joined: Aug 14, 2005
Posts: 31
floats, if not explicityly casted, need to be declared with the keyword 'f', correct?

float num = 5.0f (this is legal)

Are floats used very often? I can see from a memory conservation point of view why they would be used, but I don't see them too often in practice.
I agree. Here's the link:
subject: Float Primitive
It's not a secret anymore!