Programming Languages

Java Tutorial - You think you got what it takes to learn the fine craft of Java? Well think no more! Find out first hand by evaluating this easy to read Java Tutorial. It is meant to be for the new comers to the world or Java. And I don't mean the coffee neither.

TW Tech Glossary - Misplaced your bible? Well here it is! This truly took a while to complete and should be used by all from beginners to advance techies. Look into it, you won't be sorry. (Very Resourceful)

Software Engineering Phases - There are four fundamental phases in most, if not all, software engineering methodologies. These phases are analysis, design, implementation, and testing. These phases address what is to be built, how it will be built, building it, and making it high quality…

JSP & JAVA Servlets - JSPs have dynamic scripting capability that works in tandem with HTML code, separating the page logic from the static elements ...

What is Java?

Java is a high-level programming language developed by Sun Microsystems. Java is an object-oriented language similar to C++, but simplified to eliminate language features that cause common programming errors. Java source code files (files with a .java extension) are compile into a format called byte code (files with a .class extension), which can then be executed by a Java interpreter. Compiled Java code can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (VMs), exist for most operating systems, including UNIX, the Macintosh OS, and Windows.

Java is a general purpose programming language with a number of features that make the language well suited for use on the World Wide Web. Small Java applications are called Java applets and can be downloaded from a Web server and run on your computer by a Java-compatible Web browser , such as Netscape Navigator or Microsoft Internet Explorer.


 

Tips

Vector (n) does not have n elements

In Java, arrays cannot be resized dynamically. If you want a dynamic data structure with random access, you use a Vector. A vector has a current size and a capacity, the number of elements it can ho1d without having to grow, again.

The constructor Vector (int n) builds a vector with capacity, not with size n, as a C programmer would certainly expect.
 

Vector v = new Vector (100);
v.setElementAt(new Double,3.14), 0,); // throws ArrayIndex OutOfBoundsException

If you want a vector with 100 elements, use
 

Vector v = new Vector(100);
v.setSize(100);
v.setElementAt(new Double(3.14), 0); // OK

Hassle-free array growth

Suppose you have an array of some type that is full, and you want to grow it.
 

Employee[] a = new Employee[100];
// array is full
int newLength = a.length * 11 / 10 + 10;
Employee[] newArray = new Employee[newLength];
System.arraycopy(a, 0, newArray, 0, a.length);
a = newArray;

That gets boring really quickly. Let's try to make it generic.
 

static Object[] arrayGrow(Object[] a) // not usefull
{
int newLength = a.length * 11 / 10 + 10;
Object[] newArray = new Object[newLength];
System.arraycopy(a, 0, newArray, 0, a.length);
return newArray;
}
Problem: Return type is Object[], not Employee [] .
a = (Employee[]) arrayGrow(a); // throws ClassCastException.

Use reflection to make a new array of the same type:
 

static Object arrayGrow(Object a) // useful
{ Class cl = a.getClass(); if (!cl.isArray()) return null; int length = Array.getLength(a);
int newLength = length * 11 / 10 + 10;
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newlnstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}

Typical usage:
 

Employee[] a = new Employee[100]; . . . . .// array is full
a = (Employee[]) arrayGrow(a);

This arrayGrow method can be used to grow arrays of any type, not just arrays of objects.
 

int[] ia = ( 1, 2, 3, 4 };
ia = (int[])arrayGrow(a);

Note that the parameter of arrayGrow is declared to be of type object, not an array of objects (Object [] ). The integer array type int [] can be converted to an object, but not to an array of objects!

Anonymous arrays

You can supply initial values to an array as follows:
 

 int[] primes = { 2, 3, 5, 7, 11, 13 };

If a function requires array parameters, then the traditional solution is to define and initialize an array variable and pass it to the function. For example, the getTables method of the DatabaseMetadata class requires an array of strings "TABLE", "VIEN", "ALIAS", ... to describe which kinds of tables are requested.
 

String [] tables = ( "TABLE", "VIEW" );
md.getTables(null, "%", "%", tables);

Rather than introducing a new variable that is only used once, use an anonymous array
 

new Type [] ( value1, value2, ... )
In our example,
md.getTables (null, "%", "%", new String[] { "TABLE", "VIEW" ) )
Use ""+x to convert x to a string

The base class Object has a method toString that subclasses redefine to print out a representation of the object. If x is any object, then x.to String()
converts it to a string. If x is a number, then you can't call x. toString (), because numbers aren't objects in Java, and you can't apply methods. Instead, you are supposed to use String.valueOf(x)

Just concatenate with an empty string:
 

 "" + x

This converts x to a string. If x is a number, then its value is turned into a string. If x is an object, then its toString method is invoked. There is no penalty. The Java compiler is aware of this idiom, and it does not generate code to concatenate an empty string.

The lazy programmer's version of tostring Background

It is boring to write toString for every class. The toString method usually just calls toString for every data field.
 

class Employee
{ public String toString()
{ return "Employee{ + "
"name=" + name + "," +
"salary=" + salary + "," +
"hireDay=" + hireDay + "}";
}
private Stzing name;
private double salary;
private Day hireDay;
}

Use the reflection feature to enumerate and print all data fields. Here we take advantage of the fact that a Hash table has a perfectly good toString method. The only drawback is that the fields are printed in random order.
 

 public String toString()
{ java.util.Hashtable h = new java.util.Hashtable();
Class cls = getClass();
Field[] f = cls.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < f.length; i++)
{ try ( h.put(f[i].getName () , f[i].get (this) ); }
catch (IllegalAccessException e) { }
}
if (cls.getSuperclass () . getSuperclass () ! = null)
h.put("super", super.toString(j);
return cls.getName() + h;
}

Here is a typical printout:
 

 Employee(hireDay=Day[1996,12,1], salary=35000.0, name=Harry Hacker}

In Java 1.1 , you must paste the code into each class - only the class itself can peek inside its private data. In Java 1.2, use Accessible Object. set Accessible and place the code in a convenient base class

Don't catch every exception

Many Java methods throw checked exceptions. You notice them when the compiler complains:
 

public void writeStack(DataOutput out)
{ for (int i = 0; i < 100; i++)
{ String s = (String) stk.pop(); // error: throws EmptyStackException
out.writeChars(n); // error: throws IOException
}
}

The first instinct is to surround each line with a try/catch clause.
 

 public void writeStack(DataOutput out)
{ for (int i = 0; i < 100; i++)
{ String s
try
{ s = (String)s.pop();
}
catch (EmptyStackException ese)
{// stack was empty
}
try
{ out.writeChars(s);
}
catch (IOExcep=ioe ioe)
{ // problem writing file
}
}
}

Whoa! Now the code looks really complicated. Exception handling is supposed to make exceptional situation simpler.

Find the correct place for handling the exception. Perhaps you want to have one try block that contains all commands in the method.
 

public void writeStack (DataOutput out)
{ try
{ for (int i = 0; i < 100; i++)
{ String s = (String)s.pop();
out.writeChars (s);
}
}
catch (EmptyStackException ese)
{ // stack was empty
}
catch (IOException ioe)
{ // problem writing file
}
}

Or even better, try to propagate the exception
 

 public void writeStack(DataOutput out)
throws EmptyStackException, IOException
{ for (int i = 0; i < 100; i++)
{ String s = (String)s.pop();
out.writeChars(s);
}
}

If you don't want to deal with exception handling...

When writing a quick and dirty program, it is a hassle to have to pay attention to all the exceptions that are thrown.
 

public void writeStack(DataOutput out)
{ for (int i = 0; i < 100; i-.+)
{ String s = (String) stk.pop(); // compiler complains: EmptyStackE,xception
out.writeChars(n); // compiler complains: IOException
}
}

Add throws Exception after every method, even main!
 

public void writeStack(DataOutput out) throws Exception
{ for (int i = 0; i < 100; itt)
{ String s = (String) stk.pop(); // compiler doesn't complain
out.writeChars(n); // compiler doesn't complain
}
}
public static void main () throws Exception
{ . . .
outFile = . . .
writeStack(outFile); // compiler doesn't complain
. . .
}

This is definitely a tactical move for prototyping only. Once you finished your code exploration, you should put the exception handling code at the proper place, and make the exception specifications of all methods precise.

Use printStackTrace when an exception hits

When an exception hits, you want to know the exact circumstances under which it occurred Of course, if you don't catch the exception, the program dies with a stack trace. But if you do catch it, how do you get the stack trace?

You can get a stack trace from any exception object with the printStackTrace method in the Throwable class. The following code catches any exception, prints the exception object and the stack trace, and re throws the exception so it can find its intended handler.
 

 try

{ . . .
}
catch (Throwable t)
{ t. printStackTrace ();
throw t;
}


Better Buttons

Traditionally, implementing a button is done in two steps:

  1. Add the button to the user interface in the constructor or init method
    add(new JButton("Click me"));
  2. Put the button action into the actionPerformed method
    if (arg.equals("Click me")) ( /* button action goes here */ )
    This separates the button creation from the button action.

Make a base class BetterButton as follows:
 

abstract class BetterButton exends JButton implements ActionListener
{ public BetterButton(String label)
{ super(label);
addActionListener(this);
}
abstract void action();
public void actionPerformed(ActionEvent evt) { action(); }
}

Now you can define the label and the button action at the same time:
 

add(new BetterButton("Click me") {void action(){ /* button action goes here +/ }});

Note that the action. procedure was not defined as public. That saves typing when it is redefined in the anonymous subclass. But it only works if BetterButton is in the same package as the class that creates the buttons.

Easier menus

Adding menu items and listeners by hand is incredibly tedious. Here is some code to build up a typical menu.
 

JMenu m = new JMenu ("Edit");
JMenuItem mi = new JMenuItem("Undo");
mi.addActionListener(this);
m.add(mi);
mi = new JMenuItem("Redo");
mi.addActionListener(this);
m.add(mi);
m.addSeparator();
mi = new JMenuItem("Cut");
mi.addActionListener(this);
m.add(mi);
mi = new JMenuItem("Copy");
mi.addActionListener(this);
m.add(mi);
mi = new JMenuItem("Paste");
mi.addActionListener(this);
m.add (mi );
menuBar. add (m);

Use a procedure makeMenu to take the drudgery out of making menus. This procedure takes three parameters. The first parameter is either a string or a menu. If it is a string, makeMenu makes a menu with that title. The second parameter is an array of items, each of which is a string, a menu item or null. The makeMenu procedure makes a menu item out of each string and a separator out of each null, then adds all items and separators to the menu. The third parameter is the listener for the menu items. (We assume that all menu items have the same listener.) Here is the call to makeMenu that is equivalent to the preceding menu construction code.
 

mbar.add( makeMenu( "Edit",
new Object[]
{ "Undo",
"Redo",
null,
"Cut",
"Copy",
"Paste",
}, this));

Here's the source code for the makeMenu procedure, which can easily be added to any program that requires a sophisticated menuing system.
 

private static JMenu makeMenu(Object parent, Object[] items, Object target)
{ JMenu m = null;
if (parent instanceof JMenu)
m = (JMenu)parent;
else if (parent instanceof String)
m = new JMenu((String)parent);
else
return null;
for (int i = 0; i < items.length; i++)
{ if (items[i] instanceof String)
{ JMenuItem mi = new JMenuItem((String)items[i]);
if (target instanceof ActionListener)
mi.addActionListener((ActionListener)target);
m.add(mi);
}
else if (items [i] instanceof JMenuZtem)
{ JMenultem mi = (JMenuItem)items[i];
if(target instanceof ActionListener)
mi.addActionListener((ActionListener)target);
m.add(mi);
}
else if (items[i] == null)
m.addSeparator();
}
return m;
}

In Windows programs, menus are generally defined in an external resource file and tied to the application with resource identifiers. It is possible to build menus programmatically, but it is not commonly done. In Java, there are no external UI resources and menus must be built programmatically.

Enumeration values in a List field

Suppose you want the user to choose a color from one of the predefined Java colors,
 

Color.red
Color.yellow

The traditional solution is tedious:

  1. Add strings to a list field
  2. When a string was selected, use an if/else/else branch to convert it to the enum Value.
    if (selection.equals("red")) color = Color.red;
    else if (selection.equals("yellow")) color = Color.yellow;
    else . . .
  3. Feel bad for not having set up a hash table instead.
    The problem is that color. red is the name of a constant, whereas "red" is a string object.

Use reflection to convert the strings!
Sample usage:
 

new EnumList(Color.class, new String[] {"red", "yellow", . . .})
The code:
class EnumList extends JList
{ public EnumList(Class cl, String[] labels)
{for (int i = 0; i < Labels.length; i++)
{ String label = labels[i];
int value = 0;
try
{ java.lang.reflect.Field f = cl.getField(label);
value = f.getInt(cl);
}
catch(Exception e)
{ Label = "(" + label + ")";
}
table.put (label, new Integer (value i ));
}
setListData(labels);
setSelectedIndex(0);
}
public int getValue()
{ return ((Integer)table.get(getSelectedValue())).intValue();
}
private Hashtable table = new Hashtable();
}

The drawback of this trick is that it does not work for internationalization. The enumerated constants are usually in English, but your users may want to see them in the local language, i.e. ot
gelb

Don't use = to compare strings

In Java, strings are immutable. That means, you can't change a value of a string like you can in C
 

string greeting = "Hello"; // C++
greeting[4] = '!'; // can't do this in Java

Of course, in Java you can replace the string to which a string variable refers.
 

String greeting = "He llo"; // Java
greeting = greeting.substr(0, 4) +

The string "Hello" is unchanged, but greeting now refers to a different string.
That means, strings act just like numbers.
 

int x = 5;
x++; // 5 is unchanged, but x is now 6

Strings have the look and feel of numbers in every way but one. You cannot use == to compare them.
 

if {cmd == "Help!")... // no!

This code tests whether cmd and "Help! " are the identical object, not whether they have the same
contents. You must use equals.
 

if (cmd.equals ("Help! ") )... // OK

It is unfortunate that the == code compiles. This is one of the few instances in Java where nonsense code
compiles without a warning. (Of course, in C++, there are lots of traps where nonsense code compiles.)

Don't return references to mutable objects

Especially for simple classes, it is common to supply accessory functions for all data fields.
 

class Employee
{ public Employee(String n, double s, Day
{ name = n;
salary = s;
hireDay = d"'
}
public String getName()
{ return name;
}
public double getSalary()
{ return salary;
}
public Day getDay() // WRONG!
{ return hireDay;
}
private String name;
private double salary;
private Day hireDay;
}

Never return a reference to a mutable object. Consider the following.
 

Employee harry = new Employee("Harry Hacker", 35000, new Day(1996, 10, 1,l));
Day d = harry.getDay();
d.setYear(1997); // oops--now changed Harry's hire day
Remedy: Return a clone of the field:
public Day getDay() // OK
{ return hireDay.clone ();
}

 

EZClone with serialization

Cloning in Java is tedious to implement. You need to
  1. have the class implement cloneable
  2. implement the clone method

public void clone
{ try
{ X ret = super.clone();
// now clone all object data fields and" copy all number Data fields
ret.objdata1 = objdata1.clone
ret.numdata1 = numdata1;
return ret;
} catch(CloneNotSupportedException)
{ return null;
}
}

Use serialization for cloning. As long as all data fields are serializable. the entire cloning process can be automated.
 

class EZClone implements Cloneable, Serializable
{ public Object clone()
try
{ ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeQbject(this);
out.close();
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
ObjectInputStream in = new ObjectInputStream(bin);
Object ret = in.readObject();
in.close();
return ret;
} catch(Exception e)
{ return null;
}
}
class X extends EZClone
{ // no clone necessary
}

 

A debug window for applets

When you debug an applet inside a browser (especially Internet Explorer), you can't always issue print statements - they don't show up, or they are only logged to a file.

Display the debug printout in a separate window. Here is the code for the window class.
 

import java.awt.*;
import java.awt.event.";
import javax.swing.
class DebugWin extends JFrame
{ public void print(Object ob)
output.append("\n" + ob);
public DebugWin()
{
setTitle("DebugWin");
output.setEditable(false);
output.setText("[DebugWin]");
getContentPane().add(new JScrollPane(output), "Center");
setSize(300, 200);
setLocation(200, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ setVisible(false); }
} );
show();
}
private JTextArea output = new JTextArea ();
}

Here is how you use it.
 

class MyFrame extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
dw.print("Event = " + evt);
}
private DebugWin dw = new DebugWin();
}

 

Use main for unit tests in every class

Of course, you start a Java program by loading a class and invoking the main procedure. If another class has a main procedure, it is ignored (unless you explicitly call it).

Add a main procedure to every class so that you can run a simple unit test:
 

class MyClass
{ // methods
// fields
public static void main(String[] args) // for testing only
{
MyClass myObject = new MyClass();
System.out.println(myObject.myMethod());
}
}

Class Objects

Objects of type Class are useful to analyze the capabilities of classes, and to generate new objects. In Java 1,0, you could use the forName method to generate an object of a particular class:
 

Class cl = Class.forName("java.lang.Double");

In Java 1.1, you can simply add .class to the name of any type to get the representative class.
 

Class cll = Double.class; // it works for classes
Class c12 = double.class; // it works for numeric types
Class c13 = double[].class; // it works for arrays
Class c14 = Cloneable.class; // it works for interfaces
Sample code:
Number n = numberFormat.parse ();
if (n.getClass( ) == Double.class ) . . .

The Class class should have been called Type since it can represent non-classes as well (i.e. numbers and interfaces). Note that arrays are classes in Java.

Class names of arrays

Class objects are useful for programs that inquire dynamically about the capabilities of classes. The construct Type. class returns an object of type class, as does the static forName method. The getName method returns the name of the class.

These methods are inconsistently implemented for basic types and arrays. Consider the following:
 

Class cll = Double.class;
Class c12 = double.class;
Class c13 = double[].class;
Class c14 = Double[].class;
System.out.println(cll.getName()); // prints java.lang.Double
System.out.println(c12.getName()); // prints double
System.out.println(c13.getName()); // prints [D (!)
System.out.println(c14.getName()); // prints[Ljava.lang.Double;
cll = Class.forName("java.lang.Double"); // ok
c12 = Class.forName("double"); // throws exception
c13 = Class.forName("double[]"); // throws exception
c13 = Class.forName("[D"); // ok

Method pointers

One of the less safe features of C is function pointers - it is usually not possible to write generic and typesafe code with function pointers. (In C++, templates can be used to for typesafe generic code.) For example, consider qsort in the standard C library, or a callback in Windows or X Windows. These callbacks use unchecked void* parameters. For that reason, Java did not have function pointers. But they Of course, normally you would use a dynamically bound method with a known name and signature, and then use inheritance to call different functions with similar properties, But sometimes, that is too constraining.

Consider this example - we want to print tables of mathematical functions.
 

{ double dx = (to - from) / 20;
for (double x = from; x <= to; x += dx)
{
double y = f(x); // wrong syntax
Format.print(System.out, "%12.4f |", x);
Fozmat.print(System.out, "%12.4f\n", y);
}
static void printTable(double from, double to, Method f)
}
printTable(0, 10, Math.sin); // wrong syntax

In principle, this is now possible in Java, but the syntax is more complex. Here is how you can get a method pointer:
 

printTable(0, 10, java.lang.Math.class.getMethod("sqrt", new Class[] { double.class }));

And here is how you make the call:
 

static void printTable(double from, double to, Method f)
{ . . .
// compute y = f (x);
Object [] args = ( new Double (x) );
Double d = (Double) f. invoke (null, args);
double y = d.doubleValue ();
}

However, keep in mind that invoking a method is very slow. Only use this as a last resort, if there is no better design involving an inter face and a dynamically bound method.

 

Using Passwords to protect your Midlets

The simplest way for a MIDlet to safeguard sensitive information, is to prompt the user for a password. You can program this into the MIDlet so that the MIDlet does this when it starts or the first time it accesses sensitive data. You can also have the MIDlet ask the user to confirm certain sensitive operations by reentering the password.

Prompting the user for a password is easily done using either the TextBox or the TextField class, both part of the javax.microedition.lcdui package. The TextBox class displays a top-level window consisting of a title, an optional label, and a text entry field. No other user interface components can be added to a TextBox. So for more flexibility, you can use a TextField component. A TextField is a text entry field that can be placed on a Form, that is, a top-level window that can display multiple user interface components. Both components have an identical interface, so what's being discussed here applies to either component.

Here's the code for a simple password prompter using the TextBox class:
 

// Defines a class that prompts the user
// for a numeric password.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PasswordPrompter
extends TextBox
implements CommandListener {
private Display display;
private Displayable next;
private int pin;
// Constructor with a default title
public PasswordPrompter( int pin,
int maxchars,
Display display,
Displayable next ){
this( defaultTitle, pin, maxchars,
display, next );
}
// Constructor with a specific title
public PasswordPrompter( String title,
int pin,
int maxchars,
Display display,
Displayable next ){
super( title, "", maxchars,
TextField.NUMERIC | TextField.PASSWORD );
addCommand( okCommand );
setCommandListener( this );
this.display = display;
this.next = next;
this.pin = pin;
display.setCurrent( this );
}
// Responds to the one-and-only command event
// by checking the entered password against
// the pin. If it's OK, the user is "forwarded"
// to the next displayable, otherwise an alert
// is displayed.
public void commandAction( Command c,
Displayable d ){
String pinStr = getString();
try {
if( Integer.parseInt( pinStr ) == pin ){
display.setCurrent( next );
return;
}
}
catch( NumberFormatException e ){
}
Alert alert = new Alert( "Error!",
"Invalid password",
null,
AlertType.ERROR );
setString( "" );
display.setCurrent( alert, this );
}
private static final Command okCommand =
new Command( "OK", Command.OK, 1 );
private static final String defaultTitle =
"Enter Password";
}

To invoke the prompter, just create an instance of the PasswordPrompter class, passing it the password to check against (as an integer), the maximum number of characters to allow for the password, the Display instance for the MIDlet, and the screen to display if the correct password is entered. Here is a simple MIDlet that demonstrates the use of the prompter:
 

// A MIDlet that demonstrates the use of the
// password prompter.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class PasswordTester extends MIDlet
implements CommandListener {
private Display display;
private Command exitCommand
= new Command( "Exit ", Command.EXIT, 1 );
public PasswordTester(){
}
protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}
protected void pauseApp(){
}
protected void startApp()
throws MIDletStateChangeException {
if( display == null ){ // first time called...
initMIDlet();
}
}
private void initMIDlet(){
display = Display.getDisplay( this );
// First time through, ask for the password.
new PasswordPrompter( 1234, 4, display,
new TrivialForm() );
}
public void exitMIDlet(){
notifyDestroyed();
}
public void commandAction(
Command c, Displayable d ){
exitMIDlet();
}
// A trivial UI screen
class TrivialForm extends Form {
TrivialForm(){
super( "MainApp" );
addCommand( exitCommand );
setCommandListener( PasswordTester.this );
}
}
}

In the example above, the password is hard coded to be "1234." Of course, you wouldn't do this in a real application. What you would do is get the password either from a record store maintained by the application or else from an application property. An application property is a value stored in either the MIDlet suite manifest or the suite's application descriptor. To retrieve an application property just call the MIDlet's getAppProperty method. For example:
 

MIDlet midlet = ....;
int password;
try {
String p = midlet.getAppProperty( "Password" );
password = Integer.parseInt( p );
}
catch( NumberFormatException e ){
}

Using an application property allows you to customize a MIDlet suite for a particular customer. For example, as part of a servlet-controlled download process, the customer could be asked to enter an email address. The process could then generate a random password and a custom version of the MIDlet suite, and send the password to the customer by email. The customer would have to enter the password to activate the application, once it's on the device.


Did You Know?

  • Java starts with C++.
  • Aside from C, Java is the closest thing you can get to C++.



AlarmPlanet: Web's First Home Security Portal

3,500 HD Channels

Email Marketing on Sterioids


Links