Java Syntax Reference I
Java Syntax Reference I
Chapter 15
Java Syntax Reference
CONTENTS
l Modifiers
This section serves as a reference for the Java language itself. All keywords and operators in the
language are listed in alphabetical order, each followed by a complete explanation of the term, its
syntax, and an example of how it might be used in actual code. Further, for ease of identification, the
terms are set in bold in the code samples.
abstract
An abstract class or method is one that is not complete. Interfaces are automatically abstract.
Syntax:
Example:
break
Syntax:
break;
Example:
while (true) {
if ( connection.isClosed() )
break;
else
// code goes here
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 2 of 14
catch
The catch statement is used to handle any exceptions thrown by code within a try block.
Syntax:
try {
statement(s)
}
catch(Exception list) {
statement(s)
}
Example:
InputStream in;
int val;
...
try {
val = in.read() / in.read();
}
catch(ArithmeticException e) {
System.out.println("Invalid data. val set to 0.");
val = 0;
}
catch(Exception e) {
System.out.println("Exception encountered, but not handled.");
}
class
This is used in a class declaration to denote that the following code defines a class.
Syntax:
Example:
class MyClass
continue
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 3 of 14
Syntax:
continue;
Example:
Enumeration enum;
Object value;
...
while ( enum.hasMoreElements() ) {
value = enum.nextElement();
if ( value.equals("Invalid") )
continue;
else
System.out.println( value);
}
do…while
This is used to perform operations while a condition is met. The loop body will be executed at least
once.
Syntax:
do
statement(s)
while (booleanVariable);
do
statement(s)
while (booleanExpression);
Example:
do {
val = in.readByte();
System.out.println(val);
} while (val != '\n');
boolean valid = true;
do {
val = in.readByte();
if (val == '\n')
valid = false;
else
System.out.println(val);
} while (valid);
else
This is used in conjunction with the if statement to perform operations only when the requirements of
the if statement are not met.
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 4 of 14
Syntax:
if (booleanVariable)
statement(s)
else
statement(s)
if (booleanExpression)
statement(s)
else
statement(s)
Example:
if (stillRunning) {
System.out.println("Still Running");
advanceCounter();
}
else {
System.out.println("We're all done.");
closeConnection();
}
if (size >= 5)
System.out.println("Too big");
else
System.out.println("Just Right");
extends
This is used to make the current class or interface a subclass of another class or interface.
Syntax:
Example:
final
The final modifier makes a class or method final, meaning that it cannot be changed in a subclass.
Interfaces cannot be final.
Syntax:
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 5 of 14
Example:
finally
The finally statement is used in error handling to ensure the execution of a section of code. Regardless
of whether an exception is thrown within a try statement, the code in the finally block will be executed.
Syntax:
try {
statement(s)
}
finally {
cleanUpStatement(s)
}
try {
statement(s)
}
catch (Exception) {
exceptionHanldingStatement(s)
}
finally {
cleanUpStatement(s)
}
Example:
if (divisor == 0)
throw new ArithmeticException("Division by Zero.");
}
finally {
System.out.println("The fraction was " + numerator + "/" +
divisor);
}
}
try {
percent_over = quantity / number_ordered * 100; // could cause
division by 0
}
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 6 of 14
catch (ArithmeticException e) {
percent_over = 0;
}
finally { // regardless of the success of the try, we still need to
print the info
System.out.println("Quantity = " + quantity);
System.out.println("Ordered = " + ordered);
System.out.println("Percent Over = " + percent_over);
}
for
Syntax:
Example:
String name;
...
if
Syntax:
if (booleanVariable)
statement(s)
if (booleanExpression)
statement(s)
Example:
if (ValidNumbersOnly)
checkInput(Answer);
if (area >= 2*PI) {
System.out.println("The size of the loop is still too big.");
reduceSize(area);
}
implements
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 7 of 14
Syntax:
Example:
import
Syntax:
import packageName;
import className;
import interfaceName;
Example:
import java.io.*;
import java.applet.Applet;
import java.applet.AppletContext;
instanceof
The instanceof operator returns true if the object to the left of the expression is an instance of the
class to the right of the expression.
Syntax:
Example:
Modifiers
Access modifiers are used to control the accessibility and behavior of classes, interfaces, methods, and
fields.
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 8 of 14
native
Syntax:
Example:
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 9 of 14
new
The new operator allocates memory for an object, such as a String, a Socket, an array, or an instance of
any other class.
Syntax:
Example:
package
This is used to place the current class within the specified package.
Syntax:
package packageName;
Example:
package java.lang;
package mytools;
public
Syntax:
Example:
private
The private modifier makes the method or field accessible only to methods in the current class.
Syntax:
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 10 of 14
Example:
return
The return statement is used to return a value from a method. The data type returned must correspond
to the data type specified in the method declaration.
Syntax:
return value;
Example:
static
The static modifier makes a method or field static. Regardless of the number of instances that are
created of a given class, only one copy of a static method or field will be created.
Syntax:
Example:
A static block is a set of code that is executed immediately after object creation. It can only handle
static methods and static fields.
Syntax:
static
statement(s)
Example:
static {
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 11 of 14
type = prepare();
size = 25;
}
super
Syntax:
super
super.methodName()
super.fieldName
Example:
switch
Syntax:
switch (variableName) {
case (valueExpression1) : statement(s)
case (valueExpression2) : statement(s)
default : statement(s)
}
Example:
char ans;
...
switch (ans) {
case 'Y' : startOver();
break;
case 'n' ;
case 'N' : cleanUp();
default : System.out.println("Invalid response.");
}
synchronized
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 12 of 14
Every object has a "lock" that can be seized by an operation. Any synchronized operation seizes this
lock, preventing other synchronized processes from beginning until it has finished.
Syntax:
Synchronized Method:
synchronized returnType methodName(optionalParameters)
synchronized (objectName)
statement(s)
Example:
this
Syntax:
this
this.methodName()
this.fieldName
Example:
throw
The throw statement is used to throw an exception within the body of a method. The exception must be
a subclass of one of the exceptions declared with the throws statement in the method declaration.
Syntax:
throw exceptionObject
Example:
throws
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 13 of 14
The throws keyword specifies the types of exceptions that can be thrown from a method.
Syntax:
Example:
try
The try statement is used to enclose code that can throw an exception. It should be used with the catch
() statement and may be used with the finally statement.
Syntax:
try
statement(s)
catch(Exception list)
statement(s)
finally
statement(s)
Example:
InputStream in;
int val;
...
try
val = in.read() / in.read();
catch(ArithmeticException e) {
System.out.println("Invalid data. val set to 0.");
val = 0;
}
catch(Exception e)
System.out.println("Exception encountered, but not handled.");
finally {
in.close();
System.out.println("Stream closed.");
while
Syntax:
while (booleanVariable)
statement(s)
while (booleanExpression)
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004
Chapter 15 -- Java Syntax Reference Page 14 of 14
statement(s)
Example:
FileInputStream din;
byte info;
http://docs.rinet.ru/ProPauk/ch15.htm 12/4/2004