LMC - AEC 09 - Chapter 03
LMC - AEC 09 - Chapter 03
Disclaimer
This learning material is used in compliance with the flexible teaching-learning approach
espoused by CHED in response to the pandemic that has globally affected educational institutions.
Authors and publishers of the contents are well acknowledged. As such, the college and its faculty
do not claim ownership of all sourced information. This learning material will solely be used for
instructional purposes not for commercialization.
3
MODULE 3: INTRODUCTION TO JAVA PROGRAMMING
Chapter
r
Introduction
to
Java
Programming
Chapter 3: Introduction to Java Programming
Module Overview
This module will provide the fundamentals of java programming. It will cover the following
lessons:
This lesson presents lectures on how to create, compile and run a simple
Java program using notepad or an integrated development environment.
Learning Outcomes
3
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Module Map
Describe Java
Programming
Design, implement, test, and
debug a program, based on a
given specification, that uses
each of the following
fundamental programming
Explain the components: (1) primitive
development process data types, (2} basic
of Java Programming computation, (3) simple I/0,
(4) conditional and iterative
structures, (5) definition of
functions and parameter
passing, and (6) recursion
4
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Introduction
Java is the universal programming language used around the world both in
education and in the industry. It is an open source language that is readily available in
the Internet for download and use.
Java is used primarily in school in teaching computer programming concepts among
students. The syntax and commands are easy to understand and teaches the students to
write better because it uses object oriented concepts in writing a program.
5
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
What is Java?
JAVA is both an application development platform and a programming language.
It is a simple, robust, secured high-level general-purpose language that is object-oriented,
class-based, and web enabled.
The Java programming language was developed by Sun Microsystems in the
early 1990s. Although it is primarily used for Internet-based applications, Java is a simple,
efficient, general-purpose language. Java was originally designed for embedded network
applications running on multiple platforms. It is a portable, object-oriented, interpreted
language.
Java is extremely portable. The same Java application will run identically on any
computer, regardless of hardware features or operating system, as long as it has a Java
interpreter. Besides portability, another of Java's key advantages is its set of security
features which protect a PC running a Java program not only from problems caused by
erroneous code but also from malicious programs (such as viruses). You can safely run
a Java applet downloaded from the Internet, because Java's security features prevent
these types of applets from accessing a PC's hard drive or network connections.
An applet is typically a small Java program that is embedded within an HTML page.
Java can be considered both a compiled and an interpreted language because its
source code is first compiled into a binary byte-code. This byte-code runs on the Java
Virtual Machine (JVM), which is usually a software-based interpreter. The use of compiled
byte-code allows the interpreter (the virtual machine) to be small and efficient (and nearly
as fast as the CPU running native, compiled code). In addition, this byte-code gives Java
its portability: it will run on any JVM that is correctly implemented, regardless of
computer hardware or software configuration. Most Web browsers (such as Microsoft
Internet Explorer or Netscape Communicator) contain a JVM to run Java applets.
Java programs are more highly structured than C++ equivalents. All functions (or
Java methods) and executable statements in Java must reside within a class while C++
allows function definitions and lines of code to exist outside of classes (as in C-style
6
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
programs). Global data and methods cannot reside outside of a class in Java, whereas
C++ allows this. These restrictions, though cumbersome at times, help maintain the
integrity and security of Java programs and forces them to be totally object-oriented.
Another key feature of Java is that it is an open standard with publicly available
source code. Sun Microsystems controls the Java language and its related products but
Sun's liberal license policy contributed to the Internet community embracing Java as a
standard. You can freely download all the tools you need to develop and run Java applets
and applications from Sun's Java Web site
It is used for:
• Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.)
• It is one of the most popular programming language in the world
• It is easy to learn and simple to use
• It is open-source and free
• It is secure, fast and powerful
• It has a huge community support (tens of millions of developers)
• Java is an object oriented language which gives a clear structure to programs and
allows code to be reused, lowering development costs
• As Java is close to C++ and C#, it makes it easy for programmers to switch to
Java or vice versa
7
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
NAME:
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
Compare Java, C and C++. Check the similarities (if there is any) and differences.
(15 pts.)
Java C C++
8
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Java was initially named as Oak, a tree outside of James Gosling’s window. It
was renamed and relaunched to Java when the web and the Internet was becoming
popular by which Java can be used for web programming.
The drive for developing Java language was the desire to have a platform
independent language to be rooted in various electronic products such as toasters and
refrigerators. A personal hand-held remote control named Star 7 was the first project
developed using Java language.
Figure 1 below shows the Java versions in its history, for Java Standard
Edition (SE) Development Kit (JDK). The JDK 13 version is set to be released in
September 2019.
JDK 1.0 J2SE 1.2 J2SE 1.4 JDK 6 JDK 8 JDK 10 JDK 12
Oak Playground Merlin Mustang March 2014 March 20, March 19,
December February 2002 December 2018 2019
January 1996
2006
1998
From the table above we can see that the naming and the version number
have been changing over times:
• Versions 1.0 and 1.1 are named as JDK (Java Development Kit).
9
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Major versions were released after every 2 years, however the Java SE 7
took 5 years to be available after its predecessor Java SE 6, and 3 years for Java
SE 8 to be available to public afterward.
Since Java SE 10, new versions will be released very six months.
Java versus C, C++ and C#
Java is correlated to C, C++ and C# languages. The syntax of Java was
derived from C and C++. Java is basically the Internet version of C++ and its object
model is adapted from C++.
C# adapted the its features from Java, and both share the same C++ style
syntax, support distributed programming, and apply the same object model. Java is
easy to learn when you are already familiar with C#.
10
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
11
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
The Java Virtual Machine is an engine that executes the java code. When
java programs are compiled, the bytecode is generated. The bytecode is converted
into a machine language.
The JVM is both a specification and an implementation. It is a specification
because it provides runtime environment in which java bytecode can be executed.
And that the implementation of JVM is through the Java Runtime Environment
(JRE). An instance of JVM is created every time java command is written on the
command prompt to run the java class which is known as Runtime Instance.
12
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
getProduct.java
getProduct() { COMPILER
getProduct.class
}
getSum.java
getSum() {
getSum.class
}
RAM
JVM
Class
Loader
compute.class
JVM Memory
JVM Native
Class PC
Heap Language Method
Area Stack
Register
Stacks
getProduct.class
Java Runtime Access
13
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
The Bytecode
Bytecode is a special machine language that can be understood
and interpreted by the JVM. The bytecode is not computer hardware
dependent. Java programs can be compiled into bytecodes on any
platforms that contains Java Compiler. Using the bytecode resolves
security and portability issues for Internet programming.
INTERPRETER
Windows
INTERPRETER
compute.java COMPILER
Macintosh
INTERPRETER
Macintosh
14
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
The Java Runtime Environment (JRE) runs the codes compiled for JVM
and performs class loading, code verification and code execution. It does provide
set of libraries, JVM and other components to run Java applications. JRE can be
downloaded from java.sun.com and www.oracle.com/technetwork/java.
3. JDK, JRE, JVM: What Are They and What Are Their Differences?
[youtube] https://www.youtube.com/watch?v=BXFHuaQNnLo
15
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
16
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Web Container
Database
Application
Client
Enterprise Enterprise
Bean Bean
Application
Client Container
EJB Container
Application
Vendor
Optional
Profile Specific
Packages
Classes-
OEM
Configuration
Native Operating System
Device/Hardware
18
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Packages Description
JSR 75 PIM PDA Package
JSR 82 BTAPI Java APIs for Bluetooth
JSR 120 WMA Wireless Messaging API
JSR 172 J2ME Web Service
JSR 179 Location API for J2ME 12
4. JavaFX
JavaFX platform is used to create rich internet applications. It uses a light-
weight user interface API and hardware-accelerated graphics and media engines to
take advantage of higher-performance clients and a modern look-and-feel as well as
high-level APIs for connecting to networked data sources. JavaFX applications may
be clients of Java EE platform services.
Quantum Toolkit
Web
Prism Glass Media
view
Win32 OpenGL G
Web kit streams
GTK D3D
19
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
20
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
21
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
1. Simple 2.
14. Platform
Dynamic Indepen-
dent
13. 3.
Distribute Object-
d Oriented
12. Multi-
4. Fast
Threaded
JAVA
Features
11. High
Perfor- 5. Secure
mance
10. 6. Large
Interprete Standard
d Library
9.
Architec- 7.
ture Portable
Neutral 8. Robust
1. Simple
3. An object-oriented Language
Java is an object-oriented programming language just like C++ and Python
languages. Writing programs using object-oriented style is a popular one. Object-
22
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
4. Java is fast
Java is one of the fastest programming languages like C or C++ languages
and even faster that Python and PHP languages.
5. Java is secure
Java provides a secure platform for developing and running virus-free
applications. It handles automatic memory management, reduces memory corruption
and vulnerability and provides secure communication by protecting the integrity and
privacy of data transmitted.
7. Portable
Java is portable because it facilitates you to carry the Java bytecode to any
platform. It doesn't require any implementation.
8. Robust
9. Architecture Neutral
23
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
10. Interpreted
12. Multi-Threaded
13. Distributed
14. Dynamic
Java is a dynamic language. It supports dynamic loading of classes. It means
classes are loaded on demand. It also supports functions from its native languages,
i.e., C and C++.
Java supports dynamic compilation and automatic memory management
(garbage collection).
24
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
2.
3.
25
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
3. Embedded Systems: SIM cards, blue-ray disk players, utility meters and
televisions, use embedded Java technologies.
26
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
1.
2.
3.
4.
27
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Some of the list of terms you should know before you start learning Java
programming.
Java Class A Java class is a template definition of the methods and variables in
a particular kind of object. An object can be a specific instance of a
class and can contain real values instead of variables.
Class Instance A specific implementation of the Java class.
Static Class A class method that can be called without instantiating the class.
Method
Static Class A class variable that can be accessed without instantiating the class.
Variable
Constructor Initiates the variables and method for a new class instance.
Method
Java Virtual Interprets the bytecode into code that will run on actual computer
Machine hardware.
J2EE Enterprise A Java platform enterprise server that creates standardized, reusable
Server modular components.
28
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
1. Object-oriented programming uses classes and objects. What are classes and
what are objects? What is the relationship between classes and objects?
29
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Java Applications can be created in any text editors such as Notepad, Wordpad,
TextPad and Notepad++ to name a few.
Step 3: Type: java – version in the command prompt to know the version of
JDK/JRE installed. If successful, the following shall appear:
30
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
31
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
32
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
33
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Click to open
the environment
variable window
Click Edit to
open the Edit
Environment
Variable window
34
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
C:\Program
Files\Java\jdk
1.8.0_131\bin
Output of the
program
35
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Eclipse IDE
Eclipse is an excellent open source IDE for Java developed by
International Business Machines (IBM) team and was written in Java itself.
Eclipse is written in Java and will thus need an installed JRE or JDK in
which to execute. Check for the java version installed.
Installing Eclipse
1. Download Eclipse IDE on www.eclipse.org.
2. Unzip the downloaded file on Program Files.
3. Create a Desktop shortcut.
36
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Welcome to Eclipse
37
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
38
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Creating a Class
• To create a class,
simply click on the
New button, then
select Class
• This brings up the
new class wizard
• From here you can
specify the
following...
– Package
– Class name
– Whether or
not to include
a main
– Etc…
• Fill in necessary
information then
click Finish to
continue
39
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
40
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
To run the code, right click on the class and select Run As →
Java Application
41
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
• The output of running the code can be seen in the Console tab in
the bottom pane
• After you run the code a first time, you can re-run it just by
selecting it from the run drop down menu
42
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Debugging Code
• Eclipse
comes with a
pretty good
built-in
debugger
• An easy way to enter debug mode is to right click on the class and
select Debug As → Java Application
43
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
• The first time you try to debug code you will be presented with the
following dialog
Debug Perspective
44
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Javadoc Assist
45
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Tasks
• Eclipse allows you to insert reminders into your code and stores
them for you to come back and revisit them
• You can even add your own custom tasks through the
preferences menu
46
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
• To add a table of all reminders in all of your source code you can
add the Tasks view by clicking on Window → Show View →
Tasks
47
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Local History
• Eclipse maintains a local history of file revisions which can be
accessed by right clicking on the class, then selecting
Compare With → Local History…
• Previous saved revisions are displayed in the History pane, double click a
revision to view in the built-in diff viewer
48
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
Synthesis
This module has given a tour inside Java, digging the corners of Java internal
architecture. You now have a grasp on how to create, run and compile java programs
using a text editor such as notepad.
What was covered in this module are the components of Java’s architecture, what
Java Virtual Machine (JVM) is and how it functions, the internals of the JVM architecture,
and the Java class file format.
Given the foundations of Java, you are now ready to begin learning how to use
all the various components of the Java platform in detail, starting with library, class, and
method design.
1. Tutorials Point (India( Ltd, I. (2018). Java - Environment Setup [YouTube Video]. In
YouTube. https://www.youtube.com/watch?v=bxIZ1GVWYkQ
49
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
1. Create a class named happiness. The program should output the following lines
on the screen:
2. Write a Java program to print 'Hello' on screen and then print your name on a
separate line.
4. Write a Java program to divide two numbers and print on the screen.
5. Write a Java program that takes two numbers as input and display the product of
two numbers.
50
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
PROGRAM / YR / BLK:
COURSE CODE / TITLE: AEC9 – Business Logic
TIME / DAY:
CHAPTER3
Assignment
1. Write a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers.
2. Write a Java program that takes a number as input and prints its multiplication
table up to 10.
4. Write a Java program that takes three numbers as input to calculate and print the
average of the numbers.
51
Catanduanes State University College of Information and Communications
Technology
Chapter 3: Introduction to Java Programming
References
Farrell, J. (2017). Programming logic & design, comprehensive (9th ed.). Cengage
Learning.
Linda DeMichiel, Bill Shannon (2017). Oracle: Java Platform, Enterprise Edition (Java
EE) Specification, v8 Oracle America, Inc.
Oracle. (2012, April 1). Differences between Java EE and Java SE - Your first cup: An
introduction to the Java EE platform. Moved.
https://docs.oracle.com/javaee/6/firstcup/doc/gkhoy.html
Oracle. (2020). Java(TM) web start. Oracle | Integrated Cloud Applications and Platform
Services. Retrieved May 11, 2020, from
https://www.oracle.com/technetwork/java/javase/tech/index
Parewa Labs Pvt. Ltd. (n.d.). Learn Java programming. Programiz: Learn to Code for
Free. Retrieved June 2, 2020, from https://www.programiz.com/java-programming
52
Catanduanes State University College of Information and Communications
Technology