Write Your First Java Program

Example codes for quick lookup


Please also visit my homepage for more pogramming resources


This is a short list of frequently used java code/format for novice or intermediate Java programmers. If you are looking for full tutorial or other resource please see my Java Resources Page.

  1. Your first Java everything
  2. Your first Java html tag
  3. Your first Java Applet
  4. Your first Java application
  5. Your first parameter in Java applet
  6. Your first Java array

 

You first Java everything:
(I assume you're new to Java and you don't have any Java compiler/IDE in home, and you use Win95/98 or NT at home)

  1. The easiest way to get start is to download the JDK from SUN microsystem (the Java company).
  2. Then, setup the JDK in your machine by double click the file you downloaded.
  3. After you installed the program, append the following line in at the end your autoexec.bat file in your boot partition.
    PATH=%PATH%;C:\jdk1.1.7\bin; --- if 1.17 is the version jdk you downloaded.
  4. Restart your computer.
  5. Now, type up your Java programs in Notepad or other editor of your choice. I recommend EditPlus which an very good editor.
  6. And, make a new folder and save your java program in the folder of your choice. (eg. C:\YourJavaFolder\ )
  7. Start a "Command Prompt" and change to the directory of your java program.
  8. Invoke C:\YourJavaFolder> javac YourFirstJavaFile.java
    --- javac stands for "java compiler"
  9. Debugs and repeat 8. until the program have no error
  10. For application --> Run the java program C:\YourJavaFolder> java YourFirstJavaFile
    --- no file extension, and don't make up java and javac, they are different.
    For applet --> type up Java tag and save in an html file, and double click the html file.

Return to my Homepage.


Your first Java tag in HTML:

<applet codebase="YourAppletFolder/" code="YourApplet.class" width=400 height=75>
    <param name="text" value="Welcome to HotJava!">
    <hr>
        If you were using a Java-enabled browser such as HotJava,
        you would see dancing text instead of this paragraph.
    <hr>
</applet>

Note:

Return to my Homepage.


Your first Java applet:

Here is a simple example of Java Applet:

import java.applet.*
public class YourApplet extends Applet{
    String s;

    public void paint(Graphics g){
        g.drawString( s , 20, 20);
    }

    public void init(){
        s = "Hello World";
    }
}

Notes:  

Your first parameter in Java applet:

Get parameters from browser is easy. For example, you have a parameter, <param name="text" value="Welcome to HotJava!">, in you html page. Here the code you need:

String s;
s = getParameter("text");

Note:  

  • getParameter() always return string. So, for numerical value, you have to convert yourself.
  • getParameter("text") will reture a String "Welcome to HotJava!" in the above example.

Return to my Homepage.


Your first Java array:

You need 3 or 4 steps before you use any array in Java. If you access an array elements before you done all the steps, NullPointer exception may throw by Java:

  1. Declare the array:

    String yourFirstArray[];

  2. Allocate space for the array:

    yourFirstArray = new String[500];  

  3. Allocate space for the array's elements:

    for (int i = 0; i < 500; i++){
            YourFirstArray[i] = new String();
    }

  4. Use the array(for example):

    if (YourFirstArray[10] == null){
            YourFirstArray[10] = "Hello world again!";
    }

However, if your array is primitive type (e.g. int, long, byte, char, float, double, etc.), you don't need step 3.

Return to my Homepage.


Your first Java application:

Notes that many Java book only have examples of writing Java application using Java JDK 1.0 method (even they say their book cover JDK 1.1). But, the following example use the new Java JDK 1.1 method. So, the event handling may look strange for you compare with the one you previously saw elsewhere.

import java.awt.*;
import java.awt.event.*;

public class YourFirstApp extends Frame{

// Constructor
    YourFirstApp(String frameTitle){
        super(frameTitle);
        add(new Label("YourFirstApp",
            Label.CENTER),"Center");
    }

    public static void main( String args ){
        YourFirstApp app = new YourFirstApp();
        app.setSize(300,100);
        app.show();
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent
                event){
                dispose();
                System.exit(0);
            }
        });
    }
}

You may found that an Java application is a lot more complicated that an Java applet.
Notes:  

Return to my Homepage.


Your first Java Component:

Material will be put in here later

Return to my Homepage.


Your first light weigh component:

Material will be put in here later

Return to my Homepage.


Your first JavaBean:

Material will be put in here later

Return to my Homepage.


        More example codes will be put in here later

Please visit my homepage for more pogramming resources

You're the th visitor, since Aug 11, 98
LE FastCounter