Posted on Mar 17, 2008

Random collection of notes from Sun Java course

These are some notes I scribbled down during Sun’s “Java Programming” class. Having programmed in Perl the last 7 years, this is what I wrote down as “noteworthy.”

  • Everything in Java is an object except primitive types
  • All objects are references in memory
  • A ‘foreach’ loop in java looks like this:

for (Car y: x) {

y.speed = 100;

}

  • Inherited objects always call the super classes constructor. The default is super() with no arguments. This can be overridden by placing a super(int i) as the FIRST EXECUTABLE STATEMENT in a subclass constructor.
  • All objects inherit from the base class ‘Object’.
  • obj instanceof class will return true or false based on whether obj is indeed an object of type class.
  • defining the toString method for object obj allows you to define what is printed when System.out.println(obj) is called.
  • The static keyword declares members that are associated with the class and not the class objects
  • The final keyword ensures a member can not be changed. This means that methods and classes that use final can not be overridden or extended.
  • The abstract keyword:
    • forces child classes to define a method.
    • classes with an abstract method must also be abstract.
    • abstract classes can not be instantiated directly (only via subclasses)
  • An interface is a class with only abstract methods
  • The implements class keyword is used for interfaces to enforce the rules of the interface. Unlike extends, implements can be used on more than one interface.
  • For handling exceptions, you can use try and catch blocks, or declare your method with a ‘throws‘ clause that sends the exception back to whatever called that method.