## TABLE OF CONTENTS # 1. Java Basics 2. Classes, Objects, References, Arguments, Return Values, ArrayLists, Interfaces, Inheritance, and Polymorphism 3. Object References on the Stack 4. Constructors and the Garbage Collector 5. Static and Final 6. Exception Handling 7. Events 8. Graphics 9. Layout Managers 10. Object Serialization - Saving Objects 11. Serialization and File I/O 12. Networking and Threads 13. Package, Jar, and Deployment 14. Remote Deployment with RMI 15. Servlets and JSP ## 1. JAVA BASICS # * Statements end in a semicolon: `;` * Code blocks are defined by a pair of curly braces: `{}` * Declare an int variable with a name and a type: `int x` * The assignment operator is one equals sign: `=` * The equals operator uses two equals signs: `==` * A while loop runs everything within its block (defined by curly braces) as long as the conditional test is true. * If the conditional test is fa1se, the while loop code block won't run, and execution will move down to the code immediately after the loop block. * Put a boolean test inside parentheses: `while (x == 4) {}` * Your Java program should start with a high level design. * Typically you'll write three things when you create a new class: prepcode, testcode, real (Java) code. * Prepcode should describe what to do, not how to do it. Implementation comes later. * Use the prepcode to help design the test code. * Write test code before you implement the methods. How many hits did you get last month? * Choose for loops over while loops when you know how many times you want to repeat the loop code. * Use the pre/post increment operator to add 1 to avariable: `x++;` * Use the pre/post decrement to subtract 1 from a variable: `x--;` * Use `Integer.parseInt()` to get the int value of a String. * `Integer.parseInt()` works only if the String represents a digit ("0", "1", "2", etc.). * Use `break` to leave a loop early (i.e. even if the boolean test condition is still true). ## 2. CLASSES, OBJECTS, REFERENCES, ARGUMENTS, RETURN VALUES, ARRAYLISTS, INTERFACES, INHERITANCE, AND POLYMORPHISM # * Object-oriented programming lets you extend a program without having to touch previously tested, working code. * All Java code is defined in a class. * A class describes how to make an object of that class type. A class is like a blueprint. * An object can take care of itself; you don’t have to know or care how the object does it. * An object knows things and does things. * Things an object knows about itself are called instance variables. They represent the state of an object. * Things an object does are called methods. They represent the behavior of an object. * When you create a class, you may also want to create a separate test class which you’ll use to create objects of your new class type. * A class can inherit instance variables and methods from a more abstract superclass. * At runtime, a Java program is nothing more than objects ‘talking’ to other objects. * When you don’t want a class to be instantiated (in other words, you don’t want anyone to make a new object of that class type) mark the class with the abstract keyword. * An abstract class can have both abstract and non-abstract methods. * If a class has even one abstract method, the class must be marked abstract. * An abstract method has no body, and the declaration ends with a semicolon (no curly braces). * All abstract methods must be implemented in the first concrete subclass in the inheritance tree. * Every class in Java is either a direct or indirect subclass of class `Object` (`java.lang.Object`). * Methods can be declared with Object arguments and/or return types. * You can call methods on an object only if the methods are in the class (or interface) used as the reference variable type, regardless of the actual object type. So, a reference variable of type `Object` can be used only to call methods defined in class Object, regardless of the type of the object to which the reference refers. * A reference variable of type `Object` can’t be assigned to any other reference type without a cast. A cast can be used to assign a reference variable of one type to a reference variable of a subtype, but at runtime the cast will fail if the object on the heap is NOT of a type compatible with the cast. Example: `Dog d = (Dog) x.getObject(aDog);` * All objects come out of an `ArrayList` as type `Object` (meaning, they can be referenced only by an `Object` reference variable, unless you use a cast). * Multiple inheritance is not allowed in Java, because of the problems associated with the “Deadly Diamond of Death”. That means you can extend only one class (i.e. you can have only one immediate superclass). * An interface is like a 100% pure abstract class. It defines only abstract methods. * Create an interface using the interface keyword instead of the word class. * Implement an interface using the keyword implements: Example: `Dog implements Pet` * Your class can implement multiple interfaces. * A class that implements an interface must implement all the methods of the interface, since all interface methods are implicitly public and abstract. * To invoke the superclass version of a method from a subclass that’s overridden the method, use the super keyword. Example: `super.runReport();` * Variables come in two flavors: primitive and reference. * Variables must always be declared with a name and a type. * A primitive variable value is the bits representing the value (5, 'a', true, 3.1416, etc.). * A reference variable value is the bits representing away to get to an object on the heap. * A reference variable is like a remote control. * Using the dot operator (`.`) on a reference variable is like pressing a button on the remote control to access a method or instance variable. * A reference variable has a value of `null` when it is not referencing any object. * An array is always an object, even if the array is declared to hold primitives. * There is no such thing as a primitive array, only an array that holds primitives. * Classes define what an object knows and what an object does. * Things an object knows are its instance variables (state). * Things an object does are its methods (behavior). * Methods can use instance variables so that objects of the same type can behave differently. * A method can have parameters, which means you can pass one or more values in to the method. * The number and type of values you pass in must match the order and type of the parameters declared by the method. * Values passed in and out of methods can be implicitly promoted to a larger type or explicitly cast to a smaller type. * The value you pass as an argument to a method can be a literal value (2, 'c', etc.) or a variable of the declared parameter type (for example, X where X is an int variable). There are other things you can pass as arguments, but we're not there yet. * A method must declare a return type. A `void` return type means the method doesn't return anything. * If a method declares a non-`void` return type, it must return a value compatible with the declared return type. * `ArrayList` is a class in the Java API. * To put something into an `ArrayList`, use `add()`. * To remove something from an `ArrayList`, use `remove()`. * To find out where something is (and if it is) in an `ArrayList`, use `indexOf()`. * To find out if an `ArrayList` is empty, use `IsEmpty()`. * To get the size (number of elements) in an `ArrayList`, use the `size()` method. * To get the length (number of elements) in a regular old array, remember, you use the `length` variable. * An Arraylist resizes dynamically to whatever size is needed. It grows when objects are added, and it shrinks when objects are removed. * You declare the type of the array using a type parameter, which is a type name in angle brackets: Example: `ArrayList