Java Primer by Dr. Jim McKim

Class

A Java class is a structured piece of software code. It consists mostly of variable declarations and routines. Usually there'll be just one class in any given file and they should share the same name. E.g. the Facts class should be stored in a file named Facts.java. It's traditional to capitalize the first letter of the class name and you should use the same capitalization for the file name.  Most programs in Java will use many classes. Some of these will be new, written especially for the job at hand, and some of them will have been written before. Classes that appear to be very useful, like Random, are carefully written so that they can be used over and over again and are usually made available in a library.

All Java classes have the same basic structure.

Variables

A variable is a named piece of computer memory. Each variable needs to be declared with a type and a name. The name tells you how to refer to that piece of storage later and the type tells the computer how much storage will be required and reminds you how that variable may be used. For example, the declaration

int numSides;

tells the computer to set aside space for a whole number (an integer). And it's a contract between you and the computer that you will only use this variable where it makes sense to use a whole number. You can do arithmetic with it, for example, and you can, of course, store whole numbers in this location. You'd best not try to store a number like 7.3 in this location though, as it's not a whole number!

When you need to introduce a variable into a program, your thinking should go something like this:

  1. Import the type of the variable if necessary.
  2. Decide on a name for the variable and declare it.
  3. Initialize it, usually just once.
  4. Use it as many times as you need.

With relatively complex types like Random, we need a special statement to do the initializing, usually involving the key word, new. More on this later in the course. With simple types like int, the distinction between initializing and using can be blurred. Just make sure your integers have good values before you do any arithmetic with them or print them out.

Types

Some types like int (for whole numbers) or double (for extra accurate decimal numbers) are built in and you can just use them. Other types like Random are more complex and need to be imported before they're used. It just makes things a little easier on the system if you tell it ahead of time that you're going to use a less common type like Random. We'll see later that there is a very close relationship between class and type. In fact Random is just a class that somebody else wrote so that we would have an easy way to generate random numbers.

Routines

Routines are named pieces of code within a class that actually get things done. While you can declare variables inside of routines, the most important parts are statements. Think of a routine as a paragraph in an essay that has an overall goal or point. And think of statements as the nitty gritty sentences that work together to make that point. All Java routines have the same basic structure which you can see here.

Statements

These are the nitty gritty parts of routines. Sometimes they just print things out, but most times they'll have some effect on memory as well. You can think of variable declarations as setting up memory, and statements as the places for modifying that memory. A statement like

numSides = 5;

is called an assignment statement because it assigns the variable numSides to a value of 5. If you want to think a little deeper you could say the memory location numSides is set (or assigned) to a value of 5.

We've also seen output statements like

System.out.println( "A pentagon has " + numSides);

In Java pretty much all statements must end in semicolons.  There are a few other kinds of statements as well. We'll gradually take them up in the next few days.

Libraries

Software libraries provide classes that other people have written. We usually have to use an import statement to get at them.