Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
In Listing 1, below, you get a blast of Java code. Like all novice programmers, you're expected to gawk humbly at the code. But don't be intimidated. When you get the hang of it, programming is pretty easy. Yes, it's fun, too.

Listing 1: A Simple Java Program

/*
* A program to list the good things in life
* Author: Barry Burd, [email protected]
* February 10, 2021
*/
public class ThingsILike 
{
 public static void main (String[] args)
{
  System.out.println("Chocolate, royalties, sleep");
 } 
}
Buried deep in the heart of Listing 1 is the single line that actually issues a direct instruction to the computer. The line
System.out.println("Chocolate, royalties, sleep");
tells the computer to display the words Chocolate, royalties, sleep in the command prompt window. This line can be described in at least two different ways:
  • It's a statement: In Java, a direct instruction that tells the computer to do something is called a statement. The statement in Listing 1 tells the computer to display some text. The statements in other programs may tell the computer to put 7 in certain memory location, or make a window appear on the screen. The statements in computer programs do all kinds of things.
  • It's a method call: A method call is a separate piece of code (in a different part of the Java program) that tells the computer to call the method into action.The statement
<tt>FixTheAlternator(junkyOldFord);</tt>
    is an example of a method call, and so is
<tt>System.out.println("Chocolate, royalties, sleep");</tt>
    Java has many different kinds of statements. A method call is just one kind.

Ending a statement with a semicolon

In Java, each statement ends with a semicolon. The code in Listing 1 has only one statement in it, so only one line in Listing 1 ends with a semicolon.

Take any other line in Listing 1, like the method header, for instance. The method header (the line with the word main in it) doesn't directly tell the computer to do anything. Instead, the method header describes some action for future reference. The header announces "Just in case someone ever calls the main method, the next few lines of code tell you what to do in response to that call."

Every complete Java statement ends with a semicolon. A method call is a statement, so it ends with a semicolon, but neither a method header nor a method declaration is a statement.

The method named System.out.println

The statement in the middle of Listing 1 calls a method named System.out.println. This method is defined in the Java API. Whenever you call the System.out.println method, the computer displays text on its screen.

Think about the name Pauline Ott, for example. Believe it or not, I know two people named Pauline Ott. One of them is a nun; the other is physicist. Of course, there are plenty of Paulines in the English-speaking world, just as there are several things named println in the Java API. So to distinguish the physicist Pauline Ott from the film critic Pauline Kael, write the full name "Pauline Ott." And, to distinguish the nun from the physicist, write "Sister Pauline Ott." In the same way, write either System.out.println or DriverManager.println. The first writes text on the computer's screen. The second writes to a database log file.

Just as Pauline and Ott are names in their own right, so System, out, and println are names in the Java API. But to use println, you must write the method's full name. You never write println alone. It's always System.out.println or some other combination of API names.

The Java programming language is case-sensitive. If you change a lowercase letter to an uppercase letter (or vice versa), you change a word's meaning. You can't replace System.out.println with system.out.Println. If you do, your program won't work.

The Java class

You may have heard the term object-oriented programming (also known as OOP). OOP is a way of thinking about computer programming problems — a way that's supported by several different programming languages. OOP started in the 1960s with a language called Simula. It was reinforced in the 1970s with another language named Smalltalk. In the 1980s, OOP took off big time with the language C++.

Some people want to change the acronym, and call it COP, class-oriented programming. That's because object-oriented programming begins with something called a class. In Java, everything starts with classes, everything is enclosed in classes, and everything is based on classes.

In Java, your main method has to be inside a class. The code in Listing 1 starts with the words class ThingsILike. Take another look at Listing 1, and notice what happens after the line class ThingsILike. The rest of the code is enclosed in curly braces. These braces mark all the stuff inside the class. Without these braces, you'd know where the declaration of the ThingsILike class starts, but you wouldn't know where the declaration ends.

It's as if the stuff inside the ThingsILike class is in a box. To box off a chunk of code, you do two things:

  • You use curly braces: These curly braces tell the compiler where a chunk of code begins and ends.
  • You indent code: Indentation tells your human eye (and the eyes of other programmers) where a chunk of code begins and ends.
Don't forget. You have to do both.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: