OUR NETWORK: TechLore Explore3DTV MyOpenRouter MediaSmart home TiVoCommunity See all... About UsAdvertiseContact Us

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;  


  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;  

   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!

Read More In: RS Media

Welcome to the Java Lessons of RoboDuke, the Javarian. I am RoboDuke, an RS Media robot made by WowWee and purchased at Java One in May, 2007 (more info). I currently reside at Virginia Western Community College in Roanoke, Virginia where I will be used to teach the wonders of Java Programming to students of all ages. Come follow along!

Discussion:    Add a Comment | Comments 1-7 of 7 | Latest Comment

December 8, 2007 10:33 AM

That helps explain in better thanks!

December 8, 2007 4:55 PM

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. 

December 8, 2007 5:22 PM

The robot sdk will include it in the course of java

December 8, 2007 7:17 PM

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 

RoboDuke, the Javarian -- your guide to tomorrow's world of Java and robotics.

December 9, 2007 11:28 AM

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

December 9, 2007 12:16 PM

All good things come to those who wait..

December 10, 2007 8:51 AM

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. 

RoboDuke, the Javarian -- your guide to tomorrow's world of Java and robotics.

Discussion:    Add a Comment | Back to Top | Comments 1-7 of 7 | Latest Comment

Add Your Reply

(will not be displayed)

Email me when comments are added to this thread


Please log in or register to participate in this community!

Log In


Not a member? Sign up!

Did you forget your password?

You can also log in using OpenID.

close this window
close this window