The document discusses Java programming language and its environment. It describes how Java programs are compiled to bytecode format instead of machine code, allowing them to run on any machine with a Java Virtual Machine. It also outlines some key characteristics and advantages of Java, as well as how to write different types of Java programs.
The document discusses Java programming language and its environment. It describes how Java programs are compiled to bytecode format instead of machine code, allowing them to run on any machine with a Java Virtual Machine. It also outlines some key characteristics and advantages of Java, as well as how to write different types of Java programs.
The document discusses Java programming language and its environment. It describes how Java programs are compiled to bytecode format instead of machine code, allowing them to run on any machine with a Java Virtual Machine. It also outlines some key characteristics and advantages of Java, as well as how to write different types of Java programs.
The document discusses Java programming language and its environment. It describes how Java programs are compiled to bytecode format instead of machine code, allowing them to run on any machine with a Java Virtual Machine. It also outlines some key characteristics and advantages of Java, as well as how to write different types of Java programs.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online from Scribd
Download as pdf or txt
You are on page 1of 15
X INTRODUCTION
Starting from a failed project, Java has emerged as a popular programming
language within a short period of time. JavaSoft Sun MicroSystems had successfully signed up 38 licences in only one year after Java technology was announced. Besides that, it also successfully attracted 6000 programmers to attend the first seminar JavaOne Developer Conference TM -in 1996.
What is so special about Java that computer industries and businesses are able to accept it within a very short period of time compared to other new technologies in computer history? The answer to this question is provided in subtopic 1.2: Characteristics of Java. We will now look at an overview of Java.
JAVA ENVIRONMENT In the traditional language environment such as C, C++ and Pascal, source code compilation will generate the object code in binary file format, as shown in Figure 1.1. This binary file is specific for the machine that generates it; in other words, it cannot be executed directly on other machines. For example, the binary file generated by the SPARC machine cannot be executed on a Pentium machine.
1.1 T T o o p p i i c c
1 1
X Introduction to Java LEARNING OUTCOMES By the end of this topic, you should be able to: 1. Describe the Java environment; 2. Outline the strengths of Java; 3. Explain the types of Java programs; and 4. Compile and execute a Java program. X TOPIC 1 MANUFACTURING SYSTEMS
2 To execute the program on another machine, the program source code must be recompiled on that machine. The traditional programming environment depends heavily on this computer architecture.
Figure 1.1: Traditional language environment
Figure 1.2 illustrates the Java programming environment. The output of a Java program compilation is in the form of bytecode. Java bytecode is a set of instructions that is quite similar to the machine code instructions or native code for a machine. The difference is that the bytecode is not specific to any processor. The result of a compilation can be implemented by a Java Virtual Machine (JVM), any machine that will interpret the bytecode.
Figure 1.2: Java language environment
If we want to implement a Java program on a Pentium machine, then we need an interpreter or JVM for the Pentium machine. The same applies for the SPARC machine. However, unlike traditional languages such as C and Pascal, a Java program does not have to be recompiled to enable it to run on another machine. Java Program Java CompiIer Java Java Interpreter (Pentium) Java Interpreter (Power PC) Java Interpreter (SPARC)
What are the Advantages of Using Bytecode? There are advantages in using bytecode as an intermediate and not allowing people to compile it into the machine language of whatever computer they want to run. A compiler has to understand Java in order to compile it. A compiler is a complex program, while a Java bytecode interpreter is a simple program. This makes it easy to write a bytecode interpreter for a new type of computer; once that is done, that computer can run any compiled Java program. It would be much more difficult to write a Java compiler for the same computer.
CHARACTERISTICS OF JAVA Java technology provides a neat development platform based on objects. Java environment provides class library, which contains various classes that have already been tested. This class library can be accessed by the programmer who wants to use it in his/her program development; furthermore, this library can be extended by the programmer according to his/her requirements. Other main characteristics of Java are highlighted below:
(a) Simple Java has the functionalities needed to implement its rich feature set. It does not add lots of syntactic structure or unnecessary features.
(b) Object-Oriented This is the core concept in Java. Almost everything in Java is either a class, an interface, a method or an object.
(c) Platform Independent Java programs are compiled to a bytecode format that can be read and run by interpreters on many platforms including Windows 95, Windows NT, and Solaris 2.3 and later versions.
(d) Safe Java code can be executed in an environment that prohibits it from introducing viruses, deleting or modifying files, or otherwise performing data destroying and computer crashing operations.
(e) High Performance Java can be compiled on the fly with a Just-In-Time compiler (JIT) that rivals C++ in speed.
1.2 X TOPIC 1 MANUFACTURING SYSTEMS
4 (f) Multi-Threaded Java is inherently multi-threaded. A single Java program can have many different things processing independently and continuously.
TOOLS TO DEVELOP JAVA PROGRAMS When Java was introduced in 1995, Java Development Kit (JDK) was the only development tool available. Although what is provided in JDK is sufficient to develop any Java program, the facilities provided for programmers are outdated compared to the latest integrated development environment for languages such as Visual Basic and C++.
The Integrated Development Environment (IDE) refers to the software package that combines various development tools. This includes program editor, compiler, debugger and other utilities. Most IDE have features such as windows, drag-and-drop, and other graphic elements. The aim is to make the software development process fast, efficient, and easy to debug.
Part of IDE uses the Rapid Application Development (RAD) approach. RAD accelerates the software development process by using tools such as interface designer. Many Java IDE available in the market use the graphical interface to support RAD.
The following are some Java IDE software with their URLs: x WinEdt (http://www.winedt.com/) x JCreator (http://www.jcreator.com) x Java WebIDE (http://www.chami.com/webide) x Symantec Caf/Visual Caf (http://www.symantec.com/) x Forte for Java (http://www.sun.com/forte/ffj/) x Visual Age (http://www-4.ibm.com/software/ad/vajava/) x NetBeans (www.netbeans.org)
JDK can be downloaded free from the website: http://www.oracle.com
1.3 TOPIC 1 MANUFACTURING SYSTEMS W
5
WRITING JAVA PROGRAMS There are two types of program in Java Application and Applet. Application itself could be divided into two types, namely: Text-based application and Frame. Text-based application does not support Graphical User Interface (GUI) while Frame actually is a GUI-based stand-alone program.
Text-based application can be written using a structured approach or object- oriented paradigm. But Frame can only be written using an object-oriented approach.
Apart from application, another type of Java program is Applet. Applet is a web- enabled program that will be introduced in Topic 9 because it is easier to understand applets after we have discussed features such as inheritance and methods. The examples of text-based application and Java applet will be illustrated in the next sections. Frame is not covered in the syllabus. Thus, we will skip any elaboration about Frame in this module.
Figure 1.3: Types of Java program 1.4.1 Writing Text-based Application Using Structured Approach Text-based application is a stand-alone program that can be written using a structured approach or object-oriented approach. In this section, we will discuss how you can write text-based applications using the structured approach.
1.4 Java Program AppIication AppIet Text-based appIication Frame Applet is a web-based program which supports GU Frame is non-Web stand alone GU program X TOPIC 1 MANUFACTURING SYSTEMS
6 Program 1.1 shows an example of text-based application written using a structured approach.
Program 1.1: Hello.java
Line Number 1 // this program displays Hello world 2 class Hello{ 3 public static void main (String args[]) { 4 System.out.println(Hello world!); 5 } 6 }
In structured approach, all the statements and instructions are dumped into the main method. Sometimes, extra methods are added to perform specific tasks. No object(s) is/are created when writing Java programs using the structured approach. Program 1.1 is described below:
Line 1 Line 1 in the program is a comment written by the programmer. It helps others to understand the nature of the program. Thus, the program becomes more readable.
Line 2 Line 2 declares the name of the class as Hello.
Line 3 Line 3 is where the program starts to execute. In this class, a method called main() contains the statements to display the Hello world! string. The main() method must be present in all Java applications because it is the first method that will be executed when an application runs (i.e. it is the where the program starts to execute). The Java technology interpreter must find this defined exactly as given or it refuses to run the program. The following describes each element of line 3:
public: The method main() can be accessed by anything, including the Java interpreter. static: This keyword tells the compiler that the main() method is usable in the context of the class Hello. No object is needed to execute static methods. We will learn about object in Topic 5. void: This keyword indicates that the method main() does not return any values (i.e. there is no return keyword in the main() method). String[ ] args: This method declares the single parameter to the main() method. The name of the parameter is args with the type of String array. TOPIC 1 MANUFACTURING SYSTEMS W
7 Line 4 The statement System.out.println(Hello world!) in the body of the method will instruct the computer to display the message Hello world! upon execution.
Lines 5 and 6 Lines 5 and 6 contain two braces to close the method main() and class Hello respectively.
How Do We Compile and Execute a Java Application? The following activity will guide you to compile and execute Program 1.1.
Note: Before doing this activity, please download Java Development Kit (JDK) which is downloadable from myVLE (click eServices Resources for IT). The JDK version available in myVLE is version 6.0 (1.6; jdk1.6.0 update 24).
The purpose of this exercise is for you to compile and execute Program 1.1. It is compulsory for you to follow all the steps to ensure that you learn how to compile and execute a Java program using JDK.
STEP 1 By using Notepad in Windows, type program 1.1. Then, save it in a file with the name Hello.java in the jdk1.6.0_24/bin directory. (In this example, the Java file is saved in the C:\jdk1.6.0_24/bin directory. You may have different directory for jdk1.6.0. Check your correct jdk directory from the Windows Explorer).
STEP 2a Now lauch the Command Prompt from the Start button of your Windows by typing cmd without the quotes and then press <enter> as shown below.
ACTIVITY 1.1 X TOPIC 1 MANUFACTURING SYSTEMS
8
STEP 2b In your command prompt, type cd:\ to enter the directory C:\ as shown below:
Then, you need to enter the directory that stores the Java program that you have typed in Step 1. Use the command cd to enter the correct that directory as shown below: C:\jdk1.6.0_24\bin>cd jdk1.6.0_24/bin <enter>
(In this example, the Java file is saved in the C:\jdk1.6.0_24/bin directory. You may have different directory name. Check your correct jdk directory from the Windows Explorer).
STEP 3 Compile the Java program using the instruction javac Hello.java in the DOS environment. The example is shown below:
C:\jdk1.6.0_24\bin>javac Hello.java <enter> TOPIC 1 MANUFACTURING SYSTEMS W
9
If the error message is displayed, go to Step 4. If the error message is not displayed, go to Step 5.
STEP 4 Go back to Notepad and open the file that you have typed in Step 1; however, do not close the DOS window. Do the necessary correction in your program. Make sure you save your file after the corrections. Now go back to the DOS window and repeat Step 3.
STEP 5 If there is no error, the compiler will generate the bytecode file that is Hello.class. Now you are ready to execute your program. In order to do this, type the command java Hello in the eDOS Window as shown below:
C:\jdk1.6.0_24\bin>java Hello <enter>
X TOPIC 1 MANUFACTURING SYSTEMS
10
1.4.2 Writing Text-based Application Using Object- Oriented Approach In this section, we will discuss how we can rewrite Program 1.1 using an object- oriented approach. Unlike text-based applications written in a structured manner, text-based applications written in an object-oriented approach need two programs, namely: x Class definition program; and x Class program that has main() method for execution.
Note: The above two programs can be merged into a single program. We are not going to adopt this approach as this may lead to confusion.
Now we will focus on class definition. Program 1.2 is an example of a class definition.
Take note that this instruction consists of two parts. The first part (java) refers to the Java runtime interpreter. The second part (Hello) refers to the class name which has the main() method that is to be executed by the interpreter.
The program generates the output given below. It displays the string sent as a parameter to the System.out.println() method the first statement in the main() method.
TOPIC 1 MANUFACTURING SYSTEMS W
11 Program 1.2: Hello2.java (Class definition) Line Number 1 // this program displays Hello world 2 class Hello2{ 3 public void display(){ 4 System.out.println(Hello world!); 5 } 6 }
Observe that there is no main() method in the above program. Below is the explanation for Program 1.2.
Line 1 Line 1 in the program is the comment written by the programmer. It helps others to understand the nature of the program. Thus, the program becomes more readable.
Line 2 Line 2 declares the name of the class as Hello2.
Lines 3 and 4 Lines 3 and 4 show the declaration of a method that has name display. This method has the public keyword. It means this method can be accessed anywhere in this program or from other programs. Since this method does not return any value, void is used as the return type. The statement System.out.println(Hello world!) in the body of the method will instruct the computer to display the message Hello world! when the method is called.
Lines 5 and 6 Lines 5 and 6 contain two braces to close the method display() and class Hello2 respectively.
Compile this program using steps shown in Activity 1.1. Remember that you cannot execute the program. Do you know why? This is because Program 1.2 is just a class definition and does not have the main() method. You may recall that the Java interpreter must locate the main() method in order to execute the program. That is why we need to have another program that will have the main() method to execute Program 1.2. The second program is discussed below.
X TOPIC 1 MANUFACTURING SYSTEMS
12 Program 1.3: DisplayHello.java (Class program that has the main() method)
Line Number
1 // this program displays Hello world 2 class DisplayHello{ 3 public static void main (String args[]) { 4 Hello2 hello = new Hello2(); 5 hello.display(); 6 } 7 }
The detailed description for Program 1.3 is as below.
Line 1 Line 1 in the program is a comment written by the programmer. It helps others to understand the nature of the program. Thus, the program becomes more readable.
Line 2 Line 2 declares the name of the class as DisplayHello.
Line 3 Line 3 is where the program starts to execute. In this class, a method called main() contains the statements to display the Hello world! string. The main() method must be present in all Java applications because it is the first method that will be executed when an application runs (i.e. it is where the program starts to execute). The Java technology interpreter must find this defined exactly as given or it refuses to run the program. The following describes each element of line 3:
public: The method main() can be accessed by anything, including the Java interpreter.
static: This keyword tells the compiler that the main() method is usable in the context of the class Hello. No instance of the class is needed to execute static methods.
void: This keyword indicates that the method main() does not return any values (i.e. there is no return keyword in the main() method).
String[ ] args: This method declares the single parameter to the main() method. The name of the parameter is args with the type of String array.
TOPIC 1 MANUFACTURING SYSTEMS W
13 Line 4 This line shows how to create an object. The new Hello2 syntax instructs the Java interpreter to construct a new object of Hello2. hello is the object of the class Hello2.
Line 5 Line 5 shows the object hello calling the method display available in the class Hello2.
Lines 6 and 7 Lines 6 and 7 contain two braces to close the method main() and class DisplayHello respectively.
Compile Program 1.3 using the steps shown in Activity 1.1. Now you can execute Program 1.3 as it has the main() method. The output will be like this:
WRITING APPLETS An applet program is a Java program that can be executed from the web browser. Java Applet also supports GUI components. How do we differentiate an applet from an application? It is very simple. All applet programs must start with the heading that ends with the extends JApplet keywords as shown in the following program:
Program 1.4: TestButton.java (Applet program)
public class myApplet extends JApplet { public void paint(Graphics g){ g.drawString(Hello world!, 50,50); } }
1.5 All applet programs must have these keywords X TOPIC 1 MANUFACTURING SYSTEMS
14 When the program is executed, the interface produced can be seen in Figure 1.4.
Figure 1.4: Output of myApplet We will go into detail on the program above when we study applets in Topic 9. You can compile the above program using the steps shown in Activity 1.1 but you cannot use the command java to execute an applet. We are not going to elaborate on how to execute an applet at the moment as we will be doing this in Topic 9. Remember.. Take note that a frame is a GUI-based Java application that is standalone. Unlike an applet, a frame cannot be called by Web browsers. However, it is easy to develop a frame once you know how to develop GUI-based programs using an applet. You need to do some minor modifications in order to convert a GUI-based applet into a frame application. Do note that we will not be reviewing frame applications as it is not in our syllabus.
Points to Ponder.. Observe that each of the programmes from Program 1.1 to Program 1.4 produces the same output even though each was written using different approaches. Which approach would you choose from the following: x Java programs written in a structured approach; x Java programs written in an object-oriented approach; or x Applet-based Java programs. The answer depends on your problems requirements. Always remember that Javas strength lies in the capability of the object-oriented paradigm that also supports Internet/Web platform. The object-oriented paradigm in Java makes it easy to reuse applications. We will look at the basic constructs and structured programming in Java in Topics 1 to 4. Topics 5 to 7 deal with object-oriented programming while applet and its associated topics will be discussed in Topics 9 to 11. TOPIC 1 MANUFACTURING SYSTEMS W
15
x Starting from a failed project, Java has emerged as a popular programming language within a short period of time. x The strength of Java lies in its object-oriented paradigm, which has contributed to its success. x The object-oriented paradigm in Java makes it easy to reuse the applications. x In addition, different types of Java programs were discussed in this topic. x It is important to understand the types of Java programs and be able to compile and execute those programs. x Below is a summary of Java applications and applets as discussed:
Type of Java program How it can be written Where the output will be produced Text-based Application Structured OR Object- oriented (OO) Command Prompt Frame Object-oriented Windows of the users PC operating system Applet Object-oriented Web browsers (Internet Explorer, Netscape, etc)