Introduction to Java
Programming
Instructor: Dr. Fahed Jubair
Computer System
Application Software: Users express their instructions using
Web browser, Word processing, etc high-level programming languages
Compilers: translate high-level
System Software: programs into low-level programs
Compilers and Operating Systems
Operating systems: provide services
during program execution
Hardware: Capable of performing instructions
CPU, Memory, I/O expressed using low-level languages
(i.e., 1010100101010100101…)
@ All rights reserved.
Computer Program
• Instructions + data
instructions data
allocate
int x ;
write x
x = 10 ;
read
…=x;
@ All rights reserved.
Program Lifecycle
• Compile-time: a high-level program is translated into a low-level
program (aka object code) by a compiler
• Run-time: the executable file is executed by a computer
@ All rights reserved.
Program Execution
• Executable file is initially stored on disk
• An oversimplified picture of program execution:
• The operating system loads the executable file from disk to memory
in preparation for execution
• The operating system creates a process and adds it to the processes’
queue
• When its turn, a process is executed on a processer
• During execution, the operating system acts as a medium
between hardware and application software
@ All rights reserved.
Programming Language
• Describe a set of rules for users to express their commands to the
computer in high-level human language
• Examples: C, C++, Java, Python, C#, Javascript, and many more
• A programming language typically has two main components at its
core: a compiler & a runtime system
• Compilers translate high-level programs into assembly languages
• Runtime system: collection of library routines that provide services to
programs during their executions
• Examples: I/O services, memory allocation, interrupts, etc
• Runtime libraries make calls to the operating system to provide services
@ All rights reserved.
Compilers vs Interpreters
compile execute
Hello.cpp Hello.out output
Hello.py interpret
output
What about Java?
@ All rights reserved.
Java
• At compile time, Java compiler translates the source program into a
“virtual” machine code called bytecode
§ Unlike “native” machine codes produced by traditional compilers, bytecode is
machine-independent, i.e., designed for an abstract (i.e., virtual) machine
• At execution time, Java Virtual Machine (JVM) has an interpreter that
executes bytecode programs
@ All rights reserved.
Why Interpreters?
• Advantage of using interpreters in Java: codes run everywhere!
§ Compile on one machine and execute on any other machine
§ Bytecode is designed to favor compactness
§ Making it popular with web application
• Disadvantage of Java: virtual codes are slower than traditional native
machine codes
@ All rights reserved.
Java Features
• General-purpose language with many features
• Object-oriented
• Java comes in the following editions:
• Java Standard Edition (Java SE): used for stand-alone, client-sided applications
• Java Enterprise Edition (Java EE): used for distributed, server-side applications
• Java Micro Edition (Java ME): used for mobile application
© All rights reserved.
Java Development Kit (JDK)
• Each Java edition comes with its own JDK, a set of programs (and
libraries) that represent the edition’s implementation
• JDK has many components, some of which are:
• javac: the java compiler, which translates source code into bytecode
• java: the java loader and interpreter, which executes bytecode
• jar: the java achiever, which is used to create java libraries
• jdb: the java debugger
• javadoc: the java documentation generator
• jre: the java runtime environment, which consists of JVM, JIT compiler, and
many runtime libraries that provide services during execution
• Note: Java runtime environment can be installed alone
© All rights reserved.
Recent Java Versions
• Java SE 6 (December, 2006)
• Java SE 7 (July, 2011)
• Java SE 8 (March, 2014) We will be using this version
• Java SE 9 (September, 2017)
•…
• Java SE 16 (March, 2021)
© All rights reserved.
Java Hello Program
• Let us start with a simple hello program, Hello.java
Java is class-based language
The method where the execution always starts
Java built-in mechanism for printing
messages on console
© All rights reserved.
Compiling and Running Java Programs
• Compilation command
javac Hello.java // produces bytecode file called Hello.class
• Execution command
java Hello // shows output on console
Let us do a demo
© All rights reserved.
Programming Errors
• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result
@ All rights reserved.
Syntax Errors
Error 1: “void” is missing
Error 2: no semicolon
@ All rights reserved.
Runtime Errors
// output
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.main(Demo.java:3)
@ All rights reserved.
Logic Errors
// output
Celsius 35 is Fahrenheit degree 67
But 35 Celsius is equal to 95 in Fahrenheit ?!
@ All rights reserved.
Integrated Development Environment (IDE)
• IDEs are software development tools where programmers can
edit their codes, compile and test them
§ Immediate feedback concerning syntax or logical problems as the code
being developed
• IDE examples:
§ Eclipse
§ NetBeans
§ MS Visual Studio
§ IntelliJ IDEA
@ All rights reserved.