Kinds of Words in a Java Program - dummies

Kinds of Words in a Java Program

By Barry Burd

A hard-core Javateer will say that the Java programming language has two kinds of words: keywords and identifiers. This is true. But the bare truth, without any other explanation, is sometimes misleading. So dress up the truth a bit and think in terms of three kinds of words: keywords, identifiers that ordinary programmers like you create, and identifiers from the API.

The differences among these three kinds of words are similar to the differences among words in the English language. In the sentence “Sam is a person,” the word person is like a Java keyword. No matter who uses the word person, the word always means roughly the same thing. (Sure, you can think of bizarre exceptions in English usage, but please don’t.)

The word Sam is like a Java identifier because Sam is a name for a particular person. Words like Sam, Dinswald, and McGillimaroo aren’t prepacked with meaning in the English language. These words apply to different people depending on the context and become names when parents pick one for their newborn kid.

Now consider the sentence “Julius Caesar is a person.” If you utter this sentence, you’re probably talking about the fellow who ruled Rome until the Ides of March. Although the name Julius Caesar isn’t hard-wired into the English language, almost everyone uses the name to refer to the same person. If English were a programming language, the name Julius Caesar would be an API identifier.

So here’s how you could divide the words in a Java program into categories:

  • Keywords: A keyword is a word that has its own special meaning in the Java programming language, and that meaning doesn’t change from one program to another. Examples of keywords in Java include if, else, and do.

    The JCP committee members, who have the final say on what constitutes a Java program, have chosen all the Java keywords. If you think about the two parts of Java, the Java keywords belong solidly to the Language Specification.

  • Identifiers: An identifier is a name for something. The identifier’s meaning can change from one program to another, but some identifiers’ meanings tend to change more.

    • Identifiers created by you and me: As a Java programmer (yes, even as a novice Java programmer), you create new names for classes and other things that you describe in your programs.

      Of course, you may name something Prime, and the guy writing code two cubicles down the hall can name something else Prime. That’s okay because Java doesn’t have a predetermined meaning for Prime.

      In your program, you can make Prime stand for the Federal Reserve’s prime rate. And the guy down the hall can make Prime stand for the “bread, roll, preserves, and prime rib.” A conflict doesn’t arise, because you and your co-worker are writing two different Java programs.

    • Identifiers from the API: The JCP members have created names for many things and thrown almost 40,000 of these names into the Java API. The API comes with each version of Java, so these names are available to anyone who writes a Java program. Examples of such names are String, Integer, JWindow, JButton, JTextField, and File.

Strictly speaking, the meanings of the identifiers in the Java API aren’t cast in stone. Although you can make up your own meanings for JButton or JWindow, this isn’t a good idea. If you did, you would confuse the dickens out of other programmers, who are used to the standard API meanings for these familiar identifier names.

But even worse, when your code assigns a new meaning to an identifier like JButton, you lose any computational power that was created for the identifier in the API code.

The programmers of Sun Microsystems, the Java Community Process, and the OpenJDK Project did all the work writing Java code to handle buttons. If you assign your own meaning to JButton, you’re turning your back on all the progress made in creating the API.