1
Lesson 03: Introduction to Java
and
Basic Programming Concepts
Dr.Solaf A. Hussain
University of Sulaimani
College of Science
Department of Computer
Dec. 2021 Problem Solving and Programming Concepts
Objectives
2
Overview history of Java.
To know Java’s advantages.
To understand java virtual machine.
To distinguish the terms API, IDE, and JDK.
Show basic programming concepts
Identify the elements that make up a Java program
Use comments to improve the readability of programs.
Know how to edit, compile, and execute a program.
Write a simple java program.
Why Programming Languages?
3
Programming language is a mean of communication.
Purpose of programming language is to enable the user
to:
Enlist the help of a computer in solving problems.
Bridge the gap between problem and machine.
Difference between natural-language and human-
machine communication:
The lack of feedback in human-machine communication.
Humans are fault-tolerant in their communications.
Which Programming Language?
4
History
5
History (Cont.)
6
Sun Microsystems in1991 funded an internal corporate
research project (Green Project) led by James Gosling.
Java was officially announced in May 1995.
A key goal of Java is to write once, run anywhere
(WORA).
JDK Evolutions.
Why Java?
7
Java is a general purpose programming language.
Java is the internet programming language.
Java is a versatile programming language, you can use it
to develop applications for:
Desktop computers,
Servers, and
Small handheld devices.
Example: The software for Android cell phones is developed using
Java.
Examples of Java’s Versatility
8
Characteristics of Java
9
According to Sun’s Java White paper: Java is
Simple Secure
Object-oriented Architecture-neutral
Distributed Portable
Interpreted High-performance
Robust Multi-threaded and
Dynamic
Traditional Way of Compilation
10
Windows
compiler
Executable (Windows)
Mac OS
compiler
Computer
program
Executable (Mac)
UNIX
compiler
Executable (UNIX)
Compiling Java Source Code
11
Java was designed to run object programs on any platform.
Java programs (Source program) are compiled into a special type
of object code, known as bytecode.
The bytecode can then run on any computer with a Java Virtual
Machine.
A High Level View of Compiling/Running
Java Programs
12
First.java First.class
(javac)
class First { (Platform/Operating specific binary
public static void main (String[] 10100111000001000
args) 00100111001111001
{ System.out.println(“Hello!); }
: :
}
Java Editions
13
Java Standard Edition (Java SE)
Can be used to develop desktops and servers applications.
Java Enterprise Edition (Java EE)
Can be used for developing and running large, multi-
tiered, reliable, and secure enterprise applications that
are portable and scalable and that integrate easily with
applications and data.
Java Micro Edition (Java ME)
Can be used to develop applications for resource-
constrained devices like mobile devices, wireless
modules, etc…
Java programming
14
Three core packages used in Java programming:
Java Development Kit (JDK):
The JDK allows developers to create Java programs.
Java Runtime Environment (JRE):
It contains the Java class libraries, the Java class loader, and
the JVM.
It is responsible for orchestrating their activities.
Java Virtual Machine (JVM):
Allows Java programs to run on any device or operating
system.
Manages and optimize program memory.
Java programming Cont.
15
Application Program Interface (API):
Is a set of commands, functions, and protocols which
programmers can use when building software.
It allows programmers to use predefined functions, instead of
writing them from scratch.
Integrated Development Environment (IDE):
Is a software that provides a cohesive set of tools for
developing applications.
JDK is a package for developing Java-based software.
JRE is a package for running Java code.
The JRE can be used to run Java programs, but it's also part
of the JDK
Java Development Kit (JDK)
17
Versions
JDK 1.0 (January 23, 1996).
JDK 1.1 (February 19, 1997).
J2SE 1.2 (December 8, 1998).
J2SE 1.3 (May 8, 2000).
J2SE 1.4 (February 6, 2002).
J2EE 5.0 (September 30, 2004).
Java SE 6 (December 11, 2006).
Java SE 7 (July 28, 2011).
Java SE 8 (March 18, 2014).
Java SE 9 (2017).
.
.
.
Java SE 15 (2020)
.
.
.
Java 17 (14th September 2021)
Java IDE Tools
18
Borland Jbuilder.
NetBeans Open Source by Sun.
Eclipse Open Source by IBM.
Other tools:
TextPad Editor
JCreator LE
JGrasp
BlueJ
DrJava
A Simple Java Program
19
Create a java project in NetBeans IDE.
Give a name to the project (“FirstProgram” in this case).
Type in the following program in the class.
FirstProgram.java
/* *****************************************************
This is a Simple Java Program.
This Programs Prints “Hello University, First Stage”.
******************************************************* */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University, First Stage");
}
}
Netbeans IDE Description
20
Note that name of the file These indicators shows that
Run the program from here
and name of the class are there is an error in the
OR Right click>run file
the same program Description
of the error
Your project
will appear
hear
This is console screen.
The output of the
program is shown here
Anatomy of a Java Program
21
Comments
Reserved words
Modifiers
Classes
Methods and the main method
Statements
Blocks
Comments
22
Documentation for a single line
// Everything until the end of the line is a comment
Multi-line documentation
/* Every thing between these
delimiters is a
comment */
The compiler ignores all text between the delimiters.
Reserved Words
23
Java reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program.
abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while
Modifiers
24
Java uses certain reserved words called modifiers
Modifiers specify the properties of the data, methods,
and classes and how they can be used.
Examples of modifiers are:
public
static
private
final
abstract
protected
Classes
25
A program is defined by using one or more classes.
Class is the essential Java construct.
To program in Java, you must understand classes and
be able to write and use them.
Methods and main Method
26
A method is a series of statements.
Each class may contain any number of methods.
In our simple example ”FirstProgram.java” there is one
method named main .
Every Java application must have a user-declared main
method.
main method defines where the program execution begins.
The main method looks like this:
public static void main(String[] args) {
// Statements;
}
Statements
27
A statement represents an action or a sequence of
actions.
Each program may contain any number of statements.
System.out.println(“Hello, University, First
Stage”) is a statement.
Every statement in Java ends with a semicolon (;).
Blocks
28
A pair of braces in a program forms a block.
In Java, each block begins with an opening brace ({)
and ends with a closing brace (}).
Blocks can be nested, meaning that one block can be
placed within another, as shown in the following
code
class FirstProgram {
public static void main(String [] args) {
Method
System.out.println("Hello University, Block
First Stage");
Class
} Block
}
Trace FirstProgram.java Execution (1)
29
Enter the main method
/* *****************************************
This is a Simple Java Program.
This Programs Prints “Hello University, First Stage”.
************************************************** */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University, First Stage");
} // end method main
} // end class FirstProgram
Trace FirstProgram.java Execution (2)
30
Execute statement
/* *****************************************
This is a Simple Java Program.
This Programs Prints “Hello University, First Stage”.
************************************************** */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University, First Stage");
} // end method main
} // end class FirstProgram
Trace FirstProgram.java Execution (3)
31
Print the message “Hello University, First
Stage“ to the command line
/* *****************************************
This is a Simple Java Program.
This Programs Prints “Hello University, First Stage”.
************************************************** */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University, First Stage");
} // end method main
} // end class FirstProgram
Displaying Multiple Lines of Text (1)
32
Modify the FirstProgram.java program to display two
lines of text, using two statements.
/* *****************************************
This Program Modifies the Simple Java Program.
Uses more than one statement.
************************************************** */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University, First Stage");
System.out.println("Dept. of Computer Science");
}
}
Displaying Multiple Lines of Text (2)
33
Use a single statement output, to display multiple lines
of text
/* *****************************************
This program prints multiple lines of output, using one
statement.
************************************************** */
class FirstProgram {
public static void main(String [] args) {
System.out.println("Hello University\nFirst
Stage\nDept. of Computer Science");
}
}
Escape Sequences for Formatting
Escape Sequence Description
\n New line
\t Horizontal tab
\r Carriage return
\” Double quote
\\ Backslash
\b Backspace
Use escape sequences in println method.
Example:
System.out.println(“Hello,\tUniversity!”);
System.out.println(“Dept. of \”Computer Science\” “);
References
35
http://java.dzone.com/tips/ten-amazing-java-applications
www.cs.armstrong.edu/liang/intro6e/JavaCharacteristics.pdf
http://docs.oracle.com/en/java/
www.java.com/en/javahistory/index.jsp
http://introcs.cs.princeton.edu/java/home/
www.prenhall.com/liang
https://www.infoworld.com/article/3304858/what-is-the-jre-introduction-to-th
e-java-runtime-environment.html
Tutorial4us.com
Deitel & Deitel, “Java How to Program”, 9th ed., chapter 1, page 16.
Liang, “Introduction to Java Programming”, 10th ed., chapter 1, page 11.
Cay Horstmann, “Big Java”, 3rd ed., chapter 1, page 10.
Herbert Schildt, “Java A Beginner’s Guide: Create, Compile, and Run Java
Programs Today,” 5th Ed.