DOC PREVIEW
Modifications to Java

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Modifications to Java Since Version 1.0,Proposed Modifications to Java,and Titanium Compatibility With JavaAmir Kamil, Titanium Group, UC BerkeleyJuly 1, 2003The Java language has been modified substantially since the original 1.0 version, and additional modifi-cations are planned in the future. These changes, unfortunately, are not documented in a central location onSun’s website. Therefore, for the benefit of Java and Titanium developers, the following is a compilation1ofthe various changes to Java, with overviews of each modification, and the status of Titanium compatibilitywith each new feature.1 Modifications to Java Since 1.0The Java language was changed substantially between versions 1.0 and 1.1, with the introduction of enclosedclasses. Other, less significant features, such as instance initiliazers and the assert keyword, were also addedin Java 1.1 and later versions.1.1 Lexical ChangesVery few changes to the lexical structure of Java have been made since version 1.0. Two keywords have beenadded, strictfp and assert.1.1.1 strictfpThe strictfp keyword was introduced in Java 1.2 as a modifier. It forces methods it modifies to adhereexactly to the IEEE 754 standard for all floating point operations. It can also be applied to classes, inwhich case all floating point operations in the class will be strictly IEEE 754. Individual variables cannotbe declared strictfp.1.1.2 assertUntil Java 1.4, the language contained no built-in facilities for assertions, prompting many programmers towrite their own assertion methods. Java 1.4 introduced assert as a keyword, making it incompatible withprevious code that used it as an identifier.Two assert constructs were introduced, both of which throw AssertionErrors when the assertion fails.The first isassert [boolean expr];This construct throws an AssertionError if the given boolean expression evaluates to false. The secondconstruct is1The information contained in this document is accurate to my knowledge as of the date of its writing.1assert x != 0;assert repOk() : "Representation failure";Figure 1: The two assert constructs.assert [boolean expr] : [expr];where the second expression must have a value (cannot be void). This construct, before throwing anAssertionError, evaluates the second expression, converts it to a String, and packages it into the AssertionError.Examples of using these constructs are given in figure 1.1.2 New ConstructsA flurry of new constructs was introduced in Java 1.1, but since then only the assert construct (describedabove) has been added. The most significant new Java 1.1 construct was the enclosed class. Additionalconstructs were instance initializers, anonymous array expressions, and class literals.1.2.1 Enclosed ClassesThe most significant addition in Java 1.1 was the various types of enclosed classes2. These classes are definedinside another class, called the enclosing class. Enclosed classes are mostly useful for organization, thoughthe fact that they have access to private members of the enclosing class is also useful.1.2.1.1 Nested ClassesThe simplest type of enclosed class is the nested class. Such a class is defined within another class, with theclass definition either modified by the static keyword or implicitly static. There are only minor differencesbetween nested and normal classes. A nested class has access to private members of its enclosing class, andthe enclosing class has access to private members of the nested class. References to the nested class followthe usual rules for static members, i.e. must be preceded by the enclosing class’s name followed by the dotoperator. As with other static members, the nested class can be accessed by its simple name within thedefinition of the enclosing class. Figure 2 shows an example of a nested class.Since a nested class is a static member of the enclosing class, an instance of the nested class is notassociated with any instance of the enclosing class. As such, the nested class can be instantiated withoutany instances of the enclosing class in existence.1.2.1.2 Inner ClassesAn inner class is similar to a nested class. It is defined as a nested class but without the static key-word modifying the definition. References to an inner class follow the same rule as references to a nestedclass, and the access rules between the inner class and the enclosing class are the same as well. Un-like a nested class, however, an instance of an inner class always has an associated instance of its en-closing class (like all instance members). Thus an inner class cannot be instantiated from a staticcontext in the enclosing class or at all from an external class, except through an instance of the en-closing class. The syntax for instantiating an inner class through an instance of its enclosing class is[enclosing instance].new [inner class constructor]. Figure 3 shows an example of defining andusing an inner class.Since an instance of an inner class has an associated instance of its enclosing class, it is possible for theinner class to access members of the enclosing class directly as if they were its own. The reverse, of course,2The Java language specification uses the term nested classes to refer to these types of classes. However, this term isoverloaded to mean enclosed classes that are declared static, so we will not use it as a synonym for enclosed class.2class Outer {static class Nested {void baz(Outer out) {int b = out.x; // allowed access to private member in Outer...}private int y;}static void foo() {Nested n = new Nested(); // can be accessed by simple nameint a = n.y; // allowed access to private member in Nested...}private int x;}class SomeOtherClass {void bar() {Outer.Nested n = new Outer.Nested(); // cannot be accessed by simple name...}}Figure 2: Definition and access rules for nested classes.class Outer {class Inner {void baz(Outer out) {int b = x; // allowed direct access to private member in Outer...}private int y;}void foo() { // not static since cannot access inner class from static contextInner in = new Inner(); // can be accessed by simple nameint a = in.y; // allowed access to private member in Inner...}private int x;}class SomeOtherClass {void bar() {Outer.Inner in; // cannot be accessed by simple namein = new Outer().new Inner(); // must be created through instance of Outer...}}Figure 3: Definition and access rules for inner classes.3class Outer {void foo(final int z, int w) {class Local {void baz(Outer out) {int b = x; // allowed direct access to private


Modifications to Java

Download Modifications to Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Modifications to Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Modifications to Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?