Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Negative Floating point numbers

 
charu latha
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly....
an integer can be cast to a floating point number without an explicit cast and -32 is an integer.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default floating point number in Java is a double, not a float
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic