INTRODUCTION TO
JAVA PROGRAMMING
CHAPTER 1
The term PROGRAMMING
pertains to the method of
writing scripts or code.
Programs or software is the
by-product of programming.
Each computer program is
done through point by point
procedures called PROCESS.
Computer Programs are a
series of commands you write to
inform your machine what it
should perform. It is possible
through the use of a
programming language, which
refers to the series of syntax
used to implement algorithms.
Comparing the concepts of
Procedural and Object-Oriented
Programming
Procedural programming is a
programming style in which action is
executed one series after the other. It
encompasses using your
programming skills to create
software. To make it simple, each
operation is clustered into logical
units commonly referred to as
procedure, modules methods,
functions, and subroutines.
Object-Oriented Programming
(OOP) is an expansion of procedural
programming that allows you to take a
somewhat alternative approach to
computer program writing. OOP is
commonly utilized in two types of
applications namely, computer
applications and Graphical User
Interface (GUI). Basically, OOP
involves creating classes, objects,
and applications that uses those.
Understanding Classes, Objects,
and Encapsulation
Class is a concept defining a set of
objects with same properties. A
class definition defines what
characteristic the objects should
have, and what those objects can
do.
Objects are anything we see and
hold. It is constructed by a method
and attributes. Method is a group
of code that performs a specific
task while attribute refers to the
characteristic that describes an
object. As an analogy, we can say
that a class shirt shows what shirt
objects looks like.
It has properties such as size, color
and cut. Each shirt may have the
same attribute but we cannot say
that it contains the same value for
each attribute. A shirt may be for
male, female or both. This analogy
applies to all objects we can think
of.
Encapsulation pertains to
putting data and methods inside
an object. This concept helps in
securing those data and
methods from unauthorized
modifications.
The Java Language
Java was first introduced by Sun
Microsystems and was further acquired
by Oracle in 1995. It’s first name was
Oak. It was immediately collaborated
with Netscape Navigator’s Java Virtual
Machine (JVM). Basically, Java
adapted the syntax of c++. It just
applied some minor adjustments.
Programmers eventually adjusted
to Java when they realized its ease
of use. A program written in Java will
be compiled first into a byte-code. It
will be then translated into a run
time through the JVM. Java Virtual
Machine is all you need to run a
Java program and it is accessible to
any computer platforms.
A Java Program can be written as the
following:
1.Applets pertain to little programs that
are launched using a browser. It is
intended to add interactions to any
websites.
2.Serverlets are basically raised by the
browser. These are programs that run
through the server even without being
downloaded to the user.
3.Applications unlike applets, Java
applications can run standalone. It can
be through Console and/or Windowed
Programming
Basics Like any other language,
you should understand that in
programming, you are to perform
an action or task through
operations that a computer can
do—the input, process and output.
These operations are performed in
order to achieve a quality output.
This concept is known as
sequential processing, where
each instruction is done in pattern.
In developing an application in
Java, there are three essential
steps to take, namely;
1. Writing code;
2. Compiling code; and
3. Executing application.
In Writing Code, we can encode our scripts
(called source code) in a text file. You can use
any text editor such as Notepad, Wordpad, etc.
It is necessary to save it using the .java
extension.
After writing your source code, the next step is
to compile. In compiling, Java will create a
.class file that includes the byte-code and will
soon be translated by the JVM into instructions
that the machine understands.
We use the command javac followed by the file
name of your .java file to compile our program.
For example: javac HelloCict.java
If we compile our program successfully, a new file
with .class extension will be created. Finally, to run
our application, we invoke the JVM through the
command java followed by the name of your class.
This is how it will look like:
java HelloCict
Figure 1. HelloCict program code
The source code on Figure 1 will show
an output “Welcome to CICT!” after it
undergoes compiling and running.
This simple code on Figure 1 consists
of 7 lines. Each line signifies different
parts.
The word public is an identifier which
defines the instances that can be
accessed by any of the methods.
The public class HelloCict is
called class header. The class
keyword signifies an identifier while
the HelloCict pertains to the class
name. public in the header is also
an identifier which defines the
instances where a class can be
read.
Following the class header is an opening
curly brace that encloses the class body.
Class body are the block of codes inside the
first pair of curly braces.
The class body where the method is written.
A header written as public static void main
(String [] Args) is necessary in a Java
application.
On line 4, another opening curly braces is
seen. It encloses the method body—
anything inside these curly braces.
The method body is where Java statements are
written. The example on Figure1, will print the
text “Welcome to CICT!” which is a literal
string—characters that display same way as it
is written. Any String of characters in Java must
place between the double quotation marks (“ ”).
To print this statement, we must use the
System.out.print().
Notice on Figure2 that we first find the
directory where the code has been saved
(C:\Users\UserDesktop\JavaFolder) and we
compiled it using the command javac
HelloCict.java and finally executed the
program after successful compilation using
java HelloCict.
Locating and Correcting Errors in Java is
obviously easy. When the compiler has
detected an error on your source code, the
compiling will not take place successfully. It
will display the error you need to correct and
For example, we omit the semicolon on the
statement in line 5;
The compiler informs that 1 error in
line5 has an error because it lacks a
semicolon (;) at The end of the
statement, where the caret (^) is
located.
The examples above is a Java
application that uses console. On the
other hand, we can also create a Java
Application that produces a GUI
Output—known as windowed.
The most basic example of GUI is a
dialog box where you can display a
message in it. To do so, calling a class
that produces dialog box is a must. This
refers to the JOptionPane class. In line1,
an import statement is written. It is an
access to a given Java class that is
included in the classes called package
where JOptionPane (the class that
creates GUI like dialog boxes) belongs
to.
Line2 shows the class header. This is same as the
class header that is indicated on the console
application, so as with the line4 where method
header and main() is also declared.
Line2 shows the class header. This
is same as the class header that is
indicated on the console application,
so as with the line4 where method
header and main() is also declared.
In line5, the JOptionPane class has
been called to create a GUI in the
form of a dialog box.
In the example shown on Figure5, a
Message Dialog Box has been used
through the method
showMessageDialog(). (Other
types of dialog boxes will be
discussed on the lesson Swing
Components).
It is followed by a set of parentheses which
requires two arguments, the null— which sets
the dialog box at the center of the screen and
the String that will be displayed inside the
dialog box