DOC PREVIEW
Princeton COS 333 - Java history

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java history• invented mainly by James Gosling ([formerly] Sun Microsystems)• 1990: Oak language for embedded systems– needs to be reliable, easy to change, retarget– efficiency is secondary– implemented as interpreter, with virtual machine• 1993: run in a browser instead of a microwave– renamed "Java"– Java Virtual Machine (JVM) runs in browser• 1994: Netscape supports Java in their browser–enormous hype: a viable threat to Microsoft• 1997-2002: Sun sues Microsoft multiple times over Java– MSFT guilty of anti-competitive actions– mostly settled by 4/04• significant language changes in Java 1.5 (9/04)– generics, auto box/unbox, for loop, annotations, ...– Java 1.6 (== 6.0) 12/06 is mostly incremental changesJava vs. C and C++• no preprocessor– import instead of #include–constants use static final declaration• C-like basic types, operators, expressions– sizes, order of evaluation are specified• object-oriented– everything is part of some class– objects all derived from Object class– klunky mechanisms for converting basic <-> object• references instead of pointers for objects– null references, garbage collection, no destructors– == is object identity, not content identity• all arrays are dynamically allocatedint[] a; // a is now null a = new int[100];• strings are more or less built in• C-like control flow, but– labeled break and continue instead of goto–exceptions: try {…} catch(Exception) {…}• threads for parallelism within a single processBasic data types• Java tries to specify some of the unspecified or undefined partsof C and C++• basic types: – boolean true / false (no conversion to/from int)– byte 8 bit signed– char 16 bit unsigned (Unicode character)– int 32 bit signed– short, long, float, double• String is sort of built-in (an Object)– "..." is a String– holds 16-bit Unicode chars, NOT bytes– does NOT have a null terminator; String.length() returns length– + is string concatenation operator; += appends– immutable: string operations make new stringsClasses & objects in Java• everything is part of some object– all classes are derived from class Object• member functions & variables defined inside class– internal functions should not be public, variables should never be public• every object is an instance of some class– created dynamically by calling new• class variable: a variable declared static in class– only one instance in entire program, exists even if the class is never instantiated– the closest thing to a global variable in Javapublic class RE {static int num_REs = 0;public RE(String re) {num_REs++;...}public static int RE_count() {return num_REs;}Class methods• most methods associated with an object instance• if declared static, amounts to a global functionclass RE {public boolean equals(RE r) {return re.equals(r.re);}public static boolean equals(RE r1, RE r2) {return r1.re.equals(r2.re);}public static void main(String[] args) {RE r1 = new RE(args[0]);RE r2 = new RE(args[1]);if (r1.equals(r2)) ... // member functionif (equals(r1, r2)) ... // static functionif (r1 == r2) ... // object equality}• some classes are entirely static members and class functions, e.g., Math, System, Color– can't make a new one: no constructorScope and visibility• only one public class per file– public class hello { } has to be in hello.java• public methods of the class are visible outside the file• other methods are not– default is file private• other classes in a file are visible within the file• but not visible outside the file• variables of a class are always visible within the class• and to other classes in the same file unless private• static variables are visible to all class instancesclass Math {public static double PI = 3.141592654; // etc.}double d = Math.cos(Math.PI);Destruction & garbage collection• interpreter keeps track of what objects are currently in use• memory can be released when last use is gone– release does not usually happen right away– has to be garbage-collected• garbage collection happens automatically– separate low-priority thread does garbage collection• no control over when this happens– can set object reference to null to encourage it• no destructor (unlike C++)– can define a finalize() method for a class to reclaim other resources,close files, etc.– no guarantee that a finalizer will everbe called• garbage collection is a great idea– but this does not seem like a great designI/O and file system access• byte I/O for raw data– read(), write(), InputStream, OutputStream• character I/O for Unicode (Reader, Writer)– InputReader and OutputWriter– InputStreamReader, OutputStreamWriter– BufferedReader, BufferedWriter• byte-at-a-time I/O– System.in, .out, .err like stdin, stdout, stderr– read() returns next byte of input, -1 for end of file– any error causes an I/O Exceptionimport java.io.*;public class cat1 {public static void main(String args[]) throws IOException {int b;while ((b = System.in.read()) >= 0)System.out.write(b);}}Buffered byte I/O to/from files• buffering is usually required; too slow otherwiseimport java.io.*;public class cp2 {public static void main(String[] args) throws IOException {int b;FileInputStream fin = new FileInputStream(args[0]);FileOutputStream fout = new FileOutputStream(args[1]);BufferedInputStream bin = new BufferedInputStream(fin);BufferedOutputStream bout = new BufferedOutputStream(fout);while ((b = bin.read()) > -1)bout.write(b);bin.close();bout.close();}}Exceptions• C-style error handling– ignore errors -- can't happen– return a special value from functions, e.g.,-1 from system calls like open(), NULL from library functions like fopen()• leads to complex logic– error handling mixed with computation– repeated code or goto's to share code • limited set of possible return values– extra info via errno and strerr: global data– some functions return all possible valuesso no possible error return value is available• exceptions are the Java solution (also in C++)• exception indicates unusual condition or error• occurs when program executes a throwstatement• control unconditionally transferred to catchblock• if no catchin current function, passes to calling method• keeps passing up until caught– ultimately caught by system at top leveltry {…} catch {…}• a method can catch exceptionspublic void


View Full Document

Princeton COS 333 - Java history

Download Java history
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 Java history 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 Java history 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?