Into Java - Part III
Welcome back to the Into Java column. The past month you have acquainted yourself with the Java API documentation, as well as you have taken a glance at the theory lesson and now you know the basics about Java. You know how to write some Java code, to save the app, and to compile and run the application from your command line.
This time you can take it easy and take a closer look at the absolutely smallest parts to build with, that is data types. How do you manage data types and how do Java shows its strength at this area? At last you will examine how to extend your Java environment with Swing.
Data Types
To the contrary of REXX and other similar languages Java is a strongly typed programming language. This way javac will not let you compile the code unless you specify which data type you are using, or wish to use. Hence
x = 3.141592653589793;
is not valid, unless you have first declared x as double. As
double x;
x = 3.141592653589793;
or the shortcut
double x = 3.141592653589793;
You may ask why this strong typing is necessary, as some other languages does not require it. There are at least two benefits from it: First it will make your code less error prone as you really have to convince yourself at compile time that it should work, else the compiler will grumble at your mistakes. Second the strong typing will give you a way to differentiate methods from each other, even though using the same method name (we will explore that a few issues ahead).
Java uses eight primitive types, of which six are number types, one is char that is used for characters, and the last one is the boolean type for true/false values. These primitive types are not to be confused with any class named equally, that is boolean IS NOT an equivalent to the class Boolean of the java.lang package (but of course they have some kind of connection and you will look at that later on). The primitive types are really down as close to machine level as you ever will come when using Java, and you don't need to bother if you don't like to.
Primitive types must always be declared as shown above, but you never need to use the new operator as you have to when declaring a new object from a class.
A significant strength to other programming languages is that Java is not dependent upon the target machine. You may remember that an int in e.g. C/C++ is not of the same size on a 16-bit Intel as it is on a 32-bit SPARC, nor is it of the same size on a 32-bit processor when using Windows 3.x as it is using Windows 95/NT. That sounds quite confusing to most people, but Java is not that bothersome.
boolean
Java introduces a boolean type, that can be true or false. This data type is used in any logical test that Java supports, i.e.
boolean isSetToTrue; // variable declaration without instantiation
isSetToTrue = getBooleanAnswerFromSomething(); // asks a method
if (isSetToTrue) {
// your code here will be executed if isSetToTrue is true
}
This way you never have to think about which return value is true or which is false, as you have to using e.g. C/C++. Further, a boolean can never be converted to anything else, it is a boolean, period.
char
The char type is designed to handle the 2-byte Unicode characters, that is a char is a 2-byte sized. Since ASCII/ANSI is a small subset of Unicode you never have to worry about this powerful feature unless you are heading for the international application market. Java for OS/2 of course supports Unicode if you like to use it, a somewhat bigger download though. However, you never have to worry about how a char looks like "under the hood", as you almost never have to know exactly what happens within the Java machinery.
To use single characters you have to use single quotes
char test = 'q';
Since char is a primitive data type you may use it with arrays right out of the box. Hence
char[] chArray = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'};
is twelve characters stored in an char array, you may access them one at a time. Compare that to the String
String hw = "Hello World!"
that is one single string containing twelve characters. The figure to your right will show the difference. The upper object is chArray containing twelve chars, and you know exactly how it looks like. The lower object is the string hw, but on the contrary you don't know how it looks like inside, except that you can get the value "Hello World!" from it if you like to.
Number Data Types
Numbers can be divided into two groups, with decimals or without decimals, that is floating-point types vs. integers. Java uses two floating-point types, and it uses four integer types. Why?
Regarding floating-point numbers you will mainly use double that gives you maximum precision, but once upon a decade you may choose float that is processed faster or maybe when you run out of disk space.
Normally you will use the int
integer. But often you must choose long when calling certain methods within the Java classes, or if you like to count the distance to the sun in meters. The other two integer data types, short and byte, are used in low-level file handling, or perhaps if you are managing very large amounts of integers.
Type | Size | Range (approximately) | Code example |
---|---|---|---|
Floating-point numbers | |||
double
|
8 bytes | 1.7977E+308, 15 significant digits | double d = 3.14159;
|
float
|
4 bytes | 3.4028E+38, 6 significant digits | float f = 3.14159F;
|
Integer numbers |