Starting the Java Programming

To create a Java program, as mentioned earlier, you need a JDK. JDK installation process is very easy and does not require specific knowledge. But to use it you need to make some adjustments to your operating system. Generally you need to do is to enter the path to your JDK directory path to the settings in your operating system. Suppose your JDK directory is C: \ jdk1.4 on Windows 98 then you can simply add the command line SET PATH = C: \ jdk1.4 \ bin to your autoexec.bat file. For Windows NT/2000/XP you can simply add the directory C: \ jdk1.4 \ bin in the path variable in the System Environment. Here's how: Right-click My Computer icon, select Properties. Then select the Advanced tab. Then click Environment Variables button, find the path variable, then add the path to your JDK directory in the variable. For Linux, add the command SET CLASSPATH = (your jdk directory) into your profile file. To try JDK, type java and javac commands at the shell prompt (or DOS Command Prompt). If the command is recognized the java or javac program will display the usage syntax. For convenience and additional facilities you can use the Integrated Development Environment (IDE) for Java language such as Visual Café from Symantec or JBuilder from Borland.




The order of the steps you need to do to create a simple Java program are:

Making the program source code with any text editor. Remember, the file must berekstensi. Java and case sensitive.
Compile the source code with the javac command. For example: javac HelloWorld.java. If successful, the result is a bytecode files ending in. Class.
Execute the command java bytecode. Parameters of this command is a compilation of the file name without the extension. Class. Example: java HelloWorld.
Source Code

Here's the code for HelloWorld.java:

public class HelloWorld
(
public static void main (String [] args)
(
System.out.println ( "What the World News?");
)
)
And this is another example, which is a simple applet to display text in the applet. Call this file named HelloWorldApplet.java:

import java.awt.Graphics;

public class HelloWorldApplet extends java.applet.Applet
(
public void paint (Graphics g)
(
g.drawString ( "What the World News?", 5, 25);
)
)
Can clearly be noted that the structure of both programs are very similar, and differ only in the context of execution. Both programs will be discussed further when we discuss how to compile and execute the program.

Please note that the Java language is case sensitive, so you should consider the use of capital and lowercase letters. Besides writing the source code is not the program should take into account specific forms, so you can just write all lines of source code in one line as long as you do not forget to put a semicolon (;), or write each word in one line. However, it is recommended you follow the layout as in the example for your program easy to read and understand.


Compilation

After the second disave file with the name HelloWorld.java and HelloWorldApplet.java, we will compile the two programs with the command:

prompt> javac HelloWorld.java
prompt> javac HelloWorldApplet.java
It should be noted that the current directory your current directory where you placed the files the program. You still can compile your program from a different directory with the command:

prompt> javac (program directory) / namafile.java
After this command is complete, you will see that has created two files. Class, the bytecode compilation of our source code.

Program Syntax

Now we will try to discuss the elements in both the source code.

At the beginning of Listing 2 we find the import command. At this early stage you need to know that such statements serve only facilitate the writing method or in another programming language called procedure or function. So you only need to write Graphics java.awt.Graphics instead, because we have to import java.awt.Graphics.

Then in each listing is a public class statement. This statement is the opening statement of a class. Class itself is used to create the object. Remember that the object-oriented Java. The word public in front of the class functions to be accessed by all other programs. For now consider the object as an item that can be manipulated by a program. In addition there are 2 Listings extends said. This means that we make the class will inherit the properties of our class extends. In other words we make our class extends a subset of the class we created.

Then we find a line statement public static void main (String [] args) and public void paint (Graphics g). Both the opening statement of a method. The method itself is a collection of statements to perform a specific task in the classroom. Both actually have similar functions but in different contexts. In each application must have a method called main which will be executed first when the program is executed. While the applet, the first method will be executed when the applet is loaded paint. The word public in front of him has the same function with the existing public word on the front lines of the beginning of class. But eventually you will find other forms as well as protect the private and we will discuss later.

In Listing 1 is the word static in the main method of opening statements. This means that the main method does not modify or use the objects created by the class, so it can be said to stand alone and not tied to the object. In the main method of application, the parameters are always String [] args, where args is a name of an array of String objects. This array will contain the parameters given user as command line argument. While you do not need to understand about these parameters, simply note that the method of play should always be so.

Later on in the two methods in the second listing, we find a statement. You certainly could have put more than one statement in a method. Every statement in a method separated by a semicolon and will be executed one by one. The second statement calls on the listing it is a different method println methods and paint. Surely you can see that to call a method that takes three components:

Object that we want to use. In this case the object System.out and Graphics g.
Name the method that we want to use. In this case println and paint.
A pair of brackets containing the additional information required by the method called, the parameters.
In Listing 1, the statement System.out.println ( "What the World News?"); Means seek out objects in the class system and then call println method of the object out with a string parameter "What the World News?". 're In Listing 2, the statement g.drawString ( "What the World News?", 5, 25); means look for the object g drawString then call methods on the object with the parameters g "What the World News?", 5, 25);.

Execution

When finished discussing the basic syntax of Java in the second listing, then we will try to execute the program. For the first program in the form of regular applications, we live to type java HelloWorld command at the prompt and World news What message? will appear on the screen (or perhaps elsewhere, depending on your operating system). As for applets we must create an HTML file as a wrapper, or the caller. Below are given examples of the HTML file to wrap the applet that we make.




Give name helloword.html and store it in the same directory with the location of the files. Java and. Class before. To execute the applet we just open the HTML file in a browser that is Java-enabled or namafile.html typing commands at the prompt appletviewer.



Closing

For now you have a picture of how the process of making a simple Java program in the form of regular applications or applets. You can also try to develop a simple program is in accordance with your wishes based on the things you already know. The basic concept is you get will be developed further in the tutorial topics further. Good luck!
0 Komentar untuk "Starting the Java Programming"

Back To Top