Keywords
Lesson:
Main Points:
- The Java language specifies 50 keywords and other reserved words.
- The words goto and const are reserved even though they aren't used for
anything.
- An identifier is a word used by the programmer to name a variable,
method, class, or label.
- Identifiers may begin with a letter, a dollar sign, or an
underscore.
Java Keywords:
abstract const final instanceof private synchronized
boolean continue finally int protected this
break default float interface public throw
byte do for long return throws
case double goto native short transient
catch else if new static true
char extends null super try implements
class false import package switch void
volatile while
Identifiers:
An identifier must begin with a letter, a dollar sign ($), or an underscore (_);
subsequent characters may be letters, dollar signs, underscores, or digits. Here
are some examples:
- foobar (legal)
- BIGfloat (legal: embedded keywords OK)
- $income (legal)
- 4square (illegal: starts with digit)
- !more (illegal: must start with letter, $, or _)
NOTE: Identifiers are case sensitive. This means that the identifiers circle
and Circle are two distinct identifiers. The same goes for circle and cIrcle. This can
be confusing and so it's not a great idea to have two identifier names with the only
difference between them being letter case.
EXAMPLE:
import java.awt.*;
import java.applet.*;
public class ex2 extends Applet{ // class name: ex2 (matches file name)
int dogs; // variable name
int cats; // variable name
String message; //varialbe name
Color $xxx = new Color(0, 255, 255); //variable name beginning with $
Color _xxx = new Color(255,255,0); //variable name beginning with _
public void init(){
setBackground($xxx);
message="Good Morning Trona High!";
makePets(); // call to method
}
public void makePets(){
dogs=12;
cats=15;
}
public void paint(Graphics g){
g.setColor(_xxx);
g.drawString(message, 10, 20);
g.drawString("DOGS: "+dogs, 10, 40);
g.drawString("CATS: "+cats, 10, 60);
}
}
Study this example. It shows several names used for class, methods, and
varibles.
Assignment:
You will make a simple applet in which you create one method and six
variables. Show that you can use a variety of interesting names which are
all legal. (If you use an illegal name the compiler will let you know.) Your
applet can be VERY simple as long as it has the elements described, BUT all
variable values must be displayed in your applet.
Present your code along with your applet on a web page.