The Elements in a Java Program
That both English and Java are called languages is no coincidence. You use language to express ideas. English expresses ideas to people, and Java expresses ideas to computers.
What’s more, both English and Java have things like words, names, and punctuation. In fact, the biggest difference between the two languages is that Java is easier to learn than English.
(If English were easy, then computers would understand English. Unfortunately, they can’t.) Take an ordinary English sentence and compare it with the code in Listing. Here’s the sentence: Suzanne says “eh” because, as you know, she lives in Canada.
Identifiers that you or I can define
I like the name Suzanne, but if you don’t like traditional names, then make up a brand-new name. You’re having a new baby. Call her “Deneen” or “Chris anta.” Name him “Belton” or “Merk.”
A name is a word that identifies something, so I’ll stop calling these things names and start calling them identifiers. In computer programming, an identifier is a noun of some kind.
An identifier refers to a value, a part of a program, a certain kind of structure, or any number of things. Listing has two identifiers that you or I can define on our own. They’re the made-up words.
Identifiers with agreed-upon meanings
Many people are named Suzanne, but only one country is named Canada. That’s because there’s a standard, well-known meaning for the word “Canada.” It’s the country with a red maple leaf on its flag. If you start your own country,
you should avoid naming it Canada, because naming it Canada would just confuse everyone. (I know, a town in Kentucky is named Canada, but that doesn’t count.
Remember, you should ignore exceptions like this.) Most programming languages have identifiers with agreed-upon meanings. In Java, almost all of these identifiers are defined in the Java API. Listing has five such identifiers.
A literal is a chunk of text that looks like whatever value it represents. In Suzanne’s sentence, “eh” is literal, because “eh” refers to the word “eh.”
Programming languages have literals too. For example, in Listing 4-1, the stuff in quotes is literal.
A typical computer program has lots of punctuation. For example, consider the program
Each bracket, each brace, each squiggle of any kind plays a role in making the program meaningful.
In English, you write all the way across one line, and then you wrap your text to the start of the next line. In programming, you seldom work this way. Instead, the code’s punctuation guides the indenting of certain lines. The indentation shows which parts of the program are subordinate to which other parts.
A comment is text that’s outside the normal flow. In Figure words “A comment:” aren’t part of the Suzanne sentence. Instead, these words are about the Suzanne sentence.
The same is true of comments in computer programs. The first five lines in Listing form one big comment. The computer doesn’t act on this comment. There are no instructions for the computer to perform inside this comment.
Instead, the comment tells other programmers something about your code. Comments are for your own benefit, too. Imagine that you set aside your code for a while and work on something else. When you return later to work on the code again, the comments help you remember what you were doing.