Java Quick Tip: ints and doubles

Class Notice- Please join RoboDuke's new Java Class Group, and use the group for class questions! 

Why do we choose to use doubles instead of floats for decimal numbers? And why do we use int for whole numbers instead of short, byte or long? It turns out when Java sees the constant 25.20, it assumes that it is a double. Therefore, it is very simple for Java to do

  double hourlyWage = 25.20;  

since it is assigning the number 25.20 to a chunk of memory that is exactly the right size for it to fit. However, we saw on page 51, that if we wanted to use a float variable, we would need to do

  float hourlyWage = 25.20f;  

Seems odd? The reason is that because, referring to page 51, you are trying to take the number which Java stores in 64 bits (the size of a double and Java considers the number 25.20 a double) and shove it into a 32 bit hole (the size of a float). Java refuses to do so unless you specifically tell her (bet you didn't know she was a female) it is ok, because of problems with putting something big into a small hole. The way you tell her is by placing the f at the end of the number to indicate that you want it considered a float.

The same rule holds for whole numbers. The constant 167 is considered by Java to be an int data type. You would have no problem with

  int num= 167;  

or

  long num = 167;   

(this second one puts a 32 bit number in a 64 bit hole)

But could run into problems with

   short num = 167;  

or

   byte num = 167;   

Note that I said could. Java will actually allow you to do this if it really fits (see our example code) but complains if it does not fit. Moral of the story: Stick to ints and doubles unless you have a reason to use something else!

Comments

Robostuff's picture

That helps explain in better thanks!

dkaz's picture

RoboDuke, are you planning to take a more practical approach to this and make your lessons applicable to RS Media?

It would be fun to start out with a "Hello World" for RS Media, instead of learning the language w/o any immediate reward. 

Robo Aztec's picture

RoboDuke 
The robot sdk will include it in the course of java

RoboDuke's picture

No immediate reward?? For writing Java??  :-)  There are specific APIs for RS Media but until you know what classes, interfaces, and inheritance are, they are meaningless.  If you want to write a simple program and have him "say" Hello World", you are dreaming. :-)  Many in the course want to learn Java and then program the kid, so I think we will stay on that course.  But thanks for the thoughts!  Diane 

dkaz's picture

it should really be easier to do a "Hello World"

maybe we can build some sort of a base library that makes that easy 

as far as the coursework goes, I'm just being selfish since I already know Java
:)

Robostuff's picture

All good things come to those who wait..

RoboDuke's picture

I figured that you did already know some Java.  You should be able to download NetBeans and just go for it yourself.  You may not need these lessons at all.  But for the majority of the people following us, I really feel we need the foundations through an easy IDE and step by step instructions before we really launch it robot.