Advanced Java
Lecture 01
Samiullah Noori
Introduction to Java
Before programming, Computer!
Computer is an electronic device,
which can solve many problems
based on pre-defined instructions
given to it.
The history of digital computer
originated from late 1940s.
Parts of a computer
CPU: Central Processing Unit,
the most complicated part of a
computer
Storage devices: Primary stores
programs and data actively,
and Secondary memory stores
programs and data passively
IO devices: Interaction to/from
a computer. Some computers
are self contained units and
some computers interact with
devices via network
Computer Programs
Computer programs are also
referred to as software.
There are two categories of
software: Operating Systems
and Application software
Operating Systems software
refers to the a big program
that controls computer
hardware and manages their
processes
Application Software,
Application Software refers to
the programs that make
computers useful to the
users.
Programming Languages
Remember: A program is a
set of instruction that direct a
computer to certain set of
task(s)
Programming Languages are
a special kinds of languages
used to write computer
programs
What is a program?
Example: Let's consider about a program to calculate someone's‘ grosspay:
1 Display a message on the screen: "How many hours did you work?"
2 Allow the user to enter the number of hours worked
3 Once the user enters a number, store it in memory.
4 Display a message on the screen: "How much do you get paid per hour?"
5 Allow the user to enter an hourly pay rate.
6 Once the user enters a number, store it in memory.
7 Once both the number of hours worked and the hourly pay rate are
entered, multiply the
two numbers and store the result in memory.
8 Display a message on the screen that shows the amount of money earned.
The message must include the result of the calculation performed in Step 7.
Algorithms
The set of instructions in the previous slides can be referred to
as Algorithm
Algorithms is a set of step by step action plan for solving a
particular problem
The step by step means, in the previous slide, step 3 can not be
performed before step 2
Machine Language and Algorithms
Computers' CPU can only process instruction written in machine
languages.
A machine language is a stream of 0s and 1s that the CPU
recognizes.
How to write our algorithms in Machine Language?
Very tedious!!!
Programming languages use words instead of numbers to
represent tasks of an algorithm
What are below of the programs?
Application Software may consists of
hundreds of thousands to millions of line
codes that rely of sophisticated libraries
which helps the application work
On the other hand, on HW level simple
low level instruction can be executed
To execute complex application on the
top of low level machine instruction, we
have to go some layers
From a High-Level Language to the Language of Hardware
The machine language alphabets are 0 and 1
Computers are given instruction from this language
Human languages use symbols as their alphabet
So human invented a translator to translate symbolic program
notation to binary notation, which is called assembler
Languages, Compilers, Assemblers
High-level programming language, A portable language such as
C, Fortran, or Java com- posed of words and algebraic notation
that can be translated by a compiler into assembly language.
Compiler, A program that translates high-level language
statements into assembly language statements.
Assembler, A program that translates a symbolic version of
instructions into the binary version.
Languages, Compilers, Assemblers
Programming languages for different purposes
Applications and Program Database development:
development: MySQL
C SQL
C++ PostgreSql
C# Game development:
Tcl C
Java C#
Visual Basic C++
Artificial Intelligence development: DarkBASIC
python Java
Prolog
C
C++
Programming languages for different purposes
Computer drivers or other Internet and web page
hardware interface development: development:
C HTML
Assembly Java
Script development: Javascript
awk Perl
bash PHP
Perl Python
Python XML
Java Programming Language
High-level programming language
The language was designed to be simple, secure, and usable
for many different processor types
Java has a rich library that makes it possible to write portable
programs that can ignore proprietary operating systems
Java was designed for Internet application, so it is secure and
portable
Java Programming Language
Java is considered as the perfect language for the developers
and programmers to learn.
Currently it is the top-most programming language and has
grabbed the highest position with Android OS
Java can be utilized for mobile-based applications, enterprise
level purpose, for creating desktop applications, and for
establishing Android apps on tablets and smartphones.
Java Programming Language
The Java programing language was developed by Sun Microsystems
Programs written in Java, are cross platforms, it means they can be
run on different Operating Systems.
Programs written in Java, are compiled to byte codes, then the byte
codes are executed on Java Virtual Machine.
Programs written in Java, are two types:
(1) Java Application: A stand alone application run on your
desktop, such as word processor, DBMSs, spread sheets, etc.
(2) Java Applet: Small programs transmitted over the
Internet from web servers and executed by web browsers.
Compiler and JVM
The written program is called source code and saved with .java
extension.
The source code is compiled by the compiler. During compile,
many errors are detected. The java compiler translates the
source code into byte codes.
The byte codes are executed by Java Virtual Machine
Features of Java
Portability means that a
program written on one type
of language, then run on
wide variety of computers.
Because of byte codes, Java
is highly portable.
Any computer which has
JVM, Java program can be
run on it.
Some other features
Secure Interpreted
Object-oriented High performance
Robust Distributed
Multithreaded Imperative
Architecture-neutral
Versions of Java
Java has different versions
The first version was Java 1.0, then Java 1.1, then Java 1.2
Then Sun changed the name of Java into Java 2.
After the name change many versions came, 1.3, 1,4, 1.5
Now, latest version of Java is Java 22 or JDK 22
JDK and SDK
The software that we use to create a Java program is referred to as
JDK(Java Development Kit), or SDK(Software Development Kit).
Different versions exist:
J2SE (Java 2 Standard Edition)
J2EE (Java 2 Enterprise Edition)
J2ME (Java 2 Micro Edition)
IDE(Integrated Development Environment)
A program with environment, consists of text editor, debugger,
compiler, and other utilities, which facilitate the programmer to
write program in short time.
IDE examples include:
jGRASP
JetBrains
NetBeans,
Eclipse, etc.
Java Application and Applet
Two types of programs in java, Application and Applet
An Application is a stand alone program runs on a computer,
such as word processors, spread sheets, database managers,
etc.
An Applet is a small program that is transmitted over a web
server and is executed in Web browsers.
Web designers write java applets to perform operation that are
normally performed by an application and embedded into a
website
A simple Java program
// this is a simple Java program
public class Simple {
public static void main(String [ ] args) {
System.out.println(“ Programming is great fun! ”);
}
}
Special Characters
Variables and Literals
Variable: A name of a location in computers' main memory
Literals: Certain value in the program code
Variables must have data types and name. For example:
int a; (variable declaration)
a=12; (assignment)
int a=12; (initialization)
The print method
The print() and println() methods are used to display output to the
screen.
The print() method produces output to the screen in continuous
stream, and displays less-than-desirable result.
The println() method is produces more desirable results than the
print() method.
Common escape sequences
Primitive Data Types
There are different types of data in Java, and variables are
classified according to their data types
The programming process
Several steps including design, creation, testing, and debugging
activities
1) Clearly define what the program is to do.
2) Visualize the program running on the computer.
3) Use design tools to create a model of the program.
4) Check the model for logical errors.
5) Enter the code and compile it.
6) Correct any errors found during compilation. Repeat Steps 5 and 6
as many times as necessary.
7) Run the program with test data for input.
8) Correct any runtime errors found while running the program.
Repeat Steps 5 through 8 as many times as necessary.
9) Validate the results of the program.
Classes
A class is a piece of code that specifies a particular type of object.
It is a "blue-print" for creating an object.
It also specifies the "data" and "action" that an object can hold and
perform respectively.
Each object is created from a class is called the "instance" of that
class.
Classes
Objects and Classes
An object exists in memory, and performs a specific task
Objects have two general capabilities:
Storing data, infields
Performing operations, with methods
When a program needs the services of a particular type of
object, it creates that object in memory, and then calls that
object's methods as necessary.
Access Specifier
An access specifier is a Java keyword that indicates how a field or
method can be accessed
Public: When the public access specifier is applied to a class
member, the member can be accessed by code inside the class or
outside
Private: When the private access specifier is applied to a class
member, the member cannot be accessed by code outside the class.
The member can be accessed only by methods that are members of
the same class.
Protected: When the protected access specifier is applied to a class
member, The member is accessible within package and outside the
package but through inheritance only.
default: If you don't use any specifier, it is treated as default by
default. The default specifier is accessible only within package.
What is a method
Methods are commonly used to break a problem down into
small manageable pieces
This is called divide and conquer rule
Methods simplify programs
If a specific task is performed in several places in the program, a
method can be written once to perform that task, and then be
executed anytime it is needed
This is known as code reuse
What are methods returning?
Value-returning and void methods
A void method is one that simply performs a task and then
terminates
System.out.println("Hello World!");
A value-returning method not only performs a task, but also
sends a value back to the code that called it
double number = Math.pow(6,2);
Passing Arguments to a Method
Passing Multiple Arguments
Denfining a Value-Returning Method
Calling a Value-Returning Method
Constructors
Classes can have special methods called constructors
A constructor is a method that is automatically called when an
object is created
Constructors are used to perform operations at the time an
object is Created
Constructors typically initialize instance fields and perform other
object initialization tasks
Constructors have a few special properties that set them apart
from normal methods:
Constructors have the same name as the class.
Constructors have no return type (not even void)
Constructors may not return any values
Constructors are typically public
Constructor for Simple Class
// constructor from class with name Simple
public Simple(double a, double b) {
variable1 = a;
variable2 =b;
}
The Default Constructor
When an object is created, its constructor is always called
If you do not write a constructor, Java provides one when the
class is compiled
The constructor that Java provides is known as the default
constructor
It sets all of the object's numeric fields to 0
It sets all of the object's boolean fields to false
It sets all of the object's reference variables to the special value
null
Overloading Methods and Constructors
Two or more methods in a class may have the same name as long
as their parameter lists are different
When this occurs, it is called method overloading. This also applies
to constructors
Public int add(int num1, int num2){
int sum = num1+num2;
return sum;
}
Public String add(String str1, String str2){
String combined = str1+str2;
return combined;
}
End of chapter 1
Question?