Kamis, 08 Desember 2011

                                                      Programming with Java
Java as one of the promising new programming language a lot easier for junior and senior programmers. This tutorial will take you see more of this language through a discussion of the concept model of the design and use simple instructions.Is Java?
Java is object-oriented programming language developed by Sun Microsystems since 1991. This language was developed with a similar model in C + + and Smalltalk, but is designed for easy use and platform independent, which can be run on various types of operating systems and computer architecture. The language is also designed for programming on the Internet that is designed to be secure and portable.Platform Independent
Platform independent means that programs written in Java can be easily moved between different types of operating systems and various types of computer architectures. This aspect is very important to be able to achieve the purpose of Java as an Internet programming language in which a program will be run by different types of computers with different types of operating systems. This property applies to the level of source code and binary code from Java programs. Unlike the C language and C + +, all data types in the Java language has a size that is consistent across all types of platforms. Java program source code itself does not need to be changed at all if you want to recompile on other platforms. The results of the compile Java source code is not machine code or processor instructions that are specific to a particular machine, but rather a form of bytecode files ending in. Class. Bytecode execution can direct you on each platform using the Java Virtual Machine (JVM) as against the bytecode interpreter.
JVM itself is an application that runs on top of an operating system and translates the Java bytecode programs and executing them, so that the concept can be regarded as an interpreter. The process of executing a Java program can be depicted as in Figure 1. In this way, a Java program that has been compiled will be able to run on any platform, provided there is a JVM there.
Compilers and interpreters for Java programs shaped the Java Development Kit (JDK) manufactured by Sun Microsystems. JDK can be downloaded for free from java.sun.com site. Interpreter for Java programs themselves are often also called the Java Runtime or Java Virtual Machine. Java interpreter, without the compiler, called the Java Runtime Environment (JRE) can also be downloaded on the same site. To develop the required JDK Java programs, whereas if you just want to run Java bytecode enough with JRE alone. However, to execute an applet (a Java bytecode as well) you usually do not need to download JRE for Java-enabled browser already has its own JVM.Library
In addition to the compiler and interpreter, the Java language itself has a fairly large library that can facilitate you in making an application quickly. This library has been included for the graphics, user interface design, cryptography, network, sound, databases, and others.OO
Java is an object-oriented programming language. Object-oriented programming is a technique to explicitly organize the program and can be done with almost any programming language. However, Java itself has implemented a range of facilities that a programmer can optimize object-oriented programming techniques.
Little additional comparisons in C and C + +, Java inherited many object-oriented concepts of C + +, but by eliminating those aspects of complexity in C + + without reducing its strength. It is easier for novice programmers to learn Java programmers but reduces flexibility in tinkering with a program. Behind the convenience offered by Java, the Java library facility size alone makes a programmer takes a short not to be able to master the use of these libraries.Starting the Java Programming
To create a Java program, as mentioned earlier, you need JDK. JDK installation process is very easy and requires no particular knowledge. But to use it you need to do some adjustments with your operating system. Generally you need to do is 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 in your autoexec.bat file. For Windows NT/2000/XP you can simply add the directory C: \ JDK1.4 \ bin to the path variable in the System Environment. Here's how: Right-click the My Computer icon, select Properties. Then select the Advanced tab. Then click the Environment Variables button, locate the path variable, then add the path to your JDK directory into a variable. For Linux, add the command line SET CLASSPATH = (your jdk directory) into your profile file. To try JDK, type java and javac commands to the shell prompt (or DOS Command Prompt). If the command has been identified then the java or javac program will display the syntax usage. For ease and variety of additional facilities you can use the Integrated Development Environment (IDE) for Java language such as Visual Café from Symantec or from Borland JBuilder.
Sequence of steps that you must do to create a simple Java program are:

Creating a program's source code with any text editor. Remember, the file must have a. Java and case sensitive.
Compile the source code with the javac command. For example: javac HelloWorld.java. If successful, the result is a bytecode file ends in. Class.
Execute the command java bytecode. The parameters of this command is the file name without extension compilation. 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 news World?");
And this is another example, which is a simple applet for displaying text in the applet. Call this file is named HelloWorldApplet.java:
import java.awt.Graphics;
public class extends java.applet.Applet HelloWorldApplet{
public void paint (Graphics g)
g.drawString ("What news World?", 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 after we discuss how to compile and execute the program.
Keep in mind that the Java language is case sensitive, so you should pay attention to the use of uppercase and lowercase letters. Besides writing the source code programs do not have to pay attention to a particular form, 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 a line of its own. However it is recommended you follow the layout as in the example so that your program easy to read and understand.Compilation
Once both files saved with the name HelloWorld.java and HelloWorldApplet.java, we will compile the program with the command:
prompt> javac HelloWorld.javaprompt> javac HelloWorldApplet.java
Please note that your current active directory is the directory where you put the files on the program. You can still compile your program from a different directory with the command:
prompt> javac (program directory) / namafile.java
After this command is completed, you will see that it has created two files. Class, ie bytecode compilation of source code.Syntax Program
Now we will try to discuss the elements in both source code.
At the beginning of Listing 2 we find the import command. At this early stage you need to know that the statement is just easier writing of methods or functions in other programming languages ​​is called a procedure or function. So you just need to write as a substitute java.awt.Graphics graphics, because we have to import java.awt.Graphics.
Then in each listing there is a public class statement. This statement is the opening statement of a classroom. Class itself is used to create the object. Remember that Java is object oriented. Word public in front of it to function so that the class can be accessed by all other programs. For now suppose the object as an item that can be manipulated by a program. In Listing 2 there extends an additional word. This means the class that we created will inherit the properties of our class extends. In other words we make our class that extends a subset of the class we created.
Then we find the statement line public static void main (String [] args) and public void paint (Graphics g). Both are the opening statements of a method. The method itself is a collection of statements to perform a certain task in the classroom. Both of them actually have the same function but in different contexts. In each application there should be a method named main that will be executed first when the program is executed. While the applet, the first method will be executed when the applet is loaded paint. Word public in front of him has the same function with the word public in front of the line beginning class. But later you will find also other forms such as private and protected as we will discuss later.
In Listing 1 have the word static main method in an opening statement. This means the main method does not modify or use the objects created by that class, so it can be said to stand alone and are not tied to the object. In the main method in the application, the parameters are always String [] args, where args is just a name of an array of String objects. This array will contain the parameters of the given user as command line argument. While you do not need to understand about these parameters, just keep in mind that the main method of the form should always be the case.
Then in the second method 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 and separated by semicolons to be executed one by one. The second statement turns out to call a listing on another method is the method println and paint. Surely you can see that to call a method required three components, namely:

The object that we want to use. In this case the object System.out and Graphics g.
The name of the method we want to use. In this case println and paint.
A pair of parentheses that contains additional information required by the invoked method, namely the parameters.
In Listing 1, the statement System.out.println ("What news World?"); Means seek out objects in the System class and then call the println method of the object out with a string parameter "What the World News". Being in Listing 2, the statement g.drawString ("What news World?", 5, 25); means find the object g then call DrawString method on the object with the parameter g 'What news World? ", 5, 25).Execution
When finished discussing the basic syntax of Java in the second listing, then we will try to execute the second program. For the first program in the form of a regular application, we just type java HelloWorld command at the prompt and World News What's messages? will appear on the screen (or maybe somewhere else, your operating system dependent). As for the applet, we must create an HTML file as a wrapper, or the caller. Here's an example of an HTML file to wrap the applet that we created.
Try <TITLE> Applet </ TITLE>
</ HEAD>
<APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25>
</ Applet>
</ BODY></ HTML>
Give the name helloword.html and keep it in the same directory with the file locations. Java and. Class before. To execute the applet we simply open the HTML file in a Java-enabled browser or appletviewer namafile.html typing commands at the prompt.

link :http://www.master.web.id/mwmag/issue/04/content/tutorial-java-1/tutorial-java-1.html

Tidak ada komentar:

Posting Komentar