Children are curious creatures; they have a tendency to repeat the question “Why?” in seemingly endless succession. Sometimes, from the perspective of the parents, the only comprehensible answer is “Because I said so.”
Each of us has heard this almighty bailout at some point in our life. It’s as maddening now as it was when we were toddlers, so why do we tolerate it from the people who teach us Java?
There is a significant faction of people who can write useable code without knowing why they’re typing “public static void” in front of a method definition, or why “double” and “Double” refer to two different things. This is because the majority of learning resources tell us “We’ll get to that later,” and by the time they do, the nascent programmer is either off writing his first applet…or fast asleep.
The purpose of this series is to shed some light on the oft-neglected basic concepts behind Java. After all, telling a programmer “Type this line of code because it works” is akin to telling a musician “Play this note because it sounds good.” Teach that same musician some basic harmonic theory, however, and she will have much more creative control over compositions.
Because Java is an object-oriented language, we can write large programs one smaller piece at a time. This is advantageous when there are many programmers working to build a single application. Each of them may be responsible for the creation of a class file. Class files are generally used to describe objects by specifying attributes and behaviours. For example, you may be called upon to code an application that simulates a restaurant. Consider all of the objects that would be involved. There would be tables, dinnerware, waiters, customers, etc. Each of these may be described in a separate class. The attributes of a waiter may be things such as age, gender, hair colour, rate of pay, etc. The behaviours of the waiter may be to greet guests, take orders, bring food, and so forth. In Java, the attributes would be stored in variables, and the behaviours outlined in methods, all within the class Waiter.
There are eight data types in Java that are not objects. They are boolean, byte, char, double, float, long, int and short. These are referred to as primitive data types. Each of them has an object counterpart of the same name beginning with an uppercase letter, except in the case of char and int, which become Character and Integer respectively. This is important if you want to use the value contained within a primitive data type in a Vector or Hashtable, both of which only accept objects. The following line of code is an example of how to convert an int (primitive) to an Integer (object):
Integer nameOfIntegerToBeCreated = new Integer (nameOfPrimativeint);
This brings us to the subject of casting: converting one data type to another. Sometimes casting is automated by Java, such as when storing a character in an int variable yields that character’s Unicode value. Other times, we must use an explicit cast. The Java compiler should tell you when this is necessary. One example is attempting to store in an int the result of a mathematical operation involving a double value:
int result = 6.45 + 125;
The above will not compile. Consider it a built-in safety feature; the Java compiler likes you to prove that you know what you’re doing, and an explicit cast is required to truncate the decimal. An explicit cast is accomplished by prefacing the operation with the desired data type in parenthesis:
int result = (int) 6.45 + 125;
A value of 131 will be stored in the variable result.
This column merely scratches the surface of Java concepts. There is much more to be discussed, but, in an embarrassing display of hypocrisy, I must now say “I’ll get to it later.”
Part two of the series, to appear in issue 10, will explore method definitions.
Cooney is a Toronto-based programmer and a freelance Internet developer. He is currently enrolled in Humber College’s Computer Programmer program. He can be reached at [email protected]