Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CS320 Course Materials] >> [Java] >> java.semantics [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Thu Jan 22 13:43:06 PST 2004


    Some Preliminary Steps Towards a Semantics for the Java Language


      Two question marks ?? indicate that I haven't found the data yet. If you have found the answer in any official documentation please send it to where it will be incorporated with an acknowledgment.

      These are working notes and are being revised daily. I am open to corrections and improvements.

      See Also

      See [ java.syntax.html ] and [ java.glossary.html ]

      Data in Java

    1. data::=object| Basic_data_type.

      In Java there is a split between basic data types (int,char,...) and Objects [ Basic Data Types ] [ Objects] . A value, literal, variable,.... is always either one or the other. There is no overlap or confusion allowed. Further basic data types are privileged to be manipulated by operators. Objects are uniquely privileged to be manipulated by methods. Data types have many literals, but the only literal Object is

      However both Objects and basic data types can be the parameters and results of functions and can be stored in variables (or constants).

      Basic Data Types

        A type has a ready made symbol or name that is used to declare its variables. It has a size (in bits). It has a number of operators (prefix, postfix, and infix. There are certain functions and operators that return the values of that type.

        The basic types also have an interpretation, a set of constant values that can be coded in the language, and a large number of predefined operations.

      1. types::=numeric | character | boolean.
      2. numeric::=integer | floating.
      3. floating::=float | double.
      4. integer::= byte | short | int | long.
      5. character::= following
        1. symbol="char".
        2. size=16.bits.
        3. interpretation=Unsigned 16 bit, not ASCII but 16 bit Unicode.
        4. interpretation=0..2^16-1.

        (End of Net)
      6. numeric::=following
        1. symbol="byte" | "short"|"int"|"long".
        2. if symbol="byte"then size=8.bits.
        3. if symbol="short"then size=16.bits.
        4. if symbol="int"then size=32.bits.
        5. if symbol="long"then size=64.bits.
        6. interpretation=binary(length=>size, signed). Assigning a longer number into a short variable is done modulo the shorter size.

        7. operators= [ Expressions in java.syntax ]

        (End of Net)

      7. boolean::=following
        1. symbol="boolean".
        2. size=1.bits.
        3. storage=??.
        4. values={"true", "false"}.
        5. prefix={"!"}.
        6. infix={"&&", "||"}.
        7. priority= /("&&", "||").
        8. postfix={}.
        9. from_infix={"<", "<=", "==", "!=", ">=", ">"}.
        10. No conversion to numeric types.

        (End of Net)
      8. double::=following
        1. symbol="double", size=64.bits.
        2. IEEE 754.

        (End of Net)
      9. float::=following
        1. symbol="float", size=32.bits.
        2. IEEE 754.

        (End of Net)
        [ Math in java.classes ]

      . . . . . . . . . ( end of section Basic Data Types) <<Contents | Index>>


      [ Arrays in java.classes ]


      The basic types are not Objects. Basic type values are not values of Objects. Objects have user defined "methods" but basic data types do not. Objects do not have any operators(like +,-,...). All Objects are extended from the class Object. The symbol
      is a special value indicating that an Object has no value or does not exist. This value null can be assigned to an object of any class, but can not be assigned to any basic data type. However several basic data types have a class of of Objects associated with them. However several basic data types have a class of of Objects associated with them [ Object in java.classes ]

      There are a few classes of Object that encapsulate numeric basic types: [ Number in java.classes ]

      Conversions between classes and types

    2. cast::syntax= "(" classname ")" ref_to_object If B is a subclass of A and b is a reference to an object of type B and a one of type A, then (A)b casts b into an A and (B)a casts b as an A.


      The same name can be used to refer to different functions. The correct one is chosen by matching the call the functions with the same name and same number of arguments. The returned type is ignored. Perfectly matching types select the best method. However Java permits approximate matches - a byte for example may cast to a short if better match is found. This is governed by an arcane table of costs.

      Creating new objects

    3. creation::syntax= "new" C "(" arguments ")" the arguments are passed to a ,matching constructor function in class C
    4. C "(" formal_arguments ")".

      Objects are destroyed and recycled by finalization.


        An interface describes how a class can be used without describing how the class carries out the work. Several classes can have one interface. An interface can be formed by mixing in several other interfaces. All methods in an interface are public, variables can by public, private, or static. Methods in interfaces are without modifiers and function bodies.

      . . . . . . . . . ( end of section Interfaces) <<Contents | Index>>

      Programmer Defined Classes

        A programmer programs in Java solely by creating new classes. New classes are created by extending existing classes. The default is to extend the class Object. All classes - predefined or programmer created are extensions of the class Object. A new class can also implement zero or more interfaces.

        Each class defines a set of fields and a set of methods. The fields define the state of the objects. The methods define what the class will do to the state of the object. Methods can be accessible only with a class, within the same package, or only with derived classes, or by any part of any program...

      . . . . . . . . . ( end of section Programmer Defined Classes) <<Contents | Index>>

      Method invocations

        If an object x has class C and C has a method M with the right arguments and in the right scope then x.M(A) invokes method M of class C(C.M) on the xs data. The class C is determined dynamically. So if C is actually a subclass of B and 'x' is declared to be in B but is actually set to a C then it is C.M that is invoked. For example:
         		B x = new C();
         		B x = new B();
         		x.M(A); // calls C.M
         		y.M(A); // calls B.M

        Static Method Invocation

        A static method M of class C is called like this C.M(A) and if within scope the M found in the definition of C is invoked with arguments A passed by value. A static method of class C, invoked inside class C's definition can be written 'M(A)' and have the same effect.

        The variable this

        In a class the variable this refers to the currant object whose method is being executed. An expression or statement of form
      1. this.M(A) applies method M, with arguments A(called by value). The actual method invoked is determined dynamically by the class of object that this is.

        Local Methods

        In the piece of the source code that defines class C, an expression of form
      2. M(A) is interpreted as calling C.M(A) - class C's method M with arguments A (called by value).

        Notice that this.M(A) can call a different method M if this is actually an object in a subclass of C.

      . . . . . . . . . ( end of section Method invocations) <<Contents | Index>>

      See Also

      [ java.glossary.html ]

    . . . . . . . . . ( end of section Some Preliminary Steps Towards a Semantics for the Java Language) <<Contents | Index>>

Formulae and Definitions in Alphabetical Order