<Previous    Back to Start  Contents   Next>


Language Fundamentals


Identify correctly constructed package declarations, import statements, class declarations (of all forms including inner classes) interface declarations, method declarations (including the main method that is used to start execution of a class), variable declarations, and identifiers.

Order of package declarations, import statements, public class declarations

The order is as follows: package declarations, import statements, then class definitions. The order of public vs. non-public class definitions does not matter. However, note that with Suns JDK, you can only have one top-level public class per source file, and the name of the file must be the same as the name of the public class. For example;

	package acme.applications.userinterfaces;
	import java.awt.*;
	public class SomeClass {
 // etc.
 }

Correct declaration for a main() method.

[The following rules apply to creating a main() method, which allows you to run the class as an application. You can create any number of methods called 'main' in a class (overloading), which take other arguments, are not public and static, or return a value. To be able to run the class, however, there must be one method called main that takes an array of Strings as an argument, and that method must be static, and not return a value. Otherwise you will get a runtime error when you try to execute the class]

The main() method must be static, not return a value, and take an array of strings.

Whether it is declared public is something of a controversy. The Java Language Specification clearly states "The method main must be declared public, static, and void. It must accept a single argument that is an array of strings." On the other hand, 'The Complete Java2 Certification Study Guide' by Roberts, Heller et al. states. "The main method is declared public by convention. However, it is a requirement that it be static...". Tests indicate that in Java 1.2 (and 1.3), you can execute a program whose main() method is private, as bizarre as that may seem. You cannot do this in Java 1.1. or in Java 1.4.1.
Exam candidates should follow the Java language specification, and assume public is required, as far as answering questions is concerned. Use the signature described below, in your code and in your exam answers.

Either of the following two examples are accepted ways of declaring main:

	public static void main(String[] args) 
	public static void main(String args[])

Note that 'args' is just a common name for the array, and you can call it anything you like. Also:

static public void main(String[] args)

is perfectly legal and will compile.

Legal and illegal identifiers

Identifiers may contain only letters, numbers, dollar signs, i.e. "$", or underscores, i.e. "_". The first character cannot be a number. Obviously an identifier cannot have the same spelling as a keyword or the boolean and null literals (true, false and null) as described earlier.


State the correspondence between index values in the argument array passed to a main method and command line arguments.

The command line arguments, after the java command and the name of the program, are placed in the String array, starting at array element zero. The first command line argument is at position 0 in the array, the second at 1 etc. The java command and its various settings are not included in the list.


Identify classes that correctly implement an interface where that interface is either java.lang.Runnable or a fully specified interface in the question.

There is only one method in the interface, run(), which must be implemented in the class.

public void run() {
	//Do something
}

Identify all Java programming language keywords. Note: There will not be any questions regarding esoteric distinctions between keywords and manifest constants.

Java keywords (check that you know what each of these does, if not, find out before you take the exam!):

abstract assert boolean break byte
case catch char class const
continue default do double else
extends final finally float for
goto if implements import instanceof
int interface long native new
package private protected public return
short static strictFP super switch
synchronized this throw throws transient
try void volatile while

The keywords goto and const are reserved but not used.

While they are not keywords, the boolean and null literals (true, false and null) cannot be used as identifiers. So fear ye not the "manifest constants" mentioned in the objective.
[Note that Roberts & Heller lists these as keywords, but they are literals, not keywords]

A new keyword in Java2 is strictFP. strictFP is not in versions of Java prior to Java2. It is included here for completeness, however, I cannot say whether it is examinable and you will have to look elsewhere for information on its syntax. This modifier tells the compiler to use a strict form of floating point calculations, which ensures an identical result on every platform. In Java 1.1, this strict form is always used, but in Java2, when you don't specify strictFP, the Java virtual machine can do a fast calculation, which makes the most of the performance of the native processor without the overhead of converting to a consistent cross-platform format.

[Technical aside, for those interested in microprocessors:
I believe part of the reason for its introduction is that the X86 CPUs have an 80bit FPU, versus 64bits used in the Java standard and on Suns processors. Intel complained that this gave their CPUs a disadvantage in Java]

assert is new to 1.4 - covered elsewhere in this document.

A brief note on some of the more obscure keywords:

transient - indicates a field should not be serialized when the class is serialized.
volatile - indicates the field may be changed frequently by different threads. Somewhat like a weak form of synchronization.


State the effect of using a variable or array element of any kind when no explicit assignment has been made to it.

Class level variables (variables declared in the class, but outside of any methods, i.e. static and instance variables) are automatically initialised to a default value, if no explicit assignment has been made. The defaults are 0 for numerical types, '\u0000' for chars, false for booleans, and null for objects.

The elements of an array are always (even inside a method) initialised to their default values when no explicit assignment has been made to the element.

If you do not initialise a variable declared in a method (local variables) to some value, you will get a compiler error.


State the range of all primitive formats, data types and declare literal values for String and all primitive types using all permitted formats bases and representations.

Range of primitive data types.

A guide to understanding the ranges is to remember that if a type is x bits in size, then it can hold 2x possible combinations of bits. 0 must be represented somehow, so that is why 1 is subtracted from the maximum value. Negative values must be represented, so you must take one away from the exponent. For example, a byte is 8 bit (28), but one bit is needed for the positive/negative, so the exponent is 7 (-27 to 27). Then we subtract one for zero, giving -27 to 27-1.

chars are an exception as they are unsigned, so there are no negative values. floats and doubles are more complex. They use some bits to store the binary digits and others to store the exponent, like a binary version of scientific notation (e.g. 1.2354*105). It is unlikely you will be required to know these for the exam.

Primitive Type Size Range of Values
byte 8 bit -27 to 27-1
short 16 bit -215 to 215-1
int 32 bit -231 to 231-1
long 64 bit -263 to 263-1
char 16 bit '\u0000' to '\uffff'(0 to 216-1 )
float 32 bit Max. positive value: (2-2-23)*2127. Min. positive value: 2-149
double 64 bit Max. positive value: (2-2-52)*21023. Min. positive value: 2-1074

Constructing literal numeric values using decimal, octal and hexadecimal formats.

Octal literals begin with zero e.g. 013042 (and obviously only digits 0-7 are allowed). Hexadecimal literals begin with zero and an 'x' e.g. 0x23e4A (digits allowed are 0-9 and a to f, the 'x' and the letters can be upper or lower case).

Construct literal String values using quoted format.

String duh = "This should be obvious, but I'll include an example anyway.";

Construct a literal value of char type using Java's unicode escape format for a specified character code.

Use \u followed by four hexadecimal digits representing the 16 bit unicode character e.g.

char x='\u1234'

Java also supports certain escape codes for special characters such as '\n' for newline. See a textbook for more.


<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".