Java Language Basics Article

Data Types

In the previous example, we identified the roomTemp variable as being of the int data type, meaning that it could store integer (whole number) values. Now let’s say you keep your laboratory a little warmer than most, so you wanted to record a temperature of 22.5 degrees. You might modify line 8 of PrintRoomTemp.java so that it read as follows:

     int roomTemp = 22.5; // Room temperature

If you tried to compile this modified program, however, you would see an error message much like this one:

D:javaJavaLanguageBasics>javac PrintRoomTemp.java   
PrintRoomTemp.java:8: possible loss of precision  
found   : double  
required: int  
   int roomTemp = 22.5; // Room temperature  
                  ^  
1 error

Java is telling you that the int data type can’t store the value 22.5, and that you should instead use the double type, which is the Java data type for storing floating point numbers (numbers requiring a decimal point). The message "possible loss of precision" refers to the fact that the int data type could store 22 or 23, but not 22.5; that is, int does not provide the level of precision required to represent the value you’re trying to store.

So to allow for precise temperature values, you need to declare the roomTemp variable as a double instead of as an int:

     double roomTemp = 22.5; // Room temperature

Try this corrected line in your program, and you should see the expected output.

Java provides a handful of data types for use in your programs. Here’s the complete list for the technically minded:

  • boolean: true or false
  • char: a single 16-bit Unicode character
  • byte: an 8-bit signed integer
  • short: a 16-bit signed integer
  • int: a 32-bit signed integer
  • long: a 64-bit signed integer
  • float: a 32-bit floating-point number
  • double: a 64-bit floating point number

In case you’re wondering what the number of bits means for these various data types, basically the more bits a data type uses, the more different values it can store. Thus, long variables can store larger numbers than int variables, and double variables can store more precise values than float variables. In most programs you’ll be able to get by using int, double, and boolean (of which we’ll see some examples in a moment). The other types are reserved for special cases, or instances where you want to squeeze as much performance as possible out of the language by storing values in memory with as few bits as possible.

Some readers might wonder how we can store a string of text in a variable if there is no data type. The char data type lets you store a single character… does that mean that Java forces you to split up strings of text into characters when storing them in a variable? Certainly not!

You may remember that I mentioned in the previous article that Java programs are built by combining software components (also known as classes) to create the required functionality. As I’ll show in detail in the next article in this series, classes are similar in many ways to data types. In addition to letting you define your own classes, the Java language provides a number of built-in classes, one of which is the String class. The String class is actually quite special, as it is the only class in the Java language that shares certain properties with the basic data types shown above. In fact, many Java programmers get so used to using the String class as if it were a data type that they forget that it isn’t!

If that last paragraph just sailed straight over your head, don’t worry too much — classes in general and the special case of the String class will be discussed in greater detail in the next article in this series. In the meantime, all you need to know about the String class is that it can be used just like the data types we’ve seen so far:

String myName;   
myName = "Kevin Yank";  
 
String companyName = "SitePoint.com";  
 
System.out.print(myName);  
System.out.print(" works for ");  
System.out.println(companyName);

The above code will print the message "Kevin Yank works for SitePoint.com". The only detail that you should be especially careful about is to spell String with a capital letter. While all true data types are written in lowercase letters, String is a class, and as such it must be capitalized. The Java compiler will display an error message if you type it wrong.

Go to page: 1 | 2 | 3 | 4 | 5 | 6 | 7

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.