02 – Variables
We continue our tutorial this week, with a discussion of basic variables in Java. Though we’re not really ready to start writing programs yet, at the end of this lesson we’ll have some code you put into your editor and attempt to compile and run.
If you are familiar with variables in other languages then variables in Java will give you very few problems. Java is considered a strongly, statically typed language much like the C language and unlike Perl (which is weakly, dynamically typed). Discussions of the advantages and disadvantages of the various types of variable typing aside, this means you need to keep several things in mind when programming in Java.
First you must declare any variable before you use it. To create an primitive integer (from now on known as an int) named “i†you would use the following code:
int i;
Notice that (almost!) every line in Java must end in a semicolon.
As a convenience you can also assign an initial value for a variable when you declare it:
int i = 42;
Next, you cannot typically convert a variable to another type of variable without either casting or using a convenience method which Java makes available to help with conversions. So if we wanted to change our int to a String, we would use the following method:
int i = 42;
String s = Integer.toString(i);
As another example if we wanted to convert a long value to a String you would have to use a cast:
long l = 42l;
// that's the number 42 followed by
// the letter “Lâ€
int i = (int) l;
In the above example you’ll notice the line containing two slashes (//). This indicates the start of a comment, so everything after the slashes is ignored on that line.
The reason this must be cast is because you are potentially losing magnitude as longs (64 bit integers) can be much larger than ints (32 bit integers). You are telling the compiler that you know what you are doing and to trust you. Notice that going the other way a cast is not necessary:
int i = 42;
long l = i;
This is called variable promotion.
So far the variables we have looked at with one exception have been primitives. The exception is the String we created above, which in the Java language is an object. Java provides primitive data types for numbers (like int, long, float, double), characters (char), booleans (boolean) and others (like byte). However, for any of these primitive type there is an associated Object type. For example an int has a corresponding Integer class. (Notice the upper-case letters on class names – that’s a Java convention.)
Why provide both? As we saw above when we provide a class, like Integer, we can associate methods to the class that allow us to manipulate the data. There’s no way to change an int to a String directly – however, if we make it an Integer instead we have methods to help us.
Integer i = new Integer(42);
String s = i.toString();
Here you can see that we called a method called toString() on the Integer i and it returned a String object which we assigned to String s. By the way, we’ll talk about methods in a bit, but for now, consider them to be like subroutines that are associated with our object.
But if the class representation of a primitive is so useful why provide the primitives? Unfortunately with all the abilities of the associated representation classes, some things are not easy to do with the classes. For example, if we wanted to multiply two numbers:
// Easy with primitives:
int a = 2;
int b = 3;
int c = a * b;
// Hard with classes:
Integer d = new Integer(2);
Integer e = new Integer(3);
Integer f = new Integer(d.intValue() * e.intValue());
Other things like the increment operator (++) will only work on primitives as well.
int a = 1;
a++; // a is now equal to 2
That’s the basic overview of variables, though we have more to learn about them. Further details will become clear as we delve into more features of the Java language.
Variables Exercise
We haven’t explored enough of Java yet to be able to write a complete program. Instead, fill out this template to perform those actions the comments describe.
public class Exercise1 {
public static void main(String[] args) {
// Create an int called a and set it to the value of 3
// Create an int called b and set it to the value of a+5
// Create an Integer called c from b
// Create a String d from c
// Print out the value of d:
System.out.println(“>>>>†+ d);
}
}
You don’t need to worry too much about the details right now, but the System.out.println(); statement just prints to standard output.
If you save this code above as a file called “Example1.java”, you should then able to use the following commands to compile and run the program.
> javac Example1.java
> java Example1
You must be in the same directory as the .java file you created for the above to work.