the language
play

The language COMP 520 Fall 2008 The JOOS language (2) The Java - PDF document

COMP 520 Fall 2008 The JOOS language (1) The language COMP 520 Fall 2008 The JOOS language (2) The Java language: was originally called Oak; was developed as a small, clean, OO language for programming consumer devices; was built


  1. COMP 520 Fall 2008 The JOOS language (1) The language

  2. COMP 520 Fall 2008 The JOOS language (2) The Java language: • was originally called Oak; • was developed as a small, clean, OO language for programming consumer devices; • was built into the Webrunner browser; • matured into Java and HotJava; • is now supported by many browsers, allowing Java programs to be embedded in WWW pages; • is also used by web servers, even if the client user is not running Java; and • is the implementation language for several large applications.

  3. COMP 520 Fall 2008 The JOOS language (3) Basic compilation ( .java → .class ): • Java programs are developed as source code for a collection of Java classes; • each class is compiled into Java Virtual Machine (JVM) bytecode; • bytecode is interpreted or JIT-compiled using some implementation of the JVM; • Java supports a GUI; and • many browsers have Java plugins for executing JVM bytecode.

  4. COMP 520 Fall 2008 The JOOS language (4) Major benefits of Java: • it’s object-oriented; • it’s a “cleaner” OO language than C ++ ; • it’s portable (except for native code); • it’s distributed and multithreaded; • it’s secure; • it supports windowing and applets; • it’s semantics is completely standardized; • it has a huge class library; and • it’s finally finally finally officially open source.

  5. COMP 520 Fall 2008 The JOOS language (5) Java security has many sources: • programs are strongly type-checked at compile-time; • array bounds are checked at run-time; • null pointers are checked at run-time; • there are no explicit pointers; • dynamic linking is checked at run-time; and • class files are verified at load-time.

  6. COMP 520 Fall 2008 The JOOS language (6) Major drawbacks of Java: • it misses some language features, e.g. genericity (until 1.5), multiple inheritance, operator overloading; • it does not have one single standard ( JDK 1.0.2 vs. JDK 1.1.* vs. . . . ) and probably never will; • it can be slower than C++ for expensive numeric computations due to dynamic array-bounds checks; and • it’s not JOOS.

  7. COMP 520 Fall 2008 The JOOS language (7) Goals in the design of JOOS: • extract the object-oriented essence of Java; • make the language small enough for course work, yet large enough to be interesting; • provide a mechanism to link to existing Java code; and • ensure that every JOOS program is a valid Java program, such that JOOS is a strict subset of Java.

  8. COMP 520 Fall 2008 The JOOS language (8) Programming in JOOS: • each JOOS program is a collection of classes; • there are ordinary classes which are used to develop JOOS code; and • there are external classes which are used to interface to Java libraries. An ordinary class consists of: • protected fields; • constructors; and • public methods.

  9. COMP 520 Fall 2008 The JOOS language (9) $ cat Cons.java public class Cons { protected Object first; protected Cons rest; public Cons(Object f, Cons r) { super(); first = f; rest = r; } public void setFirst(Object newfirst) { first = newfirst; } public Object getFirst() { return first; } public Cons getRest() { return rest; } public boolean member(Object item) { if (first.equals(item)) return true; else if (rest==null) return false; else return rest.member(item); } public String toString() { if (rest==null) return first.toString(); else return first + " " + rest; } }

  10. COMP 520 Fall 2008 The JOOS language (10) Notes on the Cons example: • fields in JOOS must be protected : they can only be accessed via objects of the class or its subclasses; • constructors in JOOS must start by invoking a constructor of the superclass, i.e. by calling super(...) where the argument types determine the constructor called; • methods in JOOS must be public : they can be invoked by any object; and • only constructors in JOOS can be overloaded, other methods cannot.

  11. COMP 520 Fall 2008 The JOOS language (11) Other important things to note about JOOS: • subclassing must not change the signature of a method; • local declarations must come at the beginning of the statement sequence in a block; and • every path through a non-void method must return a value. (In Java such methods can also throw exceptions.)

  12. COMP 520 Fall 2008 The JOOS language (12) The class hierarchies in JOOS and Java are both single inheritance, i.e. each class has exactly one superclass, except for the root class: ✦ ✦ � ❅ ✦ ✦ ✦ � ❅ ✦ � ❅ � ❅ � ❅ � ❅ ✦ ❛❛❛❛❛ ✦ ✓ ❙ ✦ ✦ ✦ ✓ ❙ ✦ ✓ ❙ ❛ The root class is called Object , and any class without an explicit extends clause is a subclass of Object .

  13. COMP 520 Fall 2008 The JOOS language (13) The definition of Cons is equivalent to: public class Cons extends Object { ... } which gives the tiny hierarchy: Object public String toString(); public boolean equals(Object obj); Cons public void setFirst(Object newfirst); public Object getFirst(); public Cons getRest(); public boolean member(Object item); public String toString();

  14. COMP 520 Fall 2008 The JOOS language (14) The class Object has two methods: • toString() returns a string encoding the type and object id; and • equals() returns true if the object reference denotes the current object. These methods are often overridden in subclasses: • toString() encodes the value as a string; and • equals() decides a more abstract equality. When overriding a method, the argument types and return types must remain the same. When overriding equals() , hashcode() must also be overridden: equal objects must produce the same hashcode.

  15. COMP 520 Fall 2008 The JOOS language (15) Extending the Cons class: $ cat ExtCons.java public class ExtCons extends Cons { protected int intField; public ExtCons(Object f, Cons r, int i) { super(f,r); intField = i; } public void setIntField(int i) { intField = i; } public int getIntField() { return(intField); } }

  16. COMP 520 Fall 2008 The JOOS language (16) The extended hierarchy: Object public String toString(); public boolean equals(Object obj); Cons public void setFirst(Object newfirst); public Object getFirst(); public Cons getRest(); public boolean member(Object item); public String toString(); ExtCons public void setIntField(int i); public int getIntField();

  17. COMP 520 Fall 2008 The JOOS language (17) Using the Cons class: $ cat UseCons.java import joos.lib.*; public class UseCons { public UseCons() { super(); } public static void main(String argv[]) { Cons l; JoosIO f; l = new Cons("a",new Cons("b",new Cons("c",null))); f = new JoosIO(); f.println(l.toString()); f.println("first is " + l.getFirst()); f.println("second is " + l.getRest().getFirst()); f.println("a member? " + l.member("a")); f.println("z member? " + l.member("z")); } } A Java program (not an applet) requires a main() method. It is necessary to import library functions such as println() .

  18. COMP 520 Fall 2008 The JOOS language (18) Compile and run the UseCons program: $ javac joos/lib/*.java $ joosc UseCons.java Cons.java $ java UseCons The UseCons program builds these objects: equals() equals() equals() "a" "b" "c" l setFirst() setFirst() setFirst() first first first rest rest rest member() member() member() The output of the UseCons program is: a b c first is a second is b a member? true z member? false

  19. COMP 520 Fall 2008 The JOOS language (19) Types in JOOS are either primitive types: • boolean : true and false ; • int : − 2 31 . . . 2 31 − 1 ; • char : the ASCII characters; or user-defined class types; or externally defined class types: • Object ; • Boolean ; • Integer ; • Character ; • String ; • BitSet ; • Vector ; • Date . Note that boolean and Boolean are different.

  20. COMP 520 Fall 2008 The JOOS language (20) Types in Java and JOOS: • Java is strongly-typed; • Java uses the name of a class as its type; • given a type of class C , any instance of class C or a subclass of C is a permitted value; • there is “down-casting” which is automatically checked at run-time: SubObject subobj = (SubObject) obj; • there is an explicit instanceof check: if (subobj instanceof Object) return true; else return false; • and finally some type-checking must be done at run-time.

  21. COMP 520 Fall 2008 The JOOS language (21) Statements in JOOS: • expression statements: x = y + z; x = y = z; a.toString(l); new Cons("abc",null); • block statements: { int x; x = 3; } • control structures: if (l.member("z")) { // do something } while (l != null) { // do something l = l.getRest(); } • return statements: return; return true;

  22. COMP 520 Fall 2008 The JOOS language (22) Expressions in JOOS: • constant expressions: true, 13, ’\n’, "abc", null • variable expressions: i, first, rest • binary operators: || && != == < > <= >= instanceof + - * / % • unary operators: - !

  23. COMP 520 Fall 2008 The JOOS language (23) Expressions in JOOS: • class instance creation: new Cons("abc",null) • cast expressions: (String) getFirst(list) (char) 119 • method invocation: l.getFirst() super.getFirst(); l.getFirst().getFirst(); this.getFirst();

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend