my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Compile error when I try to use float, works if I change it to double.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile error when I try to use float, works if I change it to double.." Watch "Compile error when I try to use float, works if I change it to double.." New topic
Author

Compile error when I try to use float, works if I change it to double..

Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18

Here's the first source code:


And here's the second source code (the one which contains the main method):



I've commented, so just read the comments.
The problem is as you can see, that it runs just fine if I declare "vikt" as a double, but I can't declare it as a float.. (which is weird, since they're the same except double takes more space in the memory and you can define more precise numbers with it, maybe there are even more details, but please, let's stick to the basics for now, which is explaining why double works and float doesn't :P )
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Staying with the basics:
First, you should double check the tutorial. I think this one has most of what you need: datatypes.

Second: The default data type for any number with a decimal point is a double. So unless you define the constant as a float, you will get a double instead of a float. Like other primitives you can not automatically assign from a 'wider' data type (takes more bits) to a 'narrower' one, because of the loss of data. You could use float, though, if you called the method with aket.setVikt(1.3f);


Steve
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
hmm, ok.
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

And what is aket.* ?

Would it work if I change this: to this?

I think I'll just accept the double for now, but I'm still curious.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Ethan Bauer wrote:
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

it's not and it does.

however, when you simply write a literal value with a decimal point, Java assumes you have a double, not a float. And no double, regardless of the value, will fit into a float. Java thinks you have the value 1.30000000000000000000000000000000000000001213 (or something like that). you are trying to cram that into a variable that will only hold the value 1.30000000000000983 (or something like that).

So, tell java you really have a float:

paket.setVikt(1.3f);


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Ethan Bauer wrote:hmm, ok.
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

1.3 is a double, and a double is 64 bit. So no, it doesn't fit. There is actually a difference in the internal storage and precision between float and double (not so basic stuff) so even though the range seems like it should fit the reality is that the precision keeps it from doing so.

And what is aket.* ?

That was a mis-copy from your code. should have been paket.setVikt(...) not aket.setVikt(...)

Would it work if I change this: to this?

Should be rather painless to try, right? Why not give it a go and see?

I think I'll just accept the double for now, but I'm still curious.
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
haha, alright, I'll try it next time and THEN follow up with a question.
Didn't know Java automatically thinks it's a double if you write down a number with decimals, now I know that I gotta mark it with "f" for floats, same as when you write longs, you mark it with L
Thanks for the help.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

I don't know your reason for using a float instead of a double, but you can just use the double if you don't have very specific reasons not to use a double.

It's like the integer datatypes as well. Java uses int by default, but there are also the smaller byte and short available, as well as the bigger long.
Ethan Bauer
Greenhorn

Joined: Sep 22, 2010
Posts: 18
Yeah, I usually use int for integers and double for floating point numbers, but I was just curious in this case and thought I'd try float..
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Experimenting is always good. I'm glad you figured it out
 
jQuery in Action, 2nd edition
 
subject: Compile error when I try to use float, works if I change it to double..