Java Language Basics Article

Tweet

Java is a powerful, cross-platform, object-oriented programming language suitable for writing anything from a distributed application that runs on a corporate network to a database-driven Web site to host your personal photo gallery. To make it easier to learn, the Java language was designed to resemble some of the most popular programming languages in use today, most notably C/C++. If you’re not a C/C++ expert, however (and most Web developers aren’t), the language can be a little intimidating. In this article, I’ll bring you up to speed on the basic syntax of the Java language, including variables, data types, operators, and control structures.

This article is the second in a series teaching the Java programming language with an eye towards its application to Web design and development. Specifically, the goal of this series is to teach you all you need to know to create dynamic Web sites using JavaServer Pages (JSP) and Java Servlets. This article picks up right where the previous article in the series, Getting Started with Java, left off.

Variables

Here is the listing for the program I helped you create in the previous article:

1  /** 
2   * Hello.java
3   * A simple Java program
4   */
5
6  class Hello {
7    public static void main(String[] args) {
8      // Print a couple of one-line messages
9      System.out.println("Hello, World!");
10     System.out.println("This is a test.");
11   }
12 }

This is an exceedingly simple program, as Java programs go, and I’m sure you don’t need me to tell you that it’s quite useless. What good is a program that prints out the same two lines every time you run it? At the very least, a program should perform some kind of useful calculation, right?

To be able to perform calculations and do useful work, a program must be able to manipulate data in some fashion. Like most programming languages, Java lets you store data in variables. A variable may be thought of simply as a named location in memory where data may be stored. Since different kinds of data may have different storage requirements, Java requires you to specify a data type for every variable that you create.

Let’s look at a basic example to help solidify this concept in your mind. Say you were writing a program that was performing temperature calculations for a laboratory experiment. Many such experiments take the room temperature into account in their calculations, so your program might need to store the room temperature in a variable. The following code creates a variable for storing whole numbers called roomTemp, and then assigns it a value of 20:

int roomTemp;     // Create integer variable 
roomTemp = 20;    // Assign the variable a value of 20

For the Americans in the audience who are used to seeing temperatures in Fahrenheit, 20 degrees Celsius is 68 degrees Fahrenheit. int stands for integer (programming lingo for a whole number), and is the data type of the roomTemp variable. So as you can see, creating a variable is as simple as typing the data type followed by the name of the variable. Variable names in Java, like methods (as we saw in the previous article), are not capitalized by convention, but it’s okay to use uppercase letters within the variable name to make them more readable. This is why I chose to name the variable roomTemp, instead of RoomTemp or roomtemp, or some other variation on the theme.

The second line assigns a value to the variable that was just created; specifically, it stores the number 20 in the variable. The equals sign (=) is called the assignment operator because it is used to assign values to variables. During the course of this article, you’ll meet many other operators that do other weird and wonderful things to variables and the values they store.

You must always create a variable before assigning it a value, and you’ll usually want to assign the variable a value before putting it to use. Trying to assign a value to a variable that does not exist will cause the Java compiler spit out an error message when you try to compile your program. Rather than automatically creating the variable for you, as some other programming languages do, Java errs on the side of caution by assuming you mistyped the name of the variable. This helps avoid bugs due to simple typing mistakes.

A newly created variable does not have a value. For example, until we assigned it a value of 20, one might assume that the roomTemp variable had some default value, such as zero. In fact, it did not. A variable with no value is said to be null. If you have trouble getting your head around something having no value at all, you could instead think of null as a special value that is assigned to all newly created variables, no matter their data type. Attempting to use a null variable as if it contained a value is one of the most common types of programming mistakes that cause programs to crash. That goes not only for Java programs, but also for programs written in other languages such as C/C++.

A shortcut exists for creating a variable and assigning it a value on the same line. The following code is equivalent to the two lines we saw above:

int roomTemp = 20;     // Create variable and assign value
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