CSC302 2006S, Class 17: Java as Object-Oriented Language Admin: * Congratulations to Peter! * Reading: Beyond Java 4, 5, 8. (Skipping Ruby.) * Reading for Monday: Forthcoming. * Homework: Semantics of D. * Friday: Movie about Java. (Guy Steele: Growing a Language) * Syllabus updated. Overview: * Left over from Monday: Polymorphism vs. Overloading * History: The development of Java * Your perspective: Why did Java do so well? * Context: What languages are currently popular? (and how do we measure popularity?) * Alternative views of object-orientation /Polymorphism vs. Overloading/ * Polymorphism: * A function can take one parameter of many types e.g., in Java you write things like public Number square(Number x) that works on Integer, BigInteger, Double, etc. * More generally, in object-oriented langauges values in subclasses can be used in place of the superclass * Consider interfaces to be a kind of superclass * Why do we like polymorphism? * Once we've written a polymorphic function (or other thing), we can reapply it to different things * Easier to read * Less duplicate code (compare to int square(int) + double square(double) + ....) * In Java, gives the programmer some freedom in the context of strict typing * Overloading: You can write more than one function with the same name, and have the compiler/interpreter choose the correct one int square(int) ... double square(double) ... ... x = square(x) calls the corret one of the above, depending on the type of x x + y could be a number, if x and y are numbers, or a string, if x or y is a string * A kind of polymorphism? /A biased and abbreviated history of Java/ * Java: Object-oriented, imperative, C syntax * White papers said: robust, secure, portable, ... * Developed at Sun in late 80's early 90's because Sun was thinking about the future * Enterprise application development * (often for Embedded computing) * Two "library research" questions * What features does such a language need? * Robust: Toasters shouldn't crash * Make it difficult for programmers to write incorrect programs * Secure: Only access what's reasonable (Sandboxing) * Portable * Small footprint (hah!) * What existing languages might suffice? * None! * Few have security features * None are portable * But C++ has some good features * Roll your own * Early 1990's: The Web appears * Whoops! Needs a programming language * Sun marketers: Web pages can be embeeded systems. Use our language. And give it a good name. Two of the main architects * James Gosling - Author of the horror that is Gosling Emacs (boo) * Guy Steele - Developer of Scheme (yay) Java became BIG. Why? * Leveraged C syntax that lots of programmers knew; shallow adaptation ramp. * Applets were cool. * Lots of programmers converted from C++ to Java because ...? * Portability was a big win. * Sandboxing seems important. * "I hate memory management." * Change in attitude * Good marketing! /Stone's new favorite bug/ * Domain: Comparing name entered on Web form to names previously used (for validation) * Usually, says two different names are the same * Usually, says two identical names are the same * Ocasionally, says two identical names are different * Found the last problem primarily with "Nancy" * Written in Perl Correct if ($newstr eq $oldstr) Incorrect, but "works" because of coercision if ($newstr == $oldstr)