• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Why can I assign a float variable with `= 9;` but not `= 9.0;`?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I've recently been working with float types in java, and I discovered that..

doesn't work because the compiler sees it as assigning a double to a float type variable.

However, for some reason..

does work even though you would think it be like assigning an int to a float type variable.

I know you can do..

so it seems like constant ints are just treated differently in java for some reason.

Why can't I assign a float variable with a double type constant?

I know I can also do..
 
Bartender
Posts: 1899
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, floating point numbers are double in Java. In order to store them into float variable, you need to cast them explicitly or suffix with ‘f’ or ‘F’ (as you did at the bottom of your post).

HTH

-- mike
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

java tutorial wrote:Floating-Point Literals
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).



You're litterally trying to put a 64bit data into a 32 bit data type. That's why it's complaining.
 
Phil Patterson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.

I didn't know that it was a matter of the size of the datatype.

I guess you can do..



because the way the data is represented in the computer is exactly the same as if the given literal were a double?

Otherwise, I assume java would have to have some sort of duck-typing functionality.

EDIT:

Well, since a double is larger than an int, I assume the computer would just place more zeros in front to fill out the space?

 
Sheriff
Posts: 17287
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phil Patterson wrote:
Well, since a double is larger than an int, I assume the computer would just place more zeros in front to fill out the space?



The mechanism you refer to is called a widening promotion in Java.

It's actually related to the reason float num = 9.0; won't work. Since floating point literals in Java are double and double is wider than float, then you need a narrowing conversion instead, to make a double fit into a float which Java won't do automatically.
 
Junilu Lacar
Sheriff
Posts: 17287
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phil Patterson wrote:Otherwise, I assume java would have to have some sort of duck-typing functionality.


Java 10 introduced var to make it possible to infer the type of a local variable.

Here's a contrived example:

Line 2 shows how to use the var for type inference, which isn't quite what duck typing is but gets Java closer to it without giving up too much of the benefits of type-safety.
 
Phil Patterson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops, you're right. I had forgotten Java introduced duck-typing.
 
Junilu Lacar
Sheriff
Posts: 17287
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phil Patterson wrote:Whoops, you're right. I had forgotten Java introduced duck-typing.


Well, again, local variable type inference is not quite to the level of duck typing as it is in languages like Ruby. But it gets us closer.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far, then, there are no ducks in Java. (I say that with my programmer's hat on but not with my birder's hat.)
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic