Chapter 1 – Introduction to Java Programming TEXTBOOK ׀1
CHAPTER 1
Introduction to Java Programming
Overview
This chapter discusses bit of Java history, Java technology, and Java features. Also, this
chapter discusses the phases of a Java program and structure of a Java class.
Topics
History of Java programming
Java technology
Features of Java
Phases of a Java Program
Structure of a Java class
Errors
o Syntax errors
o Run-time errors
o Logic errors
Learning Objectives (LOs)
At the end of this Chapter, you are expected to:
1. Recognize the history of Java programming language;
2. Descibe the features of Java technology such as the Java Virtual Machine, garbage collection
and code security;
3. Describe the different phases of a Java program;
4. Explain the structure of a Java class; and
5. Identify types of errors.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀2
Name : ______________________________________________________ Date : ______________
Course/Year/Section : __________________________________________ Score: ______________
Learning Objective 1: Recognize the history of Java programming language
Learning Objective 2: Descibe the features of Java technology such as the Java
Virtual Machine, garbage collection and code security.
What Do You Already Know?
Let us determine how much you already know about the History of Java Programming and
features of Java technology by answering Pre-assessment for LO1 and LO2.
Pre-assessment for LO1 and LO2
Multiple Choice. Direction: Read each statement carefully; select the letter of the correct
answer and write your answer on the space provided before the number. (5 items)
_______1. He is the founder of Java.
a. Bill Gates b. James Gosling c. James Gopher d. Steve Johnson
_______2. The original name of Java is.
a. Oak b. Oakley c. Melina d. Java Beans
_______3. JVM stands for ___
a. Java Virtual Mechanism c. Java Virtual Machine
b. Java Vintage Machine d. Java Virtual Machinery
_______4. Is responsible for freeing any memory that can be freed.
a. Garbage Collection Thread c. CPU
b. Random Access Memory d. Garbage Collection
_______5. Is responsible for loading all classes needed for the Java program.
a. Class Loader b. Progress Bar c. Status Bar d. Class Verifier
Refer to the Answer Key. What is your score?
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀3
What Do You Need to Know?
Read the Information Sheet 1.1 very well then find out how much you can remember and
how much you learned by doing Self-check 1.1 found at the end of this information sheet.
Information Sheet 1.1
1.1. History of Java Programming Language
Java was created in 1991 by a team led by James Gosling at Sun Microsystems. Initially
called Oak, in honor of the tree outside Gosling's window, it was designed for use in embedded
chips in consumer electronic appliances like toasters and refrigerators. One of the first projects
developed using Java was a personal hand-held remote control named Star 7.
In 1995, when the Internet is gaining popularity, Gosling et. al. realized that Java could be
used for Internet programming thus Java was redesigned for developing Internet applications. The
name oak was changed because an existing programming language was of the same name. Java
then becomes enormously popular and its rapid rise and wide acceptance can be traced to its design
characteristics, particularly its promise that you can write a program once and run it anywhere. As
stated by Sun, “Java is simple, object oriented, distributed, interpreted, robust, secure, architecture
neutral, portable, high performance, multithreaded, and dynamic.”
Today, Java is now a full- pledged and powerful language that can be used in many ways. It
comes in three editions: Java Standard Edition (Java SE), Java Enterprise Edition (Java EE), and Java
Micro Edition (Java ME). Java SE can be used to develop client-side standalone applications or
applets. Java EE can be used to develop server-side applications, such as Java servlets and
JavaServer Pages. Java ME can be used to develop applications for mobile devices, such as mobile
phones.
For this course, we will be using Java SE to introduce Java programming.
1.2. What is Java Technology?
The Java Technology is:
A programming language
A development environment
An application environment
A deployment environment
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀4
1.2.1 As a programming language, Java can create all kinds of applications that you
could create using any conventional programming language like web
applications, stand-alone and distributed applications, and mobile applications.
1.2.2 As a development environment, Java technology provides you with a large suite
of tools:
- A compiler,
- An interpreter,
- A documentation generator,
- A class file packaging tool
1.2.3 As an application and runtime application, java technology applications are
typically general-purpose program that run on any machine where the Java
runtime environment (JRE) is installed.
1.2.4 As deployment environments, the JRE and web browsers are considered
its two main deployment environments.
- First, the JRE supplied by the Java 2 Software Development Kit (SDK)
contains the complete set of class files for all the Java technology packages,
which includes basic language classes, GUI component classes, and so on.
- Second, the other main deployment environment is on your web browser.
Most commercial browsers supply a Java technology interpreter and
runtime environment.
1.3. Java Features
Some features of Java:
1.3.1. The Java Virtual Machine (JVM)
1.3.2. Garbage Collection
1.3.3. Code Security
1.3.1 The Java Virtual Machine (JVM)
- Java Virtual Machine (JVM)
o is an imaginary machine that is implemented by emulating software on a
real machine.
o It provides the hardware platform specifications to which you compile all
Java technology code. This specification enables the Java software to be
platform-independent because the compilation is done for a generic
machine known as the JVM.
- Bytecode
o A special machine language that can be understood by the Java Virtual
Machine (JVM).
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀5
o The bytecode is independent of any particular computer hardware, so any
computer with a Java interpreter can execute the compiled Java program,
no matter what type of computer the program was compiled on.
1.3.2 Garbage Collection
- Garbage Collection Thread
o Responsible for freeing any memory that can be freed. This happens
automatically during the lifetime of the Java program.
o Programmer is freed from the burden of having to deallocate that memory
themselves.
Many programming languages allow a programmer to allocate memory during runtime.
However, after using that allocated memory, there should be a way to deallocate that memory
block in order for other programs to use it again. In C, C++ and other languages the
programmer is responsible for this. This can be difficult at times since there can be instances
wherein the programmers forget to deallocate memory and therefore result to what we call
memory leaks.
1.3.3 Code Security
- Code security is attained in Java through the implementation of its Java Runtime
Environment (JRE).
- The JRE runs code compiled for a JVM and performs class loading (through the class
loader), code verification (through the bytecode verifier) and finally code execution.
- The Class Loader
o is responsible for loading all classes needed for the Java program.
o It adds security by separating the namespaces for the classes of the local file
system from those that are imported from network sources. This limits any
Trojan horse applications since local classes are always loaded first.
o After loading all the classes, the memory layout of the executable is then
determined. This adds protection against unauthorized access to restricted
areas of the code since the memory layout is determined during runtime.
- Bytecode verifier
o After loading the class and layouting of memory, the bytecode verifier then
tests the format of the code fragments and checks the code fragments for
illegal code that can violate access rights to objects.
After all of these have been done, the code is then finally executed.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀6
Name : ______________________________________________________ Date : ______________
Course/Year/Section : __________________________________________ Score: ______________
How much have you learned?
Self-check 1.1 for LO1 and LO2
Multiple Choice. Directions: Answer the following. Choose the best answer from the choices
provided and write the letter of your choice on the space provided before the number. (10 items)
_______1. A special machine language that can be understood by the Java Virtual Machine
(JVM).
a. Bytecode b. JVM c. Bitcode d.Bytecode Verifier
_______2. A programming language, a development environment, an application environment
and a development environment is termed as
a. Java Technology b. JavaScript c. Java d. J++
_______3. Java is created on what year?
a. 1995 b. 1991 c. 1990 d. 1994
_______4. It is an imaginary machine that is implemented by emulating software on a real
machine.
a. JRE b. JVM c. SDK d. IDE
_______5. A slogan created by Sun MicroSystems to illustrate the cross-platform benefits of
the Java Language.
a. write, compile and execute c. write a program more and run it anywhere
b. write a program once and run it anywhere d. write program less and run it anywhere
_______6. He is the founder of Java.
a. Bill Gates b. James Gosling c. James Gopher d. Steve Johnson
_______7. The original name of Java is.
a. Oak b. Oakley c. Melina d. Java Beans
_______8. JVM stands for
a. Java Virtual Mechanism c. Java Virtual Machine
b. Java Vintage Machine d. Java Virtual Machinery
_______9. Is responsible for freeing any memory that can be freed.
a. Garbage Collection Thread c. CPU
b. Random Access Memory d. Garbage Collection
_______10. Is responsible for loading all classes needed for the Java program.
a. Class Loader b. Progress Bar c. Status Bar d. Class Verifier
IMPORTANT: Now check your answers against the Answer keys. If you got 95%-100% of
the items correctly, proceed to the next Activity. If not, carefully reviewing the lesson
helps you understand the concepts better. Concentrate on part that you cover the
questions you missed. After this, you are very much ready to proceed to the next learning
activity.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀7
Name : ______________________________________________________ Date : ______________
Course/Year/Section : __________________________________________ Score: ______________
Learning Objective 3: Describe the different phases of a Java program.
Learning Objective 4: Explain the structure of a Java class.
Learning Objective 5: Identify the types of errors.
What Do You Already Know?
Let us determine how much you already know about the different phases of a Java program
and structure of a Java class.
Pre-assessment 1.2 for LO3, LO4, and LO5
Matching type. Directions. Match COLUMN A with COLUMN B. Write the answer in the
space provided before the question.
COLUMN A COLUMN B
_________1. It is used to compile your Java source file. .java
_________ 2. It is used to input single comment in Java program. javac
_________ 3. Extension file name of Java source file. Notepad
A text editor used to write java source code for
_________4. //
command prompt.
_________5 It is used to run your compiled Java source file java
Refer to the Answer Key. What is your score?
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀8
What Do You Need to Know?
Read the Information Sheet 1.2 very well then find out how much you can remember and
how much you learned by doing Self-check 1.2.
Information Sheet 1.2
1.4. Phases of a Java Program
Figure 1.1 Phases of a Java Program
Step 1: The first step in creating a Java program is by writing your programs (source code) in
a text editor like notepad or wordpad.
This file save should be save into your hard disk or other secondary storage media
with the file extension .java.
Step 2: After creating and saving your Java program, you must compile the program by
using the Java Compiler into a .class file also called Java bytecodes.
Step 3: The .class file is then interpreted by the Java interpreter that converts the
bytecodes into the machine language of the particular computer you are using.
Task Tools Output
Write/Create a source file Any text editor – (Notepad) File with a .java extension
Compile the program(source Java Compiler File with a .class extension (Java
file) bytecodes)
Run the application Java Interpreter Program output
Table 1.1 Summary of Phases of a Java Program
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀9
1.5.The Structure of a Java Class
Java is case sensitive, that means if a keyword is supposed to be typed in uppercase and
then you type it in lowercase you'll get an error when compiling the code.
Also, note that Java code instructions ends with a semicolon (;). Missing on the proper case
and omitting the semicolon are some of the common errors in Java programming by beginners so,
watch out!
Let us now examine the following segment of our code:
Code editor window
Notice in the code editor diagram above the class is called
MyFirstProgram.java
public class MyFirstProgram.java {
This is the same name as the java source file in the project window MyFirstProgram.java.
When you run the program the compiler demands that the source file and the class name should
match.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀10
So if the .java file is called myfirstprogram but the class is called MyFirstProgram
then an error will occur in your program, however the package name can be different and it doesn’t
have to be the same as the source file or the class name.
Here is our code window without comments:
Let us now examine the following segment of our code:
public class MyFirstProgram {
The above part of the code is called a class segment and it denotes the starting and ending
of a Java class segment code. The start of a code segment is done with a left curly brace ( {) and ends
with a right curly brace (}). Any code inside the left and right curly braces belongs to that code
segment.
Notice inside the curly braces for the class segment is another code segment shown below:
public static void main( String[ ] args ) {
The above part of the code is called a method, public denotes that this segment of the
code can be accessed outside the class without creating any new objects as denoted by the keyword
static. void means this part of the code does not return any value.
The “main” part denotes the starting point of the Java program. When you run the program,
Java compiler locate for this part of the code and execute any code inside “main”. Always include a
“main” in your code.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀11
Do Laboratory Exercise 1.1: First Java Program using text editor found in
your Java workbook.
Do Laboratory Exercise 1.2: First Java application using NetBeans IDE found
in your Java workbook.
Do Laboratory Exercise 1.3: Java application using a dialog box found in
your Java workbook.
1.6. Errors
What we’ve shown so far is a Java program wherein we didn’t encounter any problems in
compiling and running. However, this is not always the case because we usually encounter errors
along the way.
Basically there are three types of errors that you will contend with when writing programs:
• Syntax errors (or semantic errors)
• Runtime errors
• Logic errors
1.6.1 Syntax error
Syntax errors represent grammar errors in the use of the programming language.
Common examples are:
• Misspelled variable and function names
• Missing semicolons at the end of a statement.
• Improperly matches parentheses, square brackets and curly braces
• Incorrect format in selection and loop statements
• Other common mistakes are:
Capitalization,
Spelling,
The use if incorrect special characters, and
Omission of correct punctuation.
Java attempts to isolate the error by displaying the line of code and pointing to the
first incorrect character in that line. However, the problem may not be at the exact point.
As a rule of thumb, if you encounter a lot of error messages, try to correct the first
mistake in a long list, and try to compile the program again. Doing so may reduce the total
number of errors dramatically.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀12
Do Laboratory Exercise 1.4: Java application that shows syntax error found
in your Java workbook.
1.6.2 Run-time error
Runtime errors occur when a program with no syntax errors asks the computer
to do something that the computer is unable to reliably do.
Run-time errors are errors that will not display until you run or execute your
program.
Even programs that compile successfully may display wrong answers if the
programmer has not thought through the logical processes and structures of the
program.
Common examples are:
• Trying to divide by a variable that contains a value of zero
• Trying to open a file that doesn't exist
There is no way for the compiler to know about these kinds of errors when the
program is compiled.
Do Laboratory Exercise 1.5: Java application that shows runtime error found
in your Java workbook.
1.6.3 Logic Errors
Logic errors occur when there is a design flaw in your program. Common examples
are:
• Multiplying when you should be dividing
• Adding when you should be subtracting
• Opening and using data from the wrong file
• Displaying the wrong message
Do Laboratory Exercise 1.6: Java application that shows logic error found in
your Java workbook.
Chapter 1 – Introduction to Java Programming TEXTBOOK ׀13
Name : ______________________________________________________ Date : ______________
Course/Year/Section : __________________________________________ Score: ______________
How much have you learned?
Self-check 1.2
Matching Type. Directions: Match COLUMN A with COLUMN B. Write the answer on the
space provided before the question.
COLUMN A COLUMN B
_______________1. It is used to run your compiled Java source file java
_______________2. It denotes the starting and ending of a Java class case sensitive
segment code.
_______________3. A text editor used to write java source code for javac
command prompt.
_______________4. A part which denotes the starting point of the Java F6
program.
_______________5. It is used to input single comment in Java program. Notepad
_______________6. The most common error when compiling and main
running.
_______________7. The easiest way to run a Java program. syntax Error
_______________8. Java is ___, because the variable Hello is not the public
same as the variable hello
_______________9. It is used to compile your Java source file. .java
_______________10. Extension file name of Java source file. //
How Do You Apply What You Have
Learned?
Do Activity/Job Sheets 1.1, 1.2, 1.3 and 1.4 found in your Java workbook.
IMPORTANT: Now check your answers against the Answer keys. If you got 95%-100% of
the items correctly, proceed to the next Activity. If not, carefully reviewing the lesson
helps you understand the concepts better. Concentrate on part that you cover the
questions you missed. After this, you are very much ready to proceed to the next learning