Page 2 of 5
javac *.java java IntegerTest java DoubleTest
Java uses four integral types and two floating point types, which both hold different ranges of numbers and take up varying amounts of storage space. The following table lists them, along with some of their properties:
| Integral types |
|---|
| Type | Size (Bits) | Range |
|---|
| byte | 8 | -128 to 127 |
| short | 16 | -32,768 to 32,767 |
| int | 32 | -2,147,483,648 to 2,147,483,647 |
| long | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| Floating point types |
|---|
| Type | Size (Bits) | Range |
|---|
| float | 32 | Single precision floating point (IEEE 754 conforming) |
A string type holds strings, and handles them differently from the way integral and floating point types handle numbers. The
Java language includes a String class to represent strings. You declare a string using the type String, and initialize it with a quoted string, a sequence of characters contained within double quotes, as shown below. You can
also combine two strings using the + operator.
// Code fragment // Declaration of variable s of type String, // and initialization with quoted string "Hello." String s = "Hello"; // Concatenation of string in s with quoted string " World" String t = s + " World"; System.out.println(t); // Outputs Hello World
In addition to type, scope is also an important characteristic of a variable. Scope establishes when a variable is created and destroyed and where a developer can access the variable within a program. The place in your program where you declare the variable determines its scope.
So far, I've discussed local variables, which hold temporary data that you use within a method. You declare local variables inside methods, and you can access them
only from within those methods. This means that you can retrieve only local variables anInteger, which you used in IntegerTest, and aDouble, which you used in DoubleTest, from the main method in which they were declared and nowhere else.
You can declare local variables within any method. The example code below declares a local variable in the AlarmClock snooze() method:
public class AlarmClock {
public void snooze() {
// Snooze time in millisecond = 5 secs
long snoozeInterval = 5000;
System.out.println("ZZZZZ for: " + snoozeInterval);
}
}
You can get to snoozeInterval only from the snooze() method, which is where you declared snoozeInterval. Refer to the example below:
public class AlarmClockTest {
public static void main(String[] args) {
AlarmClock aClock = new AlarmClock();
aClock.snooze(); // This is still fine.
// The next line of code is an ERROR.
// You can't access snoozeInterval outside the snooze method.
snoozeInterval = 10000;
}
}
A method parameter, which has a scope similar to a local variable, is another type of variable. Method parameters pass arguments
into methods. When you declare the method, you specify its arguments in a parameter list. You pass the arguments when you
call the method. Method parameters function similarly to local variables in that they lie within the scope of the method to
which they are linked, and can be used throughout the method. However, unlike local variables, method parameters obtain a
value from the caller when it calls a method. Here's a modification of the alarm clock that allows you to pass in the snoozeInterval.
public class AlarmClock {
public void snooze(long snoozeInterval) {
System.out.println("ZZZZZ for: " + snoozeInterval);
}
}
public class AlarmClockTest {
public static void main(String[] args) {
AlarmClock aClock = new AlarmClock();
// Pass in the snooze interval when you call the method.
aClock.snooze(10000); // Snooze for 10000 msecs.
}
}
Local variables are useful, but because they provide only temporary storage, their value is limited. Since their lifetimes span the length of the method in which they are declared, local variables compare to a notepad that appears every time you receive a telephone call, but disappears once you hang up the phone. That setup can be useful for jotting down notes, but you often want something a little more permanent. What's a programmer to do? Enter member variables.
Member variables -- of which there are two, instance and static -- make up part of a class. I will consider instance variables now, and return to static variables in a later article.
Developers implement instance variables to contain data useful to a class. An instance variable differs from a local variable in the nature of its scope and its lifetime. The entire class makes up the scope of an instance variable, not the method in which it was declared. In other words, developers can access instance variables anywhere in the class. In addition, the lifetime of an instance variable does not depend on any particular method of the class; that is, its lifetime is the lifetime of the instance that contains it.
Remember instances from the previous article? Instances are the actual objects that you create from the blueprint you design in the class definition. You declare instance variables in the class definition, affecting each instance you create from the blueprint. Each instance contains those instance variables, and data held within the variables can vary from instance to instance.
Consider the AlarmClock class. Passing the snoozeInterval into the snooze() method isn't a great design. Imagine having to type in a snooze interval on your alarm clock each time you fumbled for the
snooze button. Instead, just give the whole alarm clock a snoozeInterval. You complete this with an instance variable in the AlarmClock class, as shown below:
public class AlarmClock {
// You declare snoozeInterval here. This makes it an instance variable.
// You also initialize it here.
long m_snoozeInterval = 5000; // Snooze time in millisecond = 5 secs.
public void snooze() {
// You can still get to m_snoozeInterval in an AlarmClock method
// because you are within the scope of the class.
System.out.println("ZZZZZ for: " + m_snoozeInterval);
}
}
You can access instance variables almost anywhere within the class that declares them. To be technical about it, you declare the instance variable within the class scope, and you can retrieve it from almost anywhere within that scope. Practically speaking, you can access the variable anywhere between the first curly bracket that starts the class and the closing bracket. Since you also declare methods within the class scope, they too can access the instance variables.