Archive for the 'language' Category


Creating Java Exception getStackTrace() as a string

Dec 17, 2012 in Java, language


public class t1 {
public static void main(String[] args) {
final Throwable t = new IllegalArgumentException(“hi!”);

StringBuffer sb = new StringBuffer();
for (int tx = 0 ; tx < t.getStackTrace().length ; tx ++ ) { sb.append(t.getStackTrace()[tx] ); sb.append("\n"); } System.out.println(sb); } } [/java] or, simpler: [java] System.out.println("STACK: [[" + java.util.Arrays.toString(Thread.currentThread().getStackTrace() ).replace(",", "\n") + "]]" ); [/java] . . .

Notes (Part 1) from HeadFirst Java..

Apr 08, 2005 in Books, Java, language

Notes from HeadFirst Java by Kathy Sierra & Bert Bates

  • — A method uses parameters. A caller passes arguments.
  • — All arguments are passed by value. References too
    but, as opposed to primitive parameters, references
    may be dereferenced and values in objects pointed to
    by the references can be changed.
  • — Instance variables are declared inside a class (not in a method).
    Instance variables get default values.
  • — Local variables are declared within a method.
    They must be initialized.
  • — crossword puzzle p.162
    1a. I can’t behave – primitive
    6a. Or, in the courtroom – object
    7a. Where it’s at, baby – indexof
    9a. A forks’ origin – if
    21a. 19’s counterpart (?) (Arrays extent) – length

    2d. Where the Java action is (What’s overridable?) – method
    3d. Addressable unit – element
    15d. As If – Virtual

  • — Inheritance tree design guidelines

    1. Look for objects that have common attributes and behaviors.
    2. Design a class that represents the common state and behavior.
    3. Decide if a subclass needs behaviors (method implementations)
    that are specific to that particular subclass type.
    4. Look for more opportunities to use abstraction, by finding
    two or more subclasses that might show common behavior.
    5. Finish the class hierarchy.

  • — The ‘Is-a’ and ‘has-a’ tests.
  • — DO use inheritance if:
    — A class is a more specific type of a superclass.
    — There is common behavior shared among classes.
  • — DO NOT use inheritance:
    — Just to reuse code from another class.
    — If the subclass does not pass the IS-A test with the superclass.
  • — An abstract class is a class that is declared abstract—it may
    or may not include abstract methods.
  • — Abstract classes cannot be instantiated, but they can be subclassed.
  • — An abstract method is a method that is declared without an
    implementation (without braces, and followed by a semicolon)
  • .. In Other Words ..

  • — An abstract class can only be used to be extended.
  • — An abstract method can only be used to be overridden .
  • — An abstract method does not have a body .
  • — A single method marked ‘abstract’ forces the class to become ‘abstract’.
  • — An abstract class may have both abstract and non-abstract methods.
  • — All abstract methods must be implemented by subclasses.
  • — Non-abstract classes are “Concrete” and can be instantiated.
  • Abstract Classes versus Interfaces


  • — Unlike interfaces, abstract classes can contain fields that are not static and final,
  • — and they can contain implemented methods.
  • — Such abstract classes are similar to interfaces, except that they provide a
    partial implementation, leaving it to subclasses to complete the implementation.
  • ** If an abstract class contains only abstract method declarations,
    it should be declared as an interface instead.
  • — Multiple interfaces can be implemented by classes anywhere
    in the class hierarchy, whether or not they are related to one
    another in any way. Think of Comparable or Cloneable, for example.
  • — By comparison, abstract classes are most commonly subclassed
    to share pieces of implementation. A single abstract class is subclassed
    by similar classes that have a lot in common (the implemented parts of
    the abstract class), but also have some differences (the abstract methods).