0% found this document useful (0 votes)
133 views17 pages

Java Syntax Reference

This document provides a reference for common Java syntax, including operators, flow control, variables, packages, classes, interfaces, methods, objects, and exceptions. It covers basic syntax like assignment, arithmetic, logical, and comparison operators as well as more complex concepts like packages, classes, inheritance, polymorphism, and exceptions. The document is designed to serve as a quick reference for Java syntax rather than a complete tutorial.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
133 views17 pages

Java Syntax Reference

This document provides a reference for common Java syntax, including operators, flow control, variables, packages, classes, interfaces, methods, objects, and exceptions. It covers basic syntax like assignment, arithmetic, logical, and comparison operators as well as more complex concepts like packages, classes, inheritance, polymorphism, and exceptions. The document is designed to serve as a quick reference for Java syntax rather than a complete tutorial.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 17

Introduction to Java Programming

Handout 336895255.doc

Java Syntax Reference


This handout is designed as a reference sheet for common Java syntax. For a more
complete reference see The Java Tutorial, or the book Java in a Nutshell.
Table of Contents
OPERATORS..................................................................................................................................................2
Assignment..............................................................................................................................................2
Arithmetic...............................................................................................................................................2
Increment and Decrement.......................................................................................................................2
Boolean...................................................................................................................................................3
Logical....................................................................................................................................................3
Shift.........................................................................................................................................................3
Casts.......................................................................................................................................................4
Instanceof...............................................................................................................................................4
FLOW OF CONTROL.....................................................................................................................................4
If Statement.............................................................................................................................................4
Switch Statement.....................................................................................................................................5
For Loop.................................................................................................................................................6
While Loop..............................................................................................................................................7
VARIABLES...................................................................................................................................................7
Primitive Data Types..............................................................................................................................7
Declaring a Variable..............................................................................................................................8
Declaring an Array.................................................................................................................................8
Declaring Constants...............................................................................................................................9
PACKAGES..................................................................................................................................................10
Naming.................................................................................................................................................10
Defining a Package..............................................................................................................................10
Importing a Package............................................................................................................................10
CLASSES AND INTERFACES.........................................................................................................................11
Defining a Class....................................................................................................................................11
Defining an Interface............................................................................................................................11
Extending a Class.................................................................................................................................12
Extending an Interface.........................................................................................................................12
Implementing an Interface....................................................................................................................12
METHODS...................................................................................................................................................13
Defining a Method................................................................................................................................13
Defining a Method with Parameters....................................................................................................13
Constructors.........................................................................................................................................14
OBJECTS.....................................................................................................................................................14
Creating (Instantiating) an Object.......................................................................................................14
MODIFIERS.................................................................................................................................................15
Visibility Modifiers...............................................................................................................................15
Public Classes and Interfaces..............................................................................................................16
Abstract Classes and Methods..............................................................................................................16
Final Classes Methods and Variables..................................................................................................17
Static Methods and Variables...............................................................................................................17
EXCEPTIONS...............................................................................................................................................18
Declaring a method to throw an Exception..........................................................................................18
Throwing an Exception.........................................................................................................................18
Handling (Catching) an Exception.......................................................................................................19
MISCELLANEOUS........................................................................................................................................19
Comments.............................................................................................................................................19

L. Dodds, October 2002

1/17

Introduction to Java Programming

Handout 336895255.doc

<variable name> = <expression>; int myNumber;


myNumber = 1;
Keywords..............................................................................................................................................20
Legal Names.........................................................................................................................................20

Operators
Assignment
Example:

<expression> is a variable, function, or parameter -- or any combination of


them, with operators and parentheses -- that evaluates to the same type as
<variable name>.
If <variable name> has been declared final, then <variable name> can
only be assigned a value when it is declared. All subsequent attempts to assign a
value to <variable name> are compile time errors.

Arithmetic
+
/
*
%

Addition
Subtraction
Division, ignoring remainder
Multiplication
Modulo, remainder of dividing x by y

Increment and Decrement


Operator Description
++
Adds one to the variable and
stores and stores the new
value in that variable
-Removes one from the
variable and stores the new
value in that variable
+=
Adds the specified number to
the variable and stores the
new value in that variable
-Removes the specified
number to the variable and
stores the new value in that
variable
L. Dodds, October 2002

Example
x++
y-x += 5
y -= 5

2/17

Introduction to Java Programming


*=

Handout 336895255.doc

Multiplies the variable by the


specified number and stores
the new value in that variable
Divides the variable by the
specified number and stores
the integer result in that
variable

/=

x *= 2
y /= 2

Boolean
!=
>
<
>=
<=
==

not equal to
greater than
less than
greater than or equal to
less than or equal to
equal to

Note that when testing equality of objects, use the equals method. E.g.
object1.equals(object2). This will test whether the objects are equal in
terms of their attributes, whereas using == will only test whether the two
variables are actually references to the same object.

Logical
Operator
&&
||
!
&

Description
Example
logical AND
A && B
logical OR
A || B
logical NOT (logical negation)
!A
boolean logical AND
true & true
(both operands must be booleans)
^
boolean logical exclusive OR
false ^ false
(both operands must be booleans)
|
boolean logical inclusive OR
true | false
(both operands must be booleans)
Note that similarly to the assignment operators, there are short forms available for
working with the logical operators. E.g. x |= y perform a logical OR of x and y
and then assigns this new value to x.

Shift
Operator
>>
<<
>>>

Use

op1 >> op2


op1 << op2
op1 >>> op2

L. Dodds, October 2002

Operation
shift bits of op1 right by distance op2
shift bits of op1 left by distance op2
shift bits of op1 right by distance op2 (unsigned)

3/17

Introduction to Java Programming

Handout 336895255.doc

<variable
if( <condition>
name 1> )
= {
(<new type>)<variable
ifif(
(shoppingCart.isEmpty())
name
<condition>
2>; if
float
<variable>
myString
if(
) {&&
(x
myFloat;
<condition>
instanceof
y)
instanceof
{
) {
String
<classname>
<statementsToDoIfConditionTrue> {
<statementsToDoIfConditionTrue>
int
/* code
myInt;
<statementsToDoIfConditionTrue>
if true */
} else {
} else
/*
codeif(
is shopping
<otherCondition>
myFloat
}
}
cart
= (int)myInt;
empty
) {
<statementsToDoIfConditionFalse>}
<statementsToDoIfOtherConditionTrue>
}
else
} else {
{
<statementsToDoIfNoneAreTrue>
/*
} code if shopping cart is not empty
}

Casts
Example:

Casting does not change an object, just the type used to reference it.
Casting from one primitive type to another may cause some loss of precision.
When casting an object, if the cast is not legal then a ClassCastException
is thrown.

Instanceof
The instanceof operator checks whether one object is an instance of another,
Example:

An object is an instance of a class if it directly or indirectly descends from that


class.

Flow Of Control
If Statement
If-Else:

If-Else-If:

Examples:

L. Dodds, October 2002

4/17

Introduction to Java Programming

Handout 336895255.doc

for (<counter
switch
<selector>
initialization>;
{
switch (shoppingCart.size())
//count
<loop
from one
continuation
to ten
condition>; <counter
increment>)
case <value
{ 1> :
{
for
(int i=1; i<10; i++) {
/* Java<statement
statements1>;
*/
case 1:
System.out.println(i);
}
break;
} {
case <value 2> :
showMessage(You have one item in your shopping cart);
<statement 2>;
break;
break;
}
case <value n> :
case 2:
<statement n>;
{
break;
showMessage(You have two items in your shopping cart);
default :
break;
<statement n+1>
}
break;
default:
}
{
showMessage(You have no items in your shopping cart);
}
}

Switch Statement
Example:

The break statement causes the program to proceed with the first statement after
the switch structure. The break statement is used because otherwise the
cases of the switch statement would otherwise run together. If break is not
used anywhere in a switch structure, then each time a match occurs in the
structure, the statements for all the remaining cases will be executed (or until a
break is encountered).
The <selector> for a select statement must be an integer, char, or an
enumerated type.
If the <selector> does not match any of the values, then the default statement
is executed.

For Loop
Example:

<counter initialization> is a variable of an ordinal type, such as an


integer initialized to a value.
The <loop continuation condition> determines how long the
program will continue to loop.
<counter increment> increments the counter using an increment
operator, usually <counter>++.
When the for structure begins executing, the counter is initialized. Then, the
<loop continuation condition> is checked. If the condition is

L. Dodds, October 2002

5/17

Introduction to Java Programming

Handout 336895255.doc

while (<continuation condition>) { int num = 0;


/* Java statements */
while (num < 10) {
}
/* Java statements */
num++;
}

satisfied, then the statements in the loop body are executed. Finally the
counter is incremented. The loop begins again by checking the <loop
continuation condition>. This process continues until the control
variable no longer satisfies the <loop continuation condition>.
The program then continues by performing the first statement after the loop.
A break statement inside a for loop causes the loop to end
A continue statement inside a for loop skips the remaining statements in
the body of that structure, and proceeds with the next iteration of the loop

While Loop
Example:

<continuation condition> is a variable or expression that evaluates to a


boolean.
<continuation condition> is tested before the body of the loop is
executed. If <continuation condition> is true, the body is executed.
When the loop body has been executed, the <continuation condition> is
checked again. The loop continues to execute until the <continuation
conditon> is false.
A break statement inside a while loop causes the loop to end
A continue statement inside a while loop skips the remaining statements in the
body of that structure, and proceeds with the next iteration of the loop

Variables
Primitive Data Types
There are eight primitive data types in Java
Type
Boolean

Description
A Boolean value (true or false)

Size
1 bit. true or
false

Byte
Short
Int
Long
Float
Double

L. Dodds, October 2002

Byte-length integer
Short integer
Integer
Long Integer
Single precision floating point
number
Double precision floating point

1 byte
2 bytes
4 bytes
8 bytes
4 bytes
8 bytes

6/17

Introduction to Java Programming

Handout 336895255.doc

<modifiers> <data
<datatype>
type>[]
<variable
<array name>
name>;=<modifiers>
<expression>;
new
{<comma
<data
separated
type>[<array
<data type>[]
list of
size>];
<array name>;
values>};
<array name> = new <data type>[<array size>];

Char

number
A single character

2 bytes.
Unicode
character
As well as primitive types, Java has the notion of reference types. Arrays,
classes and interfaces are all reference types. I.e. they refer to an object in
memory rather than a primitive value. A reference type is equivalent to a pointer
in C, but the objects are referenced by name rather than memory location.

Declaring a Variable
or, to initialize the variable when its declared:

An instance variable describes the state of an instance. It is declared within


the scope of a class. If an instance variable is given an initial value, that value is
stored in the variable before the first statement of the constructor of the object is
executed.
A local variable is a temporary storage place of information. It is declared
within the scope of an individual method.
<modifiers> is an optional space-separated list of valid modifiers. Only
instance variables can have a list of modifiers (e.g. private final). See Modifiers.
<datatype>, either the name of a class or a primitive type, declares to which
type of class the variable will refer to, or which primitive type the variable will
be.
<variable name> is any valid identifier which is unique to the current scope.
<expression> is an optional variable, function, or parameter - or any
combination of them, with operators and parentheses - that evaluates to the same
type as <datatype> and whose value the variable should assume immediately
upon creation.

Declaring an Array
or, to allocate the size of the array when its declared:
or, to initialize the array when its declared:
Examples:

L. Dodds, October 2002

7/17

Introduction to Java Programming

Handout 336895255.doc

final
private
//
<visibility
creates
<datatype>
int[]
amodifier>
five
myArray;
myArray
<constant
element
=//
static
new
declares
array
name>
int[20];
final
of
= <constant
the arrayand
integers
<datatype>
value>;
<constant
initializes
name> = com.mydomain.package1
myArray
//
<constant
the elements
= value>;
new int[20];
to 1, 2,
//3,
allocates
4, 5
the array to hold 20 ints
com.mydomain.package2
private int[] myArray = {1, 2, 3, 4, 5};

Arrays are zero-based.


<modifiers> is an optional space-separated list of valid modifiers. See
Modifiers.
<data type>, either the name of a class or a primitive type, declares to what
type the array will hold references.
<variable name> is any valid identifier which is unique to the current scope.
<array size> is an integer that allocates the size of the array (how many
references it can hold).
<comma-separated list of values> are items of the declared data
type. This list initializes the size of the array to the number of values, and
initializes the elements of the array to those values.
Arrays have a length property which hold their size, e.g. myArray.length
Rather than writing your own code to copy one array into another use the
System.arraycopy() method.

Declaring Constants
A class constant:
A constant variable inside an object or method:

Use class constants for global variables that must be shared by all instances of a
class.
Use constant variables inside an object to ensure that the variable is only ever
assigned a single value.

Packages
Naming
A package is a group of Java classes. All classes within a package must have a unique
name. The standard convention is to name packages using a reverse domain name
format. For example if I own the domain name mydomain.com then I would give my
packages names like

L. Dodds, October 2002

8/17

Introduction to Java Programming

Handout 336895255.doc

package <package name>;import


<modifiers>
<package
class
name>.<class
<class name>
name>;
{
import
/*
Instance
<package
variables
name>.<interface
declared and
name>;
methods defined here */
import <package name>.*;
}

This convention has arisen to avoid naming clashes in code created by different
companies.
Of course if you dont own a domain name then youre free to name your packages as
appropriate. Using the application name as a prefix is a good option. As a rule you should
always place your code in a package.
Note: when your code is compiled in Java classes, the Java compiler will organise the
classes into directories according to the package names. E.g. all classes in the
com.mydomain.package1 package will be compiled into the
com/mydomain/package1/ directory.

Defining a Package
The package statement must appear before any other Java statements in the file, or be
omitted entirely. If the package statement is omitted, then a file is assumed to be in the
default package, to which all package-less classes belong.
All files in the same package must use the same <package name>.

Importing a Package
Classes in a package must be referenced by their fully qualified class name (e.g.
com.mydomain.package1.MyClass) unless that package is imported.

<package name> is a valid package identifier which is not the current


package.
<class name> is any public class defined in <package name>.
<interface name> is any public interface defined in <package name>.
An import that uses only a class or interface name only imports that single class.
Using an asterisk will cause all classes in that package to be imported.
Java always imports java.lang.* for every file at compile-time.

Classes and Interfaces


Defining a Class
Example:

L. Dodds, October 2002

9/17

Introduction to Java Programming

Handout 336895255.doc

<modifiers>
public
classinterface
myClass extends
{<interface
<modifiers>
public
superClass
interface
class
name>
interface
class
MotorVehicle
extends
implements
myInterface
<class
<interface
<superinterface
name>
extends
myInterface
{ extends
name>
Vehicle
{
list>
<superclass
{ {
name> {
} Class
/*
instance
constants
variables
and*/
abstract
/*
}
Abstract
MY_CONSTANT
methods
methods
declared
= 10;
and class
here */
constants declared here */
} int
/* constructors */
}
} void myMethod();
/* methods */
}
}

For definition of <modifiers> see Modifiers.


A class definition normally contains one or more constructors. If a constructor is
not defined, the Java compiler will supply a default constructor automatically.

Defining an Interface
Example:

For definition of <modifiers> see Visibility Modifiers.


A method declared in an interface cannot have a default implementation, nor can
it be declared private, protected, static, final, or
synchronized.
An instance variable declared in an interface is considered a class constant. It
cannot be declared private, protected, or synchronized.
All methods declared in an interface are implicitly abstract and public.
All instance variables declared in an interface are implicitly public,
static, and final, and therefore must include an initial value in the
declaration.

Extending a Class
Example:

<class name> will inherit all non-private methods and instance variables
defined in <superclass name>. Thus the methods and instance variables
defined for <class name> need be only those things needed to specialize the
class.
A class that implements interfaces can also inherit from a superclass. The
superclass must be extended before the interfaces are implemented:

Extending an Interface

L. Dodds, October 2002

10/17

Introduction to Java Programming

Handout 336895255.doc

public class
<modifiers>
interface
class
<return
myClass
myInterface
<class
types>
type>
implements
name>
<method
<method
extends
implements
myInterface
name>(<parameter
name>()
superInterface
<interface
{{
list>)
{list>{{
private void myMethod() {
/* statements
Class constants
Instance
variables
*/
anddeclared
abstractand
methods
methods
declared
defined
here
here
*/*/ /* statements */
}
}

Example:

<superinterface list> is a comma-separated list of interfaces from


which this interface should inherit.
<interface name> will inherit all the method declarations and class
constants in each of the interfaces listed in <superinterface list>. Thus
the methods and class constants defined for <interface name> need be only
those things needed to specialize the interface.

Implementing an Interface
Example:

<interface list> is a comma-separated list of interfaces which the class


should implement. It is expected that a class which implements an interface will
provide a definition for every method declared in that interface. If it does not, then
the class must be declared abstract.
<class name> has direct access to all class constants defined in the interfaces
it implements.

Methods
Defining a Method
Example:

<modifiers>. See Modifiers.


<return type> is either void (meaning that this method does not return a
value), a name of a class, or a base type.
<method name> is any valid identifier which is unique to the class. It names a
block of statements which, when executed in order, define a behavior for the
class.

Defining a Method with Parameters


Where <parameter list> is structured as follows:

L. Dodds, October 2002

11/17

Introduction to Java Programming

Handout 336895255.doc

<parameter
public
voidtype1>
class
drawCircle(int
myClass
<parameter
{
radius,
name1>,
Color
<modifiers>
<variable
colour)
name>
{
<class
=Shape
new
name>(<parameter
<class
circle;
name>();
list>) {
<parameter
/*
/*
statements
Instance
type2>
*/ <parameter
variables
declared
name2>,
*/ /* statements to circle
initialize
= new
the
Shape();
object */
<parameter typeN> <parameter nameN> }
}
public myClass() { // the Constructor
/* statements to initialize objects of myClass */
}
}

Example:

A method can be overloaded. This means that multiple methods with the same
name but different parameter lists can be declared within the same class. The
JVM will determine the correct method to run by examining the parameters
passed used in the method call. Methods cannot be overloaded to have different
return types.

Constructors
Example:

A constructor is a method with the same name as the class.


The constructor is invoked automatically each time an object of that class is
instantiated.
Constructors may be overloaded to provide a variety of means for initializing an
object.
Constructors cannot specify return types or return values.

Objects
Creating (Instantiating) an Object
Example:

<variable name> is a variable or parameter of the same type as <class


name>.
When executed, Java will create a new instance of <class name> and call the
appropriate constructor for the <class name> on the new instance. A reference
to the new object is stored in <variable name>.

Modifiers
There are various modifiers that can be applied when declaring classes, interfaces,
methods and variables. These are reviewed in the following sections. In some cases
modifiers have subtly different meaning depending on the type of structure (e.g. class or
method) to which theyre being applied.
L. Dodds, October 2002

12/17

Introduction to Java Programming

Handout 336895255.doc

public <other modifiers> class <class name> {


/* Instance variables declared and methods defined here */
}

Visibility Modifiers
Visibility modifiers control the encapsulation of a class, its attributes and behaviours
Public

A method which is declared public is


visible to every object, including those
outside of the package in which the class
which contains the method is defined.
Likewise, an instance variable declared
public can be accessed and modified by
any object. You should never do
this, as it breaks
encapsulation.

Protected

Subclasses inherit public methods and


instance variables and have full access to
them.
A protected method is accessible by any
object in the same package, or by any
subclass, regardless of package.
A protected instance variable can also be
accessed and modified by any object in the
same package.

Private

None

Subclasses inherit protected instance


variables and have full control over them.
A private method or instance variable is
visible only to the object in which it is
defined or declared.
Subclasses do not inherit private methods
or instance variables.
Default, or package scope.
The method or variable is only visible to
objects in the same package.

Public Classes and Interfaces


Declare the class or interface as public to ensure that it can be accessed from outside its
package. This is the most common way of declaring Java classes.

L. Dodds, October 2002

13/17

Introduction to Java Programming

Handout 336895255.doc

public<other
abstract
final
interface
class
<other
public
Circle
modifiers>
modifiers>
class
<interface
{ Shape
<data
<return
class
<return
class
name>
{
type>
<class
type>
<class
{type>
<constant
abstract
name>
<method
name>
<method
{ public
{ name>();
name>
name>(<parameter
=
void
<constant
animate();
final
final
list>)
value>;
int
void
constantNum
{myMethod()
= {
1;
/* Instance
Interface
Java
statements
variables
definition
*/ declared
here */and methods defined */
here */
here.
*/ /* Java statements */
}
}

Example:

By defining a class as public, a class in another package can instantiate that


class, inherit from it, maintain a reference to an instance of it, or access its static
methods and instance variables (if any).
By defining an interface as public, an interface in another class can inherit from
it or have a reference to an instance of it as a parameter, and a class in another
package can implement it, maintain a reference to an instance of an
implementation of it, or access it class constants.

Abstract Classes and Methods


Abstract method:
Abstract class:
Examples:

A method which is declared abstract is not followed by a definition; instead, the


declaration line simply ends in a semicolon. This indicates that a subclass must
provide an implementation of this method, otherwise it cannot be instantiated.
A class which is declared abstract cannot be instantiated. It is available for subclassing only. An abstract class often has one or more abstract methods.
A class or method cannot be declared as both abstract and final.
A variable cannot be declared abstract.

Final Classes Methods and Variables


Final variable:
Final method:
Final class:
Examples:

L. Dodds, October 2002

14/17

Introduction to Java Programming

Handout 336895255.doc

publicclass
final
static
<modifiers>
<other
void
myClass
<return
myMethod()
modifiers>
{ throws
type>
<return
<data
<method
Exception
type>
type>
name>(<parameter
<variable
<method
{
name>(<parameter
name>;
list>) throws
staticlist>)
<list
voidmyNumber;
int
myMethod()
{
{
of Java
/*
Instance
Throwables>
statements
variables
{
here
*/
declared
*/
and methods defined here *//* Java statements */
/* Java statements */
}
}
}

The final modifier can be used with variables to create constants. A value must
be assigned to the variable at declaration, and then cannot be changed. See also
Declaring Constants
A final method cannot be redefined in a subclass.
A final class cannot be subclassed.

Static Methods and Variables


Static method:
Static variable:
Examples:

A method which is declared static can be accessed without first creating an


instance of the class which contains it. This is because static methods and fields
belong to the class and not individual objects of that class.
While an instance of a class normally gets its own set of instance variables when
it is created, there exists only one instantiation of a static variable for all
instances of the class. Therefore, if one instance changes the value of a static
variable, that change is seen by other instances of the class.
A static method can refer to other methods and instance variables of the class
which contains it only if they are also declared static.
A static method is implicitly final, so it cannot also be declared abstract, and
cannot be redefined in a subclass. Inclusion of the final modifier is optional.

Exceptions
Declaring a method to throw an Exception
Example:

Every method that throws exceptions should declare what exceptions it throws
in the method declaration. A method that calls another method must handle the
exceptions thrown by that method.
<list of Throwables> is a space-separated list of the class Throwable
and its subclasses.

L. Dodds, October 2002

15/17

Introduction to Java Programming

Handout 336895255.doc

try { e;
throw
new//
<Throwable>;
where e is an
if instance
try
/*
//
/**
(problemEncountered)
This
{
is also
aof
comment.
the
a comment.
Throwable
Useful
Useful
class
for commenting
for
or one
commenting
ofaits
block
a single
of code*/
line
subclasses
/*
Statements which may{throw
readFile();
*
Thisan
isexception
a comment*/
which will appear in the
}
throw
}
* auto-generated
new Exception();
documentation produced by Javadoc.
catch(<Throwable> <exception
}* If handler
catch
(Exception
it
appears
1>)before
e)
{ {
a class, method or constant
/* Statements to do if an
/*exeption
*
declaration
Statements
of type
to doThrowable
if problem
1 is
encountered
thrown */reading file */
}
log(Problem reading file);
*/
finally {
}
/* Statements to do regardless
finally of
{ whether or not an exception is thrown.
*/
closeTheFile();
}
}

Throwing an Exception
or
Example:

<Throwable> must be the Throwable class or one of its subclasses.


When a method throws an exception, it should declare the exceptions it may
throw in the method declaration using the throws statement.

Handling (Catching) an Exception


Example:

The basic structure is: try to do something, catch exceptions that signal
problems, finally do some clean-up.
It is legal to have multiple catch blocks, each of which handle a specific type of
exception.
Its possible to omit the catch block and just have a tryfinally structure. This is
recommended where some clean-up is essential and must be done even if the
JVM encounters problems.

Miscellaneous
Comments

Keywords
A summary of the Java keywords. Note that const and goto are reserved but are not
actually implemented.
L. Dodds, October 2002

16/17

Introduction to Java Programming

abstract
boolean
break
byte
case
catch
char
class
const
continue

default
do
double
else
extends
final
finally
float
for
goto

Handout 336895255.doc

if
implements
import
instanceof
int
interface
long
native
new
package

private
protected
public
return
short
static
super
switch
synchronized
this

throw
throws
transient
try
void
volatile
while

Legal Names

Valid identifiers must be a letter, underscore, or dollar sign followed by any


number and combination of letters and numbers.
An identifier cannot be a keyword.
A class name must be unique to the package in which it is defined.
No two methods can have the same name and parameter list.
No two instance variables can have the same name.
No two local variables can have the same name

L. Dodds, October 2002

17/17

You might also like