Language fundamentals

 In this section you should be able to:

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

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

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

4.      Identify all Java programming language keywords.

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

6.      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.

 

Language Fundamentals

·        Identifiers are names of variables, methods, classes etc. When writing with Java™ code, certain rules must apply.

o       Each character is either a letter, number, underscore ( _ ) or currency symbol ( $, £ etc.)

o       The first character of an identifier cannot be a number.

o       The identifier name cannot be a reserved word.

 

·        A keyword or reserved word has special meaning and cannot be used as a user defined variable. A list of these is shown below. It is important to remember these as there most certainly will be a question on it in the exam.

 

abstract

do

implements

protected

throws

boolean

double

import

public

transient

break

else

instanceof

return

true

byte

extends

int

short

try

case

false

interface

static

void

catch

final

long

strictfp

volatile

char

finally

native

super

while

class

float

new

switch

 

const

for

null

synchronized

 

continue

goto

package

this

 

default

if

private

throw

 

 

·        Important: note the following

o       const and goto are not currently in use

o       null, true and false are reserved literals but can be considered as reserved words for the purpose of the exam.

o       The Java™ language is case sensitive so although super is a keyword, Super is not.

o       All the Java™ keywords are in lower case.

 

·        A literal in Java™ denotes a constant value. So for example 2 is an integer literal, ‘c’ is a character literal, “This is a string” is a string literal and the reserved literals true and false are reserved for Boolean literals.

 

·        Integer literals can also be specified as Octal (base 8 with a prefix of 0), or hexadecimal (base 16 with a prefix of 0x). (When writing code and using the 0 and 0x, the ‘0’ is a zero, not the letter ‘o’). So for example the integer 3 can also be represented as 03 (oct) and 0x3 (hex).

 

·        Java™ support comments. There are three types.

o       A single line comment which begins //

o       A multi-line comment. The comment is enclosed between /* and */

o       A documentation or javadoc comment is enclosed between /** and */. These comments can be used to generate HTML documents using the javadoc utility which is part of the Java™ language.

 

·        Java™ supports the following primitive data types.

Data type

Size (bits)

Size (bytes)

Range of values

Possible Uses

boolean

1

 

True or false

On/Off flags

byte

8

1

-128 to +127

ASCII characters

short

16

2

-32,768 to 32,767

Counters

char

16

2

0 to 65535

ASCII characters

int

32

4

-2,147,483,648 to 2,147,483,647

Large numbers

long

64

8

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Populations

float

32

4

-1.0x1039 to +1.0x1039

Scientific/Financial

double

64

8

-1.0x10317 to 1.0x10317

Scientific/Financial

 

·        Corresponding to all the primitive types there are wrapper classes defined. These classes provide useful methods for manipulating primitive data values and objects.

Data type

Wrapper class

byte

Byte

short

Short

char

Character

int

Integer

long

Long

float

Float

double

Double

 

·        Instance variables (data members of a class) and static variables are initialised to default values. Local variables (i.e. variables defined inside blocks or methods) are not initialised to default values. Local variables must be explicitly initialised before they can be used. If local variables are used before initialisation, a compilation error is generated. The defaults for instance and static variables are shown in the table below.

Data type

Default value

boolean

false

byte

0

short

0

char

‘\u0000’

int

0

long

0L

float

0.0F

double

0.0D

String or Object References

null

                       

public static void main(String [] args)

{

     int i;

     System.out.println( i );

}

            In this example printing of i generates a compilation error because local variable of i is used before being initialised.

            The initialisation of instance and static variables is an important concept both for understanding of Java™ language, and for the SCJP exam.

 

·        A Java™ source file has the following elements in this specific order

o       An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is – package testpackage

o       Zero or more import statements. The import statements make any classes defined in the specified package directly available. For example if a Java™ source file has statement importing the class “java.class.Button”, then a class in the file may use Button class directly without providing the names of the package which defines the Button class. Some examples of import statements are

1.  import java.awt.*;           //all classes in the awt package are imported

2.  import java.applet.Applet;   //the Applet class alone is imported

o       Any number of class and interface definitions may follow the optional package and import statements

If a file has all three of the above constructs, they must come in the specific order of package statement, one or more import statements followed by any number of class or interface definitions. Also all of the above three constructs are optional, so an empty file is a legal java file.

 

·        The java interpreter executes a method called main, defined in the class specified in the command line arguments. The main method is the entry point for execution of a class. In Java™ the main method must have the following signature

public static void main(String[] args)

The java interpreter is invoked with the name of the class as an argument. The class name can be followed by possible arguments for the main function of the class. When a Java™ program is invoked then the java interpreter name java and the class name are not passed to the main() method of the class. The rest of the command line arguments are passed as an array of String. For example

Java Sky blue gray

Would invoke the main method of the Sky class with an array of two elements “blue” and “gray”.

            There is a difference with Applets where the entry point to the program is accessed through a method called init() instead of main().

 

 

back