File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Negative Floating point numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Negative Floating point numbers" Watch "Negative Floating point numbers" New topic

Negative Floating point numbers

charu latha
Ranch Hand

Joined: Aug 20, 2002
Posts: 67
Floating point numbers when declared

ie., float f = 3.45f;

requires the "f" or "F" appended after the numeral. Now if i declared,

float f1 = -32;

it does not require an "f" or "F" appended after the number. Can anyone explain why?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Because "-32" isn't a floating point number, it's an int, and it's legal to assign an int value to a float. If you wrote "-32." (with a decimal point at the end) you'd find that the "f" was required again.

[Jess in Action][AskingGoodQuestions]
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
an integer can be cast to a floating point number without an explicit cast and -32 is an integer.

"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
The default floating point number in Java is a double, not a float

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: Negative Floating point numbers
It's not a secret anymore!