12/27/09

Java Tips

  • Inheritance is the mechanism that a sub class inherit the behavior and attributes of a super class.
  • An interface is a clollection of methods that indicate a class has some behavior in addition to what it inherits from its superclasses. The methods included in an interface do not define this behavior, that task is left for the classes that implement the inteface. To use an interface, include implements keyword as part of your class definition
    public class AnimationSign extends javax.swing.JApplet
    implements Runable {...}
  • Overriding: the method created in the sub class has the same nane as the super class, this is called overrriding.
  • Package is a way of grouping related classes and interfaces. Such as: java.awt.*,java.lang.*.
  • Literal: A literal is any number,text,or other information that directly represents a value. include: number literal(1,2,3), boolean literal(true,false),character literal(\n,\t),string literal("sss").
  • A reference is an address that indicates where an object's variable and methods are stored.
  • Casting between primitive types enables you to convert the value of one type to another primitive type. such as from int to float. Sample: (int)(x/y),(classname)object,(typename)value.
  • To find out what an object's class is: String name = key.getClass().getName();
  • instanceof operator return true or false: "Texta" instanceof String //true
  • this keyword refer to the current object: t = this.x;
  • Overloading: Methods with the same name but with different number of arguments or/and data type.
  • Constructor mrthod is a method that is called on an object when it is created. The method has the same name as the class. this(arg1,arg2,arg3) can be used to call a constructor method. Constructor can be overloaded too.
  • Use the super keyword to call the original method from inside a method definition:
    void mymethod(String a, String b) {super.mymethod(a,b);}
  • finalizer method is called just before the object is collected for garbage and has its memory reclaimed.
    protected void finalize() throws Throwable {super.finalize();} Finalizer method is used for optimizing the removal of an object. In most cases, you don't need to use finalize(0 at all
  • Applet runs on browser that support Java.
  • Painting is how an applet displays something onscreen. repaint();
  • Java archive is a collection of Java classes and other files packaged into a single file.
    jar cf Animated.jar *.class *.gif
  • Swing is a part of the Java Foundation Classes library, provide a way to offer a graphical user interface. The swing is an extension of awt. javax.swing, javax.swing.JFrame,javax.swing.JWindow. Two other packages that are used with graphical user interface programming are java.awt and java.awt.event
  • Threads are parts od program that are set up to run on their own while the rest of program does something else. This is called multitasking because the program can handle more than one tasks simultaneously.
  • final class cannot be subclassed
  • You can define a class inside a class, as if it were a method or a variable.These classes are called inner classes. Inner class are invisible to all other classes,no need to worry about the name conflicts between it and other classes.
    public class Inner extends javax.swing.Applet {...}
  • Exception catch:
    try{}
    catch (IOExceptio e){}
    catch (ClassNotFoundException e){}
    catch (InterrptedException e){}
    finally{
    }
  • finnally statement is actually useful outside exceptions,you can aalso use it to execute cleanup code after s return,a break,or a continue inside loops.
  • The throw clause: public boolean myMethod(int x,int y) throws AnException {}
  • A digital signiture is an encriped file that accompany a program indicating exactly from whom the files come from. The document that represents this digital signiture is called a certificate.
  • java.io: FileInputStream and FileOutputStream - Byte streams stored in files on disk,CD-ROM,or other storage devices
  • java.io: DataInputStream and DataOutputStream - A filtered byte stream from which data such as integers and floating-point numbers can be read
  • Java handles access to external data via the use of a class of objects called streams. A stream is an object that carries data from one place to another.
  • Object serialization: the capability to reaad and write an object using steams.
  • Objects can be serialized to disk on a single machine or can be serialized across a network.
  • Persistence : the capability of an object to exist and function outside the program that created it. Serialization enables object persistence.
    FileOutputStream disk = new FileOutputStream("SavedObject.dat")
    ObjectOutputStream obj = new ObjectOutputStream(disk)
  • RMI(remote method invocation): creates Java application that can talk to other Java application over a network. RMI allows an application to call methods and access variables inside another application, which might be running in a different Java Envirnment or different OS altogether, and pass objects back and forth over a network connection.
    java.rmi.server, java.rmi.registry, java.rmi.dgc
  • Before code with RMI, use rmic command-line program to generate the Stub and Skeleton layers so that RMI can actually work between the two sides of the process
  • RPC(Remote Procedure calls): sends only procedure calls over the wire, while RMI actually pass the whole object.
  • Socket: Java provides the Socket and ServerSocket classes as an abstraction of standard TCP socket program techniques. The Socket class provides s client side socket interface similar to standard UNIX socket.
    Socket connection = new Socket(hostName, portNum);
    connection.setSoTimeOut(50000);
    BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
    DataInputStream in = new DataInputStream(bis);
    BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
    DataOutputStream out = new DataOutputStream(bos);
  • Sever Side sockets work similar to client side sockets, with the exception of the accept() method. The accept() method accepts a connection from that client..
  • JavaBeans is an architecture and platform independent set of classes for crreating and using Java software components. You can drop a JavaBean component directly into an application without writing any code.
  • Java Database connectiion, 2 ways: JDBC-ODBC bridge, or JDBC drivers
    JDBC-ODBC bridge allows JDBC drivers to be used as ODBC drivers by converting JDBC method calls into ODBC function calls. The next is a sample for JDBC-ODBC:
    Connection payday = DriverManager.getConnection("jdbc.odbc:Payroll","Doc","Notnow");
  • The next is a sample for JDBC:
    Connection payday = DriverManager.getConnection("jdbc:JDataConnect://localhost:1150/Presidents","","");
  • Java Data Structure
  • BitSet class implements a group of bits or flags that can be set an cleared individually.
    class Somebits {
        public static final int READABLE = 0;
        public static final int WRITABLE = 1;
        public static final int STREAMABLE = 2;

        public static final int FLEXIBLE = 3;
    }
  • Vectr class implements an extendable array of objects
    Vector v = new Vector();
    v.add("Bob");
  • Stacks are a classic data structure used, last in, first out
    Stack s = new Stack();
    s.push("One");
    s.push("two");
    String s1 = (String)s.pop();
  • Map interface defines a framework for implementing a basic key-mapped data structure.
  • Hashtable class is derived from dictionary, implements the Map interface, and provides a complete implementation of a key-mapped data structure.