Java Lecture 4

[Previous Lecture] [Lecture Index] [Next Lecture]

LowerCaseReader.java

import java.io.*;

class LowerCaseReader extends FilterReader {
    public LowerCaseReader(Reader in) {
        super(in);
    }

    public int read()
        throws IOException
    {
        int c = in.read();
        if (c >= 0)
            c = Character.toLowerCase((char) c);
        return c;
    }

    public int read(char buf[], int off, int len)
        throws IOException
    {
        int nRead = in.read(buf, off, len);
        if (nRead > 0)
            for (int i = off; i < off + nRead; i++)
                buf[i] = Character.toLowerCase(buf[i]);
        return nRead;
    }
}

Packages


Access Control

Four levels of control on class members:
Class Derived Package Everywhere
public yes yes yes yes
private yes
protected yes yes yes
default yes yes

Two levels of control on classes/interfaces:
Package Outside Package
public yes yes
default yes no


Notes on Arrays


Final Keyword

final means an entity is never changed.

When applied to:


Inner (Nested) Classes

Four types of inner classes
  1. static inner class:
    declared inside a class using static
  2. inner (instance) class:
    delcared in a class (no static)
  3. local inner classes:
    declared in a method
  4. anonymous (local) inner classes:
    delcared in a new expression

Expr.java

class Expr {
  public static Expr create(String exprStr) {
      ExprTokenizer t = new ExprTokenizer(exprStr);

      Expr expr = parseOr(t);
      if (expr == null)
          return null;

      if (!t.currentMatches(ExprTokenizer.T_END))
          return null;

      return expr;
  }
  // ... rest of Expr

  static private class ExprTokenizer {
      public static final int T_END = 0;
      // ...

      public ExprTokenizer(String str) {
          // ...
      }

      public boolean currentMatches(int tok) {
          if (type == tok) {
              getNext();
              return true;
          }
          return false;
      }
      // ...
  }
}

Vector-inner.java

class Vector {
  // ...
  // Inner class
  class VectorEnum implements Enumeration {
      private int pos = 0;
      public boolean hasMoreElements() {
          return pos < size();
      }
      public Object nextElement()
          throws NoSuchElementException
      {
          if (pos >= size())
            throw new NoSuchElementException();
          return elementAt(pos++);
      }
  }

  public Enumeration elements() {
      // Creates VectorEnum tied to `this'
      return new VectorEnum();
  }

  // ...
}

Vector-local.java

class Vector {
  // ...

  public Enumeration elements() {
      // Local inner class
      class VectorEnum implements Enumeration {
          private int pos = 0;
          public boolean hasMoreElements() {
              return pos < size();
          }
          public Object nextElement()
              throws NoSuchElementException
          {
              if (pos >= size())
                throw new NoSuchElementException();
              return elementAt(pos++);
          }
      }
      // Creates VectorEnum tied to `this'
      return new VectorEnum();
  }

  // ...
}

Vector-anon.java

class Vector {
  // ...

  public Enumeration elements() {
      // anonymous inner class
      return new Enumeration() {
          // class is expected to implement
          // the Enumeration interface
          private int pos = 0;
          public boolean hasMoreElements() {
              return pos < size();
          }
          public Object nextElement()
              throws NoSuchElementException
          {
              if (pos >= size())
                throw new NoSuchElementException();
              return elementAt(pos++);
          }
      }
  }

  // ...
}

Javadoc Comments


Stack.java

/* Copyright (c) 1995, 1996 Sun Microsystems,
 * Inc. All Rights Reserved.
 * ...
 */

package java.util;

/**
 * The <code>Stack</code> class represents a
 * last-in-first-out (LIFO) stack of objects. 
 *
 * @author  Jonathan Payne
 * @version 1.16, 01/28/97
 * @since   JDK1.0
 */
public
class Stack extends Vector {
  /**
   * Pushes an item onto the top of this stack. 
   *
   * @param   item   the item to be pushed onto
   *          this stack.
   * @return  the <code>item</code> argument.
   * @since   JDK1.0
   */
  public Object push(Object item) {
      // ...
  }

  /**
   * Removes the object at the top of this
   * stack and returns that object as the value
   * of this function. 
   *
   * @return     The object at the top of this
   *             stack.
   * @exception  EmptyStackException  if this
   *             stack is empty.
   * @since      JDK1.0
   */
  public synchronized Object pop() {
      // ...
  }
  // ...

  /**
   * Returns where an object is on this stack. 
   *
   * @param   o   the desired object.
   * @return  the distance from the top of the
   *          stack where the object is]
   *          located; the return value
   *          <code>-1</code> indicates that
   *          the object is not on the stack.
   * @since   JDK1.0
   */
  public synchronized int search(Object o) {
      // ...
  }
}

[Previous Lecture] [Lecture Index] [Next Lecture]