Java for C++ Programmers Clint Jeffery University of Idaho
Object Oriented Programming Object Oriented Design comes first. Objects originated in application domains. OOP first appeared in SIMULA 67 classes and inheritance mixed w/ normal code Popularized by SmallTalk 80 everything is an object, even integers! Method call == “send an object a message”
History of Java Designed by Gosling et al at Sun ~1991 Original niche: embedded programming w/ GUIs, such as cable TV boxes Portability based on bytecode VM Happy Accident: retargeted for browsers just in time for Internet boom, 1995 Moves largely onto enterprise servers by 1999 or so Achieves 50+% university penetration, ~2005 Returns to its roots in Android, ~2008
Java Design Principles Simple, object-oriented, familiar Robust and secure Architecture neutral and portable Interpreted, threaded, and dynamic High performance (?)
Java as a Better C++ Familiar == C/C++-like syntax Tries to avoid main sources of bugs and security flaws in C++ code: pointers, memory allocation, type safety, preprocessor Tries to be more Object-Oriented: everything is in a class Tries to be easier and simpler than C++ Tries to be amenable to large projects worked on by worker-bee programmers
Java as a Worse C++ Slow, slow, and slow compared w/ C/C++ Despite garbage collection, you can still have memory problems Neither C++ nor Java retains backward compatibility w/ old code. Ridiculous class libraries...complex operations for simple tasks. Have to use their package/directory structure...like it or not.
Java Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } Must be in a HelloWorld.java Each.java can have its own main() Compile.java to.class with javac Run.class with java, or build a.jar
Java Hello World Notice how you can't write code outside a class, and can't write a class except in its proper file...but you are able to run main() even though there is no instance of class HelloWorld. Static class functions fly in the face of “pure OOP”. Also, Java's built-in types (like int) are not objects, so “more pure than C++” does not mean “pure OO”
Java Types byte, short, int, long, float, double, boolean, char (16-bit, Unicode) – Precision/size generally guaranteed across platforms Arrays – size fixed at creation Objects – instances of a class. Lots of important standard classes, like strings