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.
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)
- The easiest way to get start is to download the JDK from SUN microsystem (the Java company).
- Then, setup the JDK in your machine by double click the file you downloaded.
- 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.- Restart your computer.
- Now, type up your Java programs in Notepad or other editor of your choice. I recommend EditPlus which an very good editor.
- And, make a new folder and save your java program in the folder of your choice. (eg. C:\YourJavaFolder\ )
- Start a "Command Prompt" and change to the directory of your java program.
- Invoke C:\YourJavaFolder> javac YourFirstJavaFile.java
--- javac stands for "java compiler"- Debugs and repeat 8. until the program have no error
- 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.
<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:
- Bold = required.
Return to my Homepage.
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:
- Be careful that Java is case sensitive. Don't use wrong case.
- Remember to import java.applet.*
- Any applet must extends Applet.
- The methods, init() must be void
- The method, init() will be call when your applet just start and will call only one.
- The methods, paint() must be void and must have a signature of Graphics g.
- The method, paint() will be called automatically everytime when your applet needs repaint.
(i.e. when it starts, when it has been revealed from under another window, or when your program calls repaint() method). So, most of the time, it should not be empty. And, because it will be call everytime when it needs paint, it shouldn't be too lengthy.
You don't need a void main() in Java applet. void main() is only needed for Java Application. For applet, put the expressions that you think which should appear in void main() into void paint() and void init().Return to my Homepage.
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.
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:
- Declare the array:
String yourFirstArray[];
- Allocate space for the array:
yourFirstArray = new String[500];
- Allocate space for the array's elements:
for (int i = 0; i < 500; i++){
YourFirstArray[i] = new String();
}- 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.
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:
- You need to import java.awt.* in order to use frame, label and event
- You may note that I import both java.awt.event.* and import java.awt.*. Well, the "*" actually represent all class or interface in the package, but NOT the sub-package in it. java.awt.event is actually a package in java.awt, so both of them must be imported.
- Class of your application must be declare as public and save in a file in which the filename is the same as the Class's name.
- main() method must be static void with the signature of String args[], otherwise the compiler will complain you about that.
- Inner class is used to declare WindowsAdapter. Inner class is a new feature to Java JDK 1.1
- Close window event is defined using the new Delegation-Base event model(which is new to JDK 1.1 as well). On the other, the Inheritance-Base Event handling is no longer recommended and will be phased out in next Java JDK releases.
- The above example will not work in any Java JDK 1.0.x compiler.
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