An Introduction to Java Statements

By Herbert J. Bernstein

© Copyright, Herbert J. Bernstein, 1999, 2002, 2003

This is a brief introduction to and summary of Java Statements. Some portions of the language have been glossed over or omitted here. There is a strong relationship to the statements in C++. For more detail on Java syntax, see [Joy et al., 00]. For information on C++ and C, see see [Ritchie, 78], [Stroustrup, 86] and [Gehani, 88].

Tokens

When we write in most languages, we use linear sequences of characters or glyphs, some of which are group together to make the words of the language and some of which are used as punctuation to help us organize those words. The words and punctuation are called "tokens".

In C amd C++, the characters used are drawn from a limited set of characters, that must, at a minimum include representations of

Java permits many more characters than this, drawn from a 16-bit Unicode character set. C and C++ tend to work with more limited characters sets, usually drawn from a simple 7-bit character set. There is increasing use of an 8-but multibyte Unicode character set called UTF-8 (see www.unicode.org).

The tokens are organized into keywords, identifiers, constants, strings, operators and other separators. The keywords are the reserved words of the language. The words that are used as keywords or reserved in C, C++ or java are: abstract, asm, auto, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, entry, enum, extends, extern, false, final, finally, float, for, friend, goto, if, implements, import, inline, instanceof, int, interface, long, native, new, null, operator, package, private, protected, public, register, return, short, signed, sizeof, static, strictfp, struct, super, switch, synchronized, template, this, throw, throws, transient, true, try, typedef, union, unsigned, virtual, void, volatile, while

Identifiers are sequences of letters, digits and underscores. The first character of an identifier must be a letter or an underscore. Other characters are permitted in identifiers in java (e.g. currency symbols are also treated as letters), but it is unwise to use them for code that is to be portable. The keywords and reserved words should not be used as identifiers.

Statements

Each portion of a Java program consists of a sequence of statements. Almost all valid C and C++ statements are valid Java statements. One major difference is that there is no goto statement in Java.

Almost all valid Java statements are valid C++ statement. The major exceptions are the synchronized, throw and try statements.

C and C++ have a preprocessor which is not available in Java.

The statements in Java may be organized into:

Comments

Comments may be of the form:

     // comment \n

or

      /* comment */

The first form allows a trailing comment on a single line, while the second form allows comments that span multiple lines.

Comments may appear anywhere.

Declarations

In most programming languages, declarations give the compiler information about the types, storage requirements and intial values of identifiers. Java emphasises object-oriented programming, and the major purpose of declarations in Java is to define classes.

The types in Java consist of primitive types (byte, short, char, int, long, float, double, boolean), class types, interface types and array types. Classes, interfaces and arrays are always referenced by what in C would be called a pointer or, in C++, a reference, and thus are called reference types. A special null type (the unnamed type of the special identifier null) is defined to provide a null reference for uninitialized reference types.

A class declaration is one of the forms:

modifiersopt class identifier class_body
modifiersopt class identifier extends type class_body
modifiersopt class identifier implements type_list class_body
modifiersopt class identifier extends type implements type_list class_body

Java does not permit multiple inheritance, but provides interface declarations instead:

modifiersopt interface identifier interface_body
modifiersopt interface identifier extends type_list interface_body

The optional extends clause specifies a single class of which the newly declared class is a subclass. The optional implements clause specifies a list of one or more interfacces for which this class is a subinterface.

The optional modifiers are zero or more of the following:

The class body is enclosed within curly braces and consists of zero or more declarations of methods and fields (variables of primitive types and instances of objects).

 

Method Declarations

A method defines an action to be performed by an instance of a class. A constructor is a special method to be invoked when an instances of the class created. The method declarations in Java look like function declarations from C or C++. However, there are no functions in java other than class methods. As with a C function, a Java method can accept a fixed number of positional arguments in the following form:

    modifiersopt type identifier ( 
        formal_argument_list ) throws_clauseopt{ 
        method_body }

The optional modifiers are zero or more of the following:

The optional throws_clause begins with the keyword throws and is followed by a comma-separated list of class types from the class Throwable

The type of the value returned by the method is given by type, which must either be a known type or the special type, void.

If a method does not have formal arguments, the identifier is followed by an empty pair of parentheses. This conflicts with current recommended C and C++ usage which would handle this case with identifier ( void ). When there are formal arguments, the formal_argument_list is a comma separated list of type declarations of formal parameters. The only optional modifier permitted before the type is final. Arrays are declared by one or more pairs of trailing empty square brackets.

 

Field Declarations

Java field declarations declare the variables and other storage to be used by instances of a class. These declarations look just like C and C+ variable declarations, but one must remember that they are actually the equivalent of declarations of the members of C struct types. The declarations take one of the forms:

modifiersopt type identifier ;
modifiersopt type 
identifier=initializer;

or the same forms with comma separated lists of identifiers or initialized identifiers, or the same forms with arrays indicated by tailing pairs of empty square brackets.

As with C and C++, arrays are defined by delcarations ending with any number pairs square brackets, but in Java, the square brackets are always empty, with arrays being intialized either with new or literal arrays given in curly braces. The optional modifiers are zero or more of the following:

Executable statements

See Conditionals.html and Loops.html for more on conditionals and loops.

Operators

C, C++ and Java have a rich repertoire of operators. C++ permits the programmer to overload the existing operators. Java does not. The operators on Java are:

  • Assignment Operators
    • = -- simple assignment
    • += -- add rhs, then assign to lhs
    • -= -- subtract rhs, then assign to lhs
    • *= -- multiply by rhs, then assign to lhs
    • /= -- divide lhs by rhs, then assign to lhs
    • &= -- bitwise and, then assign to lhs
    • |= -- bitwise or, then assign to lhs
    • ^= -- bitwise exclusive or, then assign to lhs
    • %= -- bitwise remainder, then assign to lhs
    • <<= -- left shift lhs by rhs, then assign to lhs
    • >>= -- signed right shift, then assign to lhs
    • >>>= -- unsigned right shift, then assign to lhs
  • Infix Operators
    • || -- logical or
    • && -- logical and
    • | -- integer bitwise or
    • ^ -- integer bitwise exclusive or
    • & -- integer bitwise and
    • == -- numerical equality
    • != -- numerical inequality
    • < -- less than
    • > -- greater than
    • <= -- less than or equal to
    • >= -- greater than or equal to
    • << -- left shift
    • >> -- signed right shift
    • >>> -- unsignd right shift
    • + -- addition or string concatenation
    • - -- subtraction
    • * -- multiplication
    • / -- division
    • % -- remainder
  • Infix Ternary Operator
    • ? : -- conditional expression
  • Prefix Unary Operators
    • ++ -- unary prefix increment
    • -- -- unary prefix decrement
    • ! -- unary logical negation
    • ~ -- unary bitwise complement
    • + -- unary plus (noop)
    • - -- unary negate
  • Postfix Unary Operator
    • ++ -- unary postfix increment
    • -- -- unary postfix decrement


    References

    Bill Joy (ed.), Guy Steele, James Gosling, Gilad Bracha, "Java(TM) Language Specification (2nd Edition)" (June 5, 2000) 544 pages, Addison-Wesley Pub Co, ISBN 0201310082

    [Ritchie, 78] Ritchie, Dennis M., "The C Programming Language -- Reference Manual", in the "Unix Programmer's Manual, Supplementary Documents", 4.2 Berkeley Software Distribution, Virtual VAX-11 Version, Computer Science Division, Dept. of Electrical Engineering and Computer Science, U. of California, Berkeley, CA, Section 1, March 1984.

    [Stroustrup, 86] Stroustrup, Bjarne, "The C++ Programming Language (3rd edition), Addison Wesley Longman, Reading, MA, 1997, ISBN 0-201-88954-4, 920 pp.

    [Gehani, 88] Gehani, Narain, "C: An Advanced Introduction (ANSI C Edition)", Computer Science Press, Inc., Rockville, MD, 1988, ISBN 0-7167-8193-X, 265 pp.,



    Last Updated on 22 September 2003
    By Herbert J. Bernstein
    Email:
    yaya@bernstein-plus-sons.com