CS 541 Meeting -*- Outline -*- * Java Overview refs: Gosling, Joy, Steele, The Java Language Specification, Addison Wesley, 1996 Arnold, Gosling, The Java Programming Language, Addison Wesley, 1996 ** Goals ------------------------------------------ GOALS OF JAVA "... a Java program should compute the same result on all machines and in all implementations" ("The Java Language Specification") - simplicity - avoid new, untested features - few implementation dependencies so very portable - avoid safety problems garbage collection checked array access - programmer productivity safety features exception handling multithreading ------------------------------------------ Q: Why does that quote seem important? because it's so unlike C or C++! Q: What's not here vs. C++? efficiency The general character of the language is "C++ simplified", although it's not clear that in the end it is simpler... ** Basics ------------------------------------------ PARTS OF THE LANGUAGE primitive expressions: data: values: boolean, int, short, long, char (unicode characters), float, double (IEEE 754 standard), references, null objects: arrays, class instances, variables assignments method calls means of combination: statements (if, while, ...), methods, arrays, classes, inheritance (extends) means of abstraction: classes, packages, interfaces, nested and anonymous classes, monitors (synchronzied methods and stmts) ------------------------------------------ The IEEE 754 standard seemed like a good idea, but I don't like the fact that == and != don't satisfy the usual mathematical properties for NaN what's more significant is what they left out, in search of simplicity ------------------------------------------ MISSING VS. C++ primitive expressions: data: values: pointers, pointer arithmetic, references to stack locations objects: unions, structs means of combination: goto (use labeled break and continue), data layout control, multiple inheritance (both kinds), private and protected inheritance means of abstraction: macros, templates, namespaces (but has packages), friends ------------------------------------------ and lots of syntax (C++ is HUGE!), although Java started out somewhat smaller, now Java is also big ** semantics of objects Java is basically like Smalltalk (and Lisp), unlike C++ ------------------------------------------ REFERENCES vs. POINTERS A *pointer* is A *reference* in Java is C++ code: int i = 3; int * ip = &i; int * p2 = new int; ip = ip + 1; p2 = p2 + 1; int & ir = i; int & r2 = *(new int); Java code: int i = 3; Integer j = new Integer(7); j = new Integer(8); j = null; ------------------------------------------ ... an address (potentially 0 or even bogus) ... either null or the address of some object draw pictures In Java, can't have a reference to a variable, can't have a reference to a non-object, variables contain pure values or references Summary: Java has pointers but not pointer arithmetic. ------------------------------------------ DOMAINS (KINDS OF VALUES) ExpressibleValue = PureValue + Reference PureValue = boolean + int + short + long + char + float + double Reference = Object + null DenotableValue = ExpressibleValue ------------------------------------------ Explain the terms Q: What's a denotable value in C++ that isn't expressible? arrays Objects include arrays, but not variables Q: What benefit from not having references to variables? Q: What benefit from not having pointer arithmetic? ------------------------------------------ PARAMETER PASSING AND ASSIGNMENT Java is a language with: direct model of arrays (Pascal, Ada): indirect model of arrays (C, C++, Scheme): ------------------------------------------ ... call by value and the indirect model of both arrays and objects ... array variables are bound directly to a sequence of cells, assignment and pass by value make copies no aliasing of array names ... array variables contain references to a sequence of cells (indirect binding) assignment and pass by value make aliases The main difference in Java is that almost everything uses the indirect model (references), as this is used for all objects copies are only made for pure values ------------------------------------------ EXAMPLE import java.util.*; class Demo { public static void main(String[] args){ Vector x, y; x = new Vector(); y = new Vector(); x.addElement(new Integer(3)); x.addElement(new Integer(4)); y = x; y.setElementAt(new Integer(7), 0); System.out.println(x.elementAt(0)); } } ------------------------------------------ draw a picture, discuss Summary, the basic semantics in Java is like Smalltalk (or LISP)