The reason for this strange behavior is internal optimizations on the compiler—you don’t want to be
guessing what the compiler is doing, so don’t use ==!
Autoboxing: try a similar test with Integers (the wrapper class for ints). What happens? Can you explain
why ? (Hint: autoboxing and auto-unboxing!)
---------------------------------------------------------------------------------------------
4) Object-oriented Programming (OOP):
a) Approach: with a big application, break problem into smaller subproblems; assign responsibility to
each subproblem; keep each interface small.
i) Each subproblem has a specification: functionality (services code provides) and interface (input
code expects and output conditions guaranteed.) Implementation must meet specification.
ii) Specification should be separated from implementation (“data abstraction”) which makes code
more modular, easier to maintain, and allows change of implementation w/o changing interface
iii) OOP encourages data abstraction and modular code.
b) static: a static variable provides data abstraction, but means that there is only ONE variable
representing state in the class; they are used for representing states across all objects in the class(for
instance, the number of objects of the class that have been created)
c) static methods are not associated with an object instance, so they can only access static variables.
d) Instance variables: variables which are associated with a particular instance (object) of a class.
e) Instance methods: methods which are shared among all instances of same class, but the method can
reference instance variables—and therefore is tied to a particular object instance.
f) OOP provides encapsulation and extensibility
i) Encapsulation: permits code to be used without knowing implementation details
ii) Extensibility: permits behavior of classes to be changed/ extended w/o having to rewrite code of
class (don’t need to involve class implementer). Mechanism in Java: inheritance.
5) Inheritance: Classes form a hierarchy (tree) with class Object at root.
a) Each class has at most one superclass; each class has 0 or more subclasses.
b) In a subclass, all public methods and fields of the superclass are available. Even overridden methods
are available—you can access them using the keyword “super”.
c) Overriding: method declaration m in subclass B overrides a method m in superclass A if both
methods have same signature (same name, same class/instance type, same number/type of
parameters, same return type)
d) “IS-A” relationship; form hierarchies. Subclasses can add new fields and methods and can override,
but CANNOT remove fields. Must specify own constructors. Direct code reuse; indirect code reuse
(polymorphism for methods and type compatibility); note: sibling classes are not type compatible
with each other; both are w/ parent.
i) Constructor: automatic call to super() if none is given because that’s just the way it works! If no
super() on first line, compiler automatically calls it there.
ii) Final method: invariant method; cannot be changed by subclasses; static binding.
iii) Final class: cannot be extended (leaf classes)
iv) Static methods’ overriding is resolved at compile-time; others are resolved at run-time.
e) Overriding: when derived class provides a new method definition with same signature
i) Partial overriding: derived class method invokes base class method; uses super.methodName()
ii) At compile time, only visible members of static type of reference can appear at right of dot
operator (an Employee presently called a Person can’t use an Employee method) No automatic
downcasts
iii) Call if (whatever instanceOf ClassName) to avoid ClassCastException
iv) Arrays: will throw a ClassCastException during runtime if declared as Person[]= new Employee[5]
and you try to insert a Student