Keywords, Identifiers, and Primitive Types

What is a keyword?

In Java a keyword is a word that has been reserved for a specific use. For example, the word class is a keyword, as are int and import.

In Java there is a lot of code that has been written for you, so you don't need to re-invent the wheel. It would be silly to make you write detailed code every time you wanted to create a window on the screen. You would need to specify how to open a window, what a window should look like, where and how (precisely) to put the window on the screen, etc... in fact it would be impossibly annoying. One of the great things about Java is that code written for use on a Windows machine will run the same way on a Mac or on a computer running Linux. It would take far too long for you to attempt to write this code --this is why companies take so long to port their code to a new operating system.

Instead, all the code you might need to create a window (and much more) is included in a package (a collection of Java classes). To use this package you write at the top of the file that contains the code where you want to use methods that create a window:

import java.awt.*;
The keyword import will tell the compiler to include in your program all the code in the Java Abstract Windows Toolkit, so you don't have to write it yourself.

Right now you will probably not understand how to use the information in the Java AWT, but later you might want to look at what is available for you to program with. At that point you should go to the on-line Java APIs at http://java.sun.com/products/jdk/1.2/docs/api/index.html, and look up package java.awt.


What is an identifier?

Identifiers are chosen by programmers as names for the various parts of a Java program. For example, the name of the method to print a string in the applet window is drawString. The name of a class would be an identifier, and the name of a variable would be an identifier. (Methods, classes and variables will be covered later.)

There are rules about what characters you can use as an identifier.

  1. The first character is a lowercase letter, an uppercase letter, or an underscore (the "_" symbol).
  2. All other characters are a combination of digits, lowercase letters, uppercase letters, and underscores.
  3. Keywords may not be used as identifiers.


What is a type?

A type describes the set of values that a variable can contain.


A Few Primitive Types

Primitive types are very simple bits of information such as a number, a character, or the value true or false. Java considers primitive types to be keywords.

You can use the math operators +, -, *, / on all primitive types except type boolean (but you would not want to multiply the value true by the value false anyway).

However, you must remember what the types of the values are that you are multiplying! For example:

1.5 = 3 / 2
Right? So in the code below, what is the value stored in x? (Hint: x is of type int, and can only store integers.)
int a= 3, b= 2;
int x= a/b;
When dividing two variables of type int, Java automatically rounds towards zero. So the value stored in x is 1. You will also have problems if you try to assign a decimal number to an int:
int x= 3.2; // Causes an error!
You will get a nasty compiling error along the lines of: "Incompatible type for declaration. Explicit cast needed to convert double to int." Try to avoid assigning a value of one type to a variable of a different type.

If you do want to know what the remainder was, you can use "%" which is called the "mod" operator.

int a= 5, b= 3;
int x= a%b;
This will store the number 2 in x, because the remainder of 5 divided by 3 is 2.