07163833Topic1IntroductiontoJava PDF
07163833Topic1IntroductiontoJava PDF
Java
1
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 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 ConferenceTM-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.
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.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.
Lcxc"Kpvgtrtgvgt
*Rgpvkwo+
Lcxc"Kpvgtrtgvgt"
Lcxc"Rtqitco" Lcxc"Eqorkngt Lcxc"
*Rqygt"RE+
Lcxc"Kpvgtrtgvgt"
*URCTE+
(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.
(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.
(f) Multi-Threaded
Java is inherently multi-threaded. A single Java program can have many
different things processing independently and continuously.
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)
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.
Lcxc"Rtqitco"
Crrnkecvkqp Crrngv"
Vgzv/dcugf"crrnkecvkqp Htcog
Htcog" ku" pqp/Ygd" uvcpf" cnqpg" IWK"
rtqitco"
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.
ACTIVITY 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.
8 X TOPIC 1 MANUFACTURING SYSTEMS
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:
(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:
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:
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.
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.
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 display0 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!fi 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.
12 X TOPIC 1 MANUFACTURING SYSTEMS
Program 1.3: DisplayHello.java (Class program that has the main() method)
Line Number
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 DisplayHello0
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:
When the program is executed, the interface produced can be seen in Figure 1.4.
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.
Type of Java program How it can be written Where the output will
be produced
Text-based Structured OR Object- Command Prompt
Application oriented (OO)
Frame Object-oriented Windows of the userÊs
PC operating system
Applet Object-oriented Web browsers
(Internet Explorer,
Netscape, etc)