Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4044510

compiler permits using null literal in synchronized statement

XMLWordPrintable

    • 1.2fcs
    • sparc
    • solaris_2.5, solaris_2.6
    • Verified



      Name: ngC57085 Date: 04/11/97



      Java compiler (JDK1.0.2 - JDK1.1.2) permits to use null literal as expression
      in synchronized statement.

      JLS (14.17) says:

      " SynchronizedStatement:
                   synchronized ( Expression ) Block

      The type of Expression must be a reference type, or a compile-time error occurs."

      JLS (4.3) says:

      There are three kinds of reference types: class types, interface types, and array types.

      JLS (4.1) says:

      "There are two kinds of types in Java: primitive types and reference types.
      There are, correspondingly, two kinds of data values that can be stored in
      variables, passed as arguments, returned by methods, and operated on:
      primitive values and reference values.

           Type:
                   PrimitiveType
                   ReferenceType

      There is also a special null type, the type of the expression null, which
      has no name. Because the null type has no name, it is impossible to declare
      a variable of the null type or to cast to the null type. The null reference
      is the only possible value of an expression of null type. The null reference
      can always be cast to any reference type. In practice, the Java programmer
      can ignore the null type and just pretend that null is merely a special
      literal that can be of any reference type."

      See an example and compiler and JVM output below:

      ------------------------stmt10804--------------
      import java.io.PrintStream;

      public class stmt10804 {
        public static void main(String argv[]) {
           System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
        }
        public static int run(String argv[],PrintStream out) {
           int t = 10;
           synchronized (null) {
              out.println ( "failed" );
           }
           return 2/*STATUS_FAILED*/;
        }
      }
      --------------------------------------

      > javac112 -d . stmt10804.java
      > java112 -verify stmt10804
      java.lang.NullPointerException
      at stmt10804.run(stmt10804.java:14)
      at stmt10804.main(stmt10804.java:10)
      >



      ======================================================================

            tturnidgsunw Todd Turnidge (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: