Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4
| Lesson 5 | Lesson 6 | Lesson 7 | Lesson 8 | Lesson 9 | Lesson 10
In lesson 3 we will cover key terms and what they are. In java there are many terms that are reserved by java and can not be used. For example: In the program below the key terms would be: class, public, static, void, and a few others.
class hello {
public static void main(String[] args) {
//variable is here
int a = 27;
System.out.println("Hello, System.... is a reserved term too");
}
}
|
Here are a few more common key terms that are reserved by java:
abstract, double, int, strictfp,
boolean, else, interface, super,
break, extends, long, switch,
byte, final, native, synchronized,
case, finally, new, this,
catch, float, package, throw,
char, for, private, throws,
class, goto, protected, transient,
const, if, public, try,
continue, implements, return, void,
default, import, short, volatile,
do, instanceof, static, while
Those are kinds of words that can not be used as variable names or other things. They can only be used specifically for there uses only. Example:
Type this in and run:
class keyTerms {
public static void main(String[] args) {
//variables are here
int public = 27;
String class = "Wrong!";
//notice the error
}
}
|
Now you should get some thing nasty like this on your sreen:
|
--------------------Configuration: DiveLog - j2sdk1.4.0 - --------------------
C:\javap\DiveLog\src\ttt.java:4: not a statement
int public = 27;
^
C:\javap\DiveLog\src\ttt.java:4: ';' expected
int public = 27;
^
C:\javap\DiveLog\src\ttt.java:5: not a statement
String class = "Wrong!";
^
C:\javap\DiveLog\src\ttt.java:5: ';' expected
String class = "Wrong!";
^
C:\javap\DiveLog\src\ttt.java:5: expected
String class = "Wrong!";
^
C:\javap\DiveLog\src\ttt.java:5: '{' expected
String class = "Wrong!";
^
C:\javap\DiveLog\src\ttt.java:8: '}' expected
}
^
7 errors
Process completed.
|
So what happend was that we used a key term in stead of our own name. (i.e) String class = "Wrong!" class is a key term and can not be used there. Go back and fix the problem.You may rename it to whatever you like. The solution is below, just highlight the box:
class keyTerms {
public static void main(String[] args) {
//variables are here
int age = 27;
String whateverYouWant = "Correct!";
//now that's right
}
}
|
Now you should get a proccess completed. There are many key terms in java, normally you wouldn't have to check the list for them because keys terms are in blue when you type, if you are using JCreator. If not you can check the list at java.sun.com.
The key terms are only used when their function is needed. For example, If you want the compter to do and if/else (you will learn that) then that's when you will declare the key term if/else
class keyTerms {
public static void main(String[] args) {
//variables are here
int age = 27;
boolean likesIceCream = true;
String whateverYouWant = "Correct!";
//now that's right
if(likesIceCream = true){
System.out.println("I do like ice cream.");
}
else{
System.out.println("I do not like ice cream.");
}
}
}
|
Note:If you have an extreme need to use a key term then you can change the case. (i.e.)change if to IF. In java cased does matter and if and IF are like hot sauce and ketchup.
Summary:
By the end of this lesson you should have learned about key terms, when they are used and how to identify them.
|
Copyright ©2004 Java Wave, All rights reserve.
|