It's not a secret anymore!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Negative Floating point numbers" Watch "Negative Floating point numbers" New topic
Author

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
exactly....
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
Sheriff

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


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Negative Floating point numbers
 
Similar Threads
Question from JQ+
java rule round up #113
precision
general declaration
Assignment to float