SlideShare a Scribd company logo
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
1
Chapter 1
Introduction to Computers,
Programs, Object Oriented
Programming and Java
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
2
How Data is Stored?
Data of various kinds, such as numbers,
characters, and strings, are encoded as a
series of bits (zeros and ones). Computers
use zeros and ones because digital devices
have two stable states, which are referred to
as zero and one by convention. The
programmers need not to be concerned about
the encoding and decoding of data, which is
performed automatically by the system
based on the encoding scheme. The
encoding scheme varies. For example,
character ‘J’ is represented by 01001010 in
one byte. A small number such as three can
be stored in a single byte. If computer needs
to store a large number that cannot fit into a
single byte, it uses a number of adjacent
bytes. No two data can share or split a same
byte. A byte is the minimum storage unit.
.
.
.
2000
2001
2002
2003
2004
.
.
.
01001010
01100001
01110110
01100001
00000011
Memory content
Memory address
Encoding for character ‘J’
Encoding for character ‘a’
Encoding for character ‘v’
Encoding for character ‘a’
Encoding for number 3
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
3
Programming Languages
Machine Language Assembly Language High-Level Language
Machine language is a set of primitive instructions
built into every computer. The instructions are in the
form of binary code, so you have to enter binary
codes for various instructions. Program with native
machine language is a tedious process. Moreover
the programs are highly difficult to read and modify.
For example, to add two numbers, you might write
an instruction in binary like this:
1101101010011010
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
4
Programming Languages
Machine Language Assembly Language High-Level Language
Assembly languages were developed to make
programming easy. Since the computer cannot understand
assembly language, however, a program called assembler is
used to convert assembly language programs into machine
code. For example, to add two numbers, you might write an
instruction in assembly code like this:
ADDF3 R1, R2, R3
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
5
Programming Languages
Machine Language Assembly Language High-Level Language
The high-level languages are English-like and easy to learn
and program. For example, the following is a high-level
language statement that computes the area of a circle with
radius 5:
area = 5 * 5 * 3.1415;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
6
Popular High-Level Languages
Language Description
Ada
BASIC
C
C++
C#
COBOL
FORTRAN
Java
Pascal
Python
Visual
Basic
Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is used mainly in defense projects.
Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily
by beginners.
Developed at Bell Laboratories. C combines the power of an assembly language with the ease of
use and portability of a high-level language.
C++ is an object-oriented language, based on C.
Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COmmon Business Oriented Language. Used for business applications.
FORmula TRANslation. Popular for scientific and mathematical applications.
Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform-
independent Internet applications.
Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a
simple, structured, general-purpose language primarily for teaching programming.
A simple general-purpose scripting language good for writing short programs.
Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop
graphical user interfaces.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
7
Compiling Source Code in C
 A program written in a high-level language is
called a source program or source code. Because
a computer cannot understand a source program,
a source program must be translated into machine
code for execution.
 To run the source code of a C program, we need a
C compiler. The C compiler generates assembly
code (.s file) first. The assembler produces the
machine code (.obj file). Finally, the linker
produces the executable machine code (.exe file)
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
8
Compiling/Interpreting Source Code in Java
 To run the source code of a Java program, we
need a Java compiler and a Java interpreter. The
Java compiler compiles source files (.java) to
generate bytecode (.class) files. We can use the
javac command provided by Sun in the terminal
window.
 Note that a statement from the source code may
be translated into several instructions in .class
file.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
9
Interpreting Source Code
 Although the bytecode is similar to machine level codes,
it is not directly executable.
 The Java Virtual Machine (JVM) is used to load .class
files into the memory and interpret the code. The
Execution Engine or Just-In-Time compiler (JIT)
(executable name: java) reads one statement from the
bytecode in memory, translates it to the machine code of
the target platform, and then executes it right away.
 JVM helps to avoid the need to recompile the source
code for every specific platform.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
10
Major Differences Between Java and C
 No goto statement in Java
 No pointers in Java
 No unsigned data type
 No call-by-reference
 16 bit characters
 OOP rather than procedural
 long integers use 8 bytes
 Additional primitive types such as “byte”
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
11
Why Java?
The answer is that Java enables users to develop and
deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of
computing is being profoundly influenced by the Internet,
and Java promises to remain a big part of that future.
Java is a general purpose programming language.
Java is an internet programming language.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
12
Java, Web, and Beyond
 Java can be used to develop standalone
applications.
 Java can be used to develop applications
running from a browser.
 Java can also be used to develop applications
for hand-held devices.
 Java can be used to develop applications for
Web servers.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
13
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Java is partially modeled on C++, but greatly
simplified and improved. Some people refer to
Java as "C++--" because it is like C++ but
with more functionality and fewer negative
aspects.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
14
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Java is inherently object-oriented.
Although many object-oriented languages
began strictly as procedural languages,
Java was designed from the start to be
object-oriented. Object-oriented
programming (OOP) is a popular
programming approach that is replacing
traditional procedural programming
techniques.
One of the central issues in software
development is how to reuse code. Object-
oriented programming provides great
flexibility, modularity, clarity, and
reusability through encapsulation,
inheritance, and polymorphism.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
15
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Distributed computing involves several
computers working together on a network.
Java is designed to make distributed
computing easy. Since networking
capability is inherently integrated into
Java, writing network programs is like
sending and receiving data to and from a
file.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
16
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
You need an interpreter to run Java
programs. The programs are compiled into
the Java Virtual Machine code called
bytecode. The bytecode is machine-
independent and can run on any machine
that has a Java interpreter, which is part of
the Java Virtual Machine (JVM).
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
17
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Java compilers can detect many problems
that would first show up at execution time
in other languages.
Java has eliminated certain types of error-
prone programming constructs found in
other languages.
Java has a runtime exception-handling
feature to provide programming support
for robustness.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
18
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Java implements several security
mechanisms to protect your system against
harm caused by stray programs.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
19
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Write once, run anywhere
With a Java Virtual Machine (JVM),
you can write one program that will
run on any platform.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
20
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Because Java is architecture neutral,
Java programs are portable. They can
be run on any platform without being
recompiled.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
21
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Multithread programming is smoothly
integrated in Java, whereas in other
languages you have to call procedures
specific to the operating system to enable
multithreading.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
22
Characteristics of Java
 Java Is Simple
 Java Is Object-Oriented
 Java Is Distributed
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic
Java was designed to adapt to an evolving
environment. New code can be loaded on the
fly without recompilation. There is no need for
developers to create, and for users to install,
major new software versions. New features can
be incorporated transparently as needed.
Companion
Website
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
23
Application Interface
 The application program interface (API), also known
as library, contains predefined classes and interfaces
for developing Java programs
 The Java Development Toolkit (JDK) consists of a set
of separate programs, each invoked from a command
line, for developing and testing Java programs.
Instead of using the JDK, you can use a Java
development tool (e.g., NetBeans, Eclipse, and
TextPad)—software that provides an integrated
development environment (IDE) for developing Java
programs quickly
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
24
JDK Versions
 JDK 1.02 (1995)
 JDK 1.1 (1996)
 JDK 1.2 (1998)
 JDK 1.3 (2000)
 JDK 1.4 (2002)
 JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
 JDK 1.7 (2011) a. k. a. JDK 7 or Java 7
 JDK 1.8 (2014) a. k. a. JDK 8 or Java 8
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
25
JDK Editions
 Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.
 Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets, Java ServerPages, and Java
ServerFaces.
 Java Micro Edition (J2ME).
– J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java programming.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
26
Popular Java IDEs
 NetBeans
 Eclipse
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
27
A Simple Java Program
 Every Java program must have at least one class. Each class
has a name. By convention, class names start with an
uppercase letter.
 In a program, multiple classes may cooperate with each other.
 Each program needs an entry point to start execution.
 The program is executed from the main method that is defined
in one of the classes. A class may contain several methods. The
main method is the entry point where the program begins
execution.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
28
Creating, Compiling, and
Running Programs
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
29
Compiling Java Source Code
 The Java language is a high-level language, but Java bytecode is a
low-level language. The bytecode is similar to machine instructions
but is architecture neutral and can run on any platform that has a
Java Virtual Machine (JVM).
 The virtual machine is a program that interprets Java bytecode. This
is one of Java’s primary advantages: Java bytecode can run on a
variety of hardware platforms and operating systems.
 Java source code is compiled into Java bytecode (.class file) and
Java bytecode is interpreted by the JVM. Your Java code may use
the code in the Java library. To execute a Java program is to run the
program’s bytecode.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
30
Compiling Java Source Code
 You can execute the bytecode on any platform with a JVM, which is
an interpreter and resides in RAM. It translates the individual
instructions in the bytecode into the target machine (i.e. the machine
on which it is working at) language code one at a time rather than
the whole program as a single unit.
 Each step is executed immediately after it is translated.
 The bytecode can then run on any computer with a Java Virtual
Machine, as shown below. In fact, Java Virtual Machine is a
software that interprets Java bytecode.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
31
Compiling Java Source Code
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
32
Procedural versus Object-Oriented
Programming
 The procedural programming aims at solving problems using a
collection of variables and functions. This approach is not
manageable as the size of the code gets large.
 Object-oriented programming (OOP) is an alternative approach
where variables and functions are combined to form classes.
 The procedural paradigm focuses on designing methods. The
object-oriented paradigm couples data and methods together
into objects. Software design using the object-oriented
paradigm focuses on objects and operations on objects.
 Classes provide more flexibility and modularity for building
reusable software.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
33
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
34
Procedural versus Object-Oriented
Programming
 In procedural programming, data and operations on the data are
separate, and this methodology requires passing data to methods.
 Object-oriented programming places data and the operations that
pertain to them in an object. This approach solves many of the
problems inherent in procedural programming. The object-
oriented programming approach organizes programs in a way
that mirrors the real world, in which all objects are associated
with both attributes and activities.
 Using objects improves software reusability and makes
programs easier to develop and easier to maintain. A Java
program can be viewed as a collection of cooperating objects.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
35
Object-Oriented Programming Paradigm
 There are many different types of objects like trees, cars,
animals etc. around us
 These objects have different characteristics even if they belong
to the same type. For instance, different animals may have
different numbers of legs, height, size, color etc. Similarly,
different cars may have different sizes, colors, brand names etc.
 In object oriented programming, objects of the same type are
said to belong to the same class. In technical terms, a class is a
blueprint or template or structure that can be used to create
objects.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
36
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
37
Object-Oriented Programming Paradigm
 All objects have two basic characteristics:
– Properties OR states
– Behavior
 Example:
– Each car has a state or a set of properties such as model, color, year, price
etc. (i.e. what each object has)
– Each car has a set of behaviors such as start, move, stop, turn, accelerate,
park etc. (i.e. what each object does)
 Conceptually, software objects are similar to the real world ones
 A software object has a set of properties represented using data
fields/variables. The behaviors of the objects are represented in
terms of methods.
 In OOP, objects are instances of a predefined class.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
38
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
39
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Enter main method
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
40
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
Execute statement
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
41
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Trace a Program Execution
print a message to the
console
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
42
Anatomy of a Java Program
 Class name
 Main method
 Statements
 Statement terminator
 Reserved words
 Comments
 Blocks
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
43
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class Name
Every Java program must have at least one class.
Each class has a name. By convention, class names
start with an uppercase letter. In this example, the
class name is Welcome.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
44
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Main Method
Line 2 defines the main method. In order to run a
class, the class must contain a method named main.
The program is executed from the main method.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
45
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement
A statement represents an action or a sequence of actions.
The statement System.out.println("Welcome to Java!") in
the program below is a statement to display the greeting
"Welcome to Java!”.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
46
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Statement Terminator
Every statement in Java ends with a semicolon (;).
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
47
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Reserved words
Reserved words or keywords are words that have a
specific meaning to the compiler and cannot be used for
other purposes in the program. For example, when the
compiler sees the word class, it understands that the word
after class is the name for the class.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
48
Blocks
A pair of braces in a program forms a block that groups
components of a program.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
49
Special Symbols
Character Name Description
{}
()
[]
//
" "
;
Opening and closing
braces
Opening and closing
parentheses
Opening and closing
brackets
Double slashes
Opening and closing
quotation marks
Semicolon
Denotes a block to enclose statements.
Used with methods.
Denotes an array.
Precedes a comment line.
Enclosing a string (i.e., sequence of characters).
Marks the end of a statement.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
50
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
{ … }
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
51
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
( … )
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
52
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
;
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
53
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
// …
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
54
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
" … "
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
55
Programming Style and
Documentation
 Appropriate Comments
 Naming Conventions
 Proper Indentation and Spacing Lines
 Block Styles
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
56
Appropriate Comments
 Include a summary at the beginning of the program to
explain what the program does, its key features, its
supporting data structures, and any unique techniques it
uses.
 In addition to line comments (beginning with //) and
block comments (beginning with /*), Java supports
comments of a special type, referred to as javadoc
comment (begin with /** and end with */). They can be
extracted into an HTML file using the JDK’s javadoc
command. Use javadoc comments (/** ... */) for
commenting on an entire class or an entire method.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
57
Naming Conventions
 Choose meaningful and descriptive names.
 Class names:
– Capitalize the first letter of each word in the
name. For example, the class name
ComputeExpression.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
58
Proper Indentation and Spacing
 A consistent indentation style makes
programs clear and easy to read, debug, and
maintain. Indentation is used to illustrate the
structural relationships between a program’s
components or statements.
 Spacing
– Use blank line to separate segments of the code.
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
59
Block Styles
 There are two popular styles, next-line style and end-of-line style, as shown
below.
public class Test
{
public static void main(String[] args)
{
System.out.println("Block Styles");
}
}
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
End-of-line
style
Next-line
style
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
60
Programming Errors
 Syntax Errors
– Detected by the compiler
 Runtime Errors
– Causes the program to abort
 Logic Errors
– Produces incorrect result
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
61
Syntax Errors
public class ShowSyntaxErrors {
public static main(String[] args) {
System.out.println("Welcome to Java);
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
62
Runtime Errors
public class ShowRuntimeErrors {
public static void main(String[] args) {
System.out.println(1 / 0);
}
}
Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd.
All rights reserved.
63
Logic Errors
public class ShowLogicErrors {
public static void main(String[] args) {
System.out.println("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5) * 35 + 32);
}
}
Ad

Recommended

01slidejhljgjhghgjhkghjgjhghjgjhffffg.ppt
01slidejhljgjhghgjhkghjgjhghjgjhffffg.ppt
raniaelkotby1
 
CSE215_Module_01_Introduction.ppt
CSE215_Module_01_Introduction.ppt
RashedurRahman18
 
Java vs C sharp Top 8 Important Differences To Know.pdf
Java vs C sharp Top 8 Important Differences To Know.pdf
calltutors
 
Java
Java
Harsha Madushanka
 
130700548484460000
130700548484460000
Tanzeel Ahmad
 
introduction to computers PROGRAMS jva.ppt
introduction to computers PROGRAMS jva.ppt
iloveyoukika
 
Chapter 1 introduction to Computers programs, and JAVA
Chapter 1 introduction to Computers programs, and JAVA
AhsirYu
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
01_Java_Programming_Lecture-01_FCIT.pptx
01_Java_Programming_Lecture-01_FCIT.pptx
adlbdalrhmn47
 
Java Programming (M&M)
Java Programming (M&M)
mafffffe19
 
Introduction to computer programs and java
Introduction to computer programs and java
honeymacagubang
 
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
caijjournal
 
python.pptx
python.pptx
RRamyaDevi
 
microemulsions microemulsions microemulsions microemulsions
microemulsions microemulsions microemulsions microemulsions
AhmadHashlamon
 
Comparative Study of programming Languages
Comparative Study of programming Languages
Ishan Monga
 
Which is better, Java or Python? And how?
Which is better, Java or Python? And how?
narendrachinnu
 
Cr java concept by vikas jagtap
Cr java concept by vikas jagtap
Vikas Jagtap
 
15 Top reasons to choose Java for Backend Development
15 Top reasons to choose Java for Backend Development
Your Team in India
 
Features of java unit 1
Features of java unit 1
RubaNagarajan
 
Chapter-1-1 object oriented programing pdf.pdf
Chapter-1-1 object oriented programing pdf.pdf
megbde32
 
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
AmanGunner
 
1_Introduction to Java.pptx java programming
1_Introduction to Java.pptx java programming
amitraj53904
 
Top Programming Languages of 2020
Top Programming Languages of 2020
Ikbal Ahmed
 
Chapter 1 introduction to java technology
Chapter 1 introduction to java technology
sshhzap
 
Best Programming Languages to Learn This Year
Best Programming Languages to Learn This Year
Eyeglass Repair USA
 
Java report by ravi raja
Java report by ravi raja
RaviRaja55
 
Java Vs. Python - Which One to Choose In 2023 (1).pdf
Java Vs. Python - Which One to Choose In 2023 (1).pdf
TriState Technology
 
Lecture1-Algorithms-and-Flowchart-ppt.ppt
Lecture1-Algorithms-and-Flowchart-ppt.ppt
samreen82
 
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
samreen82
 

More Related Content

Similar to introduction to python programming language.ppt (20)

Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
01_Java_Programming_Lecture-01_FCIT.pptx
01_Java_Programming_Lecture-01_FCIT.pptx
adlbdalrhmn47
 
Java Programming (M&M)
Java Programming (M&M)
mafffffe19
 
Introduction to computer programs and java
Introduction to computer programs and java
honeymacagubang
 
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
caijjournal
 
python.pptx
python.pptx
RRamyaDevi
 
microemulsions microemulsions microemulsions microemulsions
microemulsions microemulsions microemulsions microemulsions
AhmadHashlamon
 
Comparative Study of programming Languages
Comparative Study of programming Languages
Ishan Monga
 
Which is better, Java or Python? And how?
Which is better, Java or Python? And how?
narendrachinnu
 
Cr java concept by vikas jagtap
Cr java concept by vikas jagtap
Vikas Jagtap
 
15 Top reasons to choose Java for Backend Development
15 Top reasons to choose Java for Backend Development
Your Team in India
 
Features of java unit 1
Features of java unit 1
RubaNagarajan
 
Chapter-1-1 object oriented programing pdf.pdf
Chapter-1-1 object oriented programing pdf.pdf
megbde32
 
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
AmanGunner
 
1_Introduction to Java.pptx java programming
1_Introduction to Java.pptx java programming
amitraj53904
 
Top Programming Languages of 2020
Top Programming Languages of 2020
Ikbal Ahmed
 
Chapter 1 introduction to java technology
Chapter 1 introduction to java technology
sshhzap
 
Best Programming Languages to Learn This Year
Best Programming Languages to Learn This Year
Eyeglass Repair USA
 
Java report by ravi raja
Java report by ravi raja
RaviRaja55
 
Java Vs. Python - Which One to Choose In 2023 (1).pdf
Java Vs. Python - Which One to Choose In 2023 (1).pdf
TriState Technology
 
Java Semimar Slide (Cetpa)
Java Semimar Slide (Cetpa)
Pratima Parida
 
01_Java_Programming_Lecture-01_FCIT.pptx
01_Java_Programming_Lecture-01_FCIT.pptx
adlbdalrhmn47
 
Java Programming (M&M)
Java Programming (M&M)
mafffffe19
 
Introduction to computer programs and java
Introduction to computer programs and java
honeymacagubang
 
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
JPT : A SIMPLE JAVA-PYTHON TRANSLATOR
caijjournal
 
microemulsions microemulsions microemulsions microemulsions
microemulsions microemulsions microemulsions microemulsions
AhmadHashlamon
 
Comparative Study of programming Languages
Comparative Study of programming Languages
Ishan Monga
 
Which is better, Java or Python? And how?
Which is better, Java or Python? And how?
narendrachinnu
 
Cr java concept by vikas jagtap
Cr java concept by vikas jagtap
Vikas Jagtap
 
15 Top reasons to choose Java for Backend Development
15 Top reasons to choose Java for Backend Development
Your Team in India
 
Features of java unit 1
Features of java unit 1
RubaNagarajan
 
Chapter-1-1 object oriented programing pdf.pdf
Chapter-1-1 object oriented programing pdf.pdf
megbde32
 
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
ICT-DBA4 -05-0811-Apply-Object-Oriented-Programming-Language-Skills.doc
AmanGunner
 
1_Introduction to Java.pptx java programming
1_Introduction to Java.pptx java programming
amitraj53904
 
Top Programming Languages of 2020
Top Programming Languages of 2020
Ikbal Ahmed
 
Chapter 1 introduction to java technology
Chapter 1 introduction to java technology
sshhzap
 
Best Programming Languages to Learn This Year
Best Programming Languages to Learn This Year
Eyeglass Repair USA
 
Java report by ravi raja
Java report by ravi raja
RaviRaja55
 
Java Vs. Python - Which One to Choose In 2023 (1).pdf
Java Vs. Python - Which One to Choose In 2023 (1).pdf
TriState Technology
 

More from samreen82 (7)

Lecture1-Algorithms-and-Flowchart-ppt.ppt
Lecture1-Algorithms-and-Flowchart-ppt.ppt
samreen82
 
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
samreen82
 
Computer system and organization types of memory
Computer system and organization types of memory
samreen82
 
System software computer software types of software
System software computer software types of software
samreen82
 
crystalReport.pptx
crystalReport.pptx
samreen82
 
Chapter 1.pptx
Chapter 1.pptx
samreen82
 
UNIT1IT SKILLS.pptx
UNIT1IT SKILLS.pptx
samreen82
 
Lecture1-Algorithms-and-Flowchart-ppt.ppt
Lecture1-Algorithms-and-Flowchart-ppt.ppt
samreen82
 
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
COMPUTER_ORGANIZATION AND COMPUTER MEMORY.ppt
samreen82
 
Computer system and organization types of memory
Computer system and organization types of memory
samreen82
 
System software computer software types of software
System software computer software types of software
samreen82
 
crystalReport.pptx
crystalReport.pptx
samreen82
 
Chapter 1.pptx
Chapter 1.pptx
samreen82
 
UNIT1IT SKILLS.pptx
UNIT1IT SKILLS.pptx
samreen82
 
Ad

Recently uploaded (20)

Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Cadastral Maps
Cadastral Maps
Google
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Cadastral Maps
Cadastral Maps
Google
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
Ad

introduction to python programming language.ppt

  • 1. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs, Object Oriented Programming and Java
  • 2. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 2 How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. The encoding scheme varies. For example, character ‘J’ is represented by 01001010 in one byte. A small number such as three can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split a same byte. A byte is the minimum storage unit. . . . 2000 2001 2002 2003 2004 . . . 01001010 01100001 01110110 01100001 00000011 Memory content Memory address Encoding for character ‘J’ Encoding for character ‘a’ Encoding for character ‘v’ Encoding for character ‘a’ Encoding for number 3
  • 3. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 3 Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Program with native machine language is a tedious process. Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010
  • 4. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 4 Programming Languages Machine Language Assembly Language High-Level Language Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3
  • 5. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 5 Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. For example, the following is a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415;
  • 6. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 6 Popular High-Level Languages Language Description Ada BASIC C C++ C# COBOL FORTRAN Java Pascal Python Visual Basic Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada language was developed for the Department of Defense and is used mainly in defense projects. Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners. Developed at Bell Laboratories. C combines the power of an assembly language with the ease of use and portability of a high-level language. C++ is an object-oriented language, based on C. Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft. COmmon Business Oriented Language. Used for business applications. FORmula TRANslation. Popular for scientific and mathematical applications. Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform- independent Internet applications. Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a simple, structured, general-purpose language primarily for teaching programming. A simple general-purpose scripting language good for writing short programs. Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop graphical user interfaces.
  • 7. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 7 Compiling Source Code in C  A program written in a high-level language is called a source program or source code. Because a computer cannot understand a source program, a source program must be translated into machine code for execution.  To run the source code of a C program, we need a C compiler. The C compiler generates assembly code (.s file) first. The assembler produces the machine code (.obj file). Finally, the linker produces the executable machine code (.exe file)
  • 8. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 8 Compiling/Interpreting Source Code in Java  To run the source code of a Java program, we need a Java compiler and a Java interpreter. The Java compiler compiles source files (.java) to generate bytecode (.class) files. We can use the javac command provided by Sun in the terminal window.  Note that a statement from the source code may be translated into several instructions in .class file.
  • 9. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 9 Interpreting Source Code  Although the bytecode is similar to machine level codes, it is not directly executable.  The Java Virtual Machine (JVM) is used to load .class files into the memory and interpret the code. The Execution Engine or Just-In-Time compiler (JIT) (executable name: java) reads one statement from the bytecode in memory, translates it to the machine code of the target platform, and then executes it right away.  JVM helps to avoid the need to recompile the source code for every specific platform.
  • 10. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 10 Major Differences Between Java and C  No goto statement in Java  No pointers in Java  No unsigned data type  No call-by-reference  16 bit characters  OOP rather than procedural  long integers use 8 bytes  Additional primitive types such as “byte”
  • 11. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 11 Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Java is a general purpose programming language. Java is an internet programming language.
  • 12. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 12 Java, Web, and Beyond  Java can be used to develop standalone applications.  Java can be used to develop applications running from a browser.  Java can also be used to develop applications for hand-held devices.  Java can be used to develop applications for Web servers.
  • 13. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 13 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects. Companion Website
  • 14. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 14 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Java is inherently object-oriented. Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. Object-oriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. One of the central issues in software development is how to reuse code. Object- oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism. Companion Website
  • 15. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 15 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy. Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file. Companion Website
  • 16. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 16 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine code called bytecode. The bytecode is machine- independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine (JVM). Companion Website
  • 17. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 17 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Java compilers can detect many problems that would first show up at execution time in other languages. Java has eliminated certain types of error- prone programming constructs found in other languages. Java has a runtime exception-handling feature to provide programming support for robustness. Companion Website
  • 18. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 18 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Java implements several security mechanisms to protect your system against harm caused by stray programs. Companion Website
  • 19. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 19 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Write once, run anywhere With a Java Virtual Machine (JVM), you can write one program that will run on any platform. Companion Website
  • 20. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 20 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled. Companion Website
  • 21. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 21 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Multithread programming is smoothly integrated in Java, whereas in other languages you have to call procedures specific to the operating system to enable multithreading. Companion Website
  • 22. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 22 Characteristics of Java  Java Is Simple  Java Is Object-Oriented  Java Is Distributed  Java Is Interpreted  Java Is Robust  Java Is Secure  Java Is Architecture-Neutral  Java Is Portable  Java's Performance  Java Is Multithreaded  Java Is Dynamic Java was designed to adapt to an evolving environment. New code can be loaded on the fly without recompilation. There is no need for developers to create, and for users to install, major new software versions. New features can be incorporated transparently as needed. Companion Website
  • 23. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 23 Application Interface  The application program interface (API), also known as library, contains predefined classes and interfaces for developing Java programs  The Java Development Toolkit (JDK) consists of a set of separate programs, each invoked from a command line, for developing and testing Java programs. Instead of using the JDK, you can use a Java development tool (e.g., NetBeans, Eclipse, and TextPad)—software that provides an integrated development environment (IDE) for developing Java programs quickly
  • 24. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 24 JDK Versions  JDK 1.02 (1995)  JDK 1.1 (1996)  JDK 1.2 (1998)  JDK 1.3 (2000)  JDK 1.4 (2002)  JDK 1.5 (2004) a. k. a. JDK 5 or Java 5  JDK 1.6 (2006) a. k. a. JDK 6 or Java 6  JDK 1.7 (2011) a. k. a. JDK 7 or Java 7  JDK 1.8 (2014) a. k. a. JDK 8 or Java 8
  • 25. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 25 JDK Editions  Java Standard Edition (J2SE) – J2SE can be used to develop client-side standalone applications or applets.  Java Enterprise Edition (J2EE) – J2EE can be used to develop server-side applications such as Java servlets, Java ServerPages, and Java ServerFaces.  Java Micro Edition (J2ME). – J2ME can be used to develop applications for mobile devices such as cell phones. This book uses J2SE to introduce Java programming.
  • 26. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 26 Popular Java IDEs  NetBeans  Eclipse
  • 27. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 27 A Simple Java Program  Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter.  In a program, multiple classes may cooperate with each other.  Each program needs an entry point to start execution.  The program is executed from the main method that is defined in one of the classes. A class may contain several methods. The main method is the entry point where the program begins execution.
  • 28. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 28 Creating, Compiling, and Running Programs
  • 29. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 29 Compiling Java Source Code  The Java language is a high-level language, but Java bytecode is a low-level language. The bytecode is similar to machine instructions but is architecture neutral and can run on any platform that has a Java Virtual Machine (JVM).  The virtual machine is a program that interprets Java bytecode. This is one of Java’s primary advantages: Java bytecode can run on a variety of hardware platforms and operating systems.  Java source code is compiled into Java bytecode (.class file) and Java bytecode is interpreted by the JVM. Your Java code may use the code in the Java library. To execute a Java program is to run the program’s bytecode.
  • 30. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 30 Compiling Java Source Code  You can execute the bytecode on any platform with a JVM, which is an interpreter and resides in RAM. It translates the individual instructions in the bytecode into the target machine (i.e. the machine on which it is working at) language code one at a time rather than the whole program as a single unit.  Each step is executed immediately after it is translated.  The bytecode can then run on any computer with a Java Virtual Machine, as shown below. In fact, Java Virtual Machine is a software that interprets Java bytecode.
  • 31. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 31 Compiling Java Source Code
  • 32. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 32 Procedural versus Object-Oriented Programming  The procedural programming aims at solving problems using a collection of variables and functions. This approach is not manageable as the size of the code gets large.  Object-oriented programming (OOP) is an alternative approach where variables and functions are combined to form classes.  The procedural paradigm focuses on designing methods. The object-oriented paradigm couples data and methods together into objects. Software design using the object-oriented paradigm focuses on objects and operations on objects.  Classes provide more flexibility and modularity for building reusable software.
  • 33. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 33
  • 34. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 34 Procedural versus Object-Oriented Programming  In procedural programming, data and operations on the data are separate, and this methodology requires passing data to methods.  Object-oriented programming places data and the operations that pertain to them in an object. This approach solves many of the problems inherent in procedural programming. The object- oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities.  Using objects improves software reusability and makes programs easier to develop and easier to maintain. A Java program can be viewed as a collection of cooperating objects.
  • 35. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 35 Object-Oriented Programming Paradigm  There are many different types of objects like trees, cars, animals etc. around us  These objects have different characteristics even if they belong to the same type. For instance, different animals may have different numbers of legs, height, size, color etc. Similarly, different cars may have different sizes, colors, brand names etc.  In object oriented programming, objects of the same type are said to belong to the same class. In technical terms, a class is a blueprint or template or structure that can be used to create objects.
  • 36. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 36
  • 37. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 37 Object-Oriented Programming Paradigm  All objects have two basic characteristics: – Properties OR states – Behavior  Example: – Each car has a state or a set of properties such as model, color, year, price etc. (i.e. what each object has) – Each car has a set of behaviors such as start, move, stop, turn, accelerate, park etc. (i.e. what each object does)  Conceptually, software objects are similar to the real world ones  A software object has a set of properties represented using data fields/variables. The behaviors of the objects are represented in terms of methods.  In OOP, objects are instances of a predefined class.
  • 38. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 38
  • 39. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 39 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution Enter main method
  • 40. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 40 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution Execute statement
  • 41. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 41 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Trace a Program Execution print a message to the console
  • 42. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 42 Anatomy of a Java Program  Class name  Main method  Statements  Statement terminator  Reserved words  Comments  Blocks
  • 43. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 43 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Class Name Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this example, the class name is Welcome.
  • 44. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 44 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Main Method Line 2 defines the main method. In order to run a class, the class must contain a method named main. The program is executed from the main method.
  • 45. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 45 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Statement A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program below is a statement to display the greeting "Welcome to Java!”.
  • 46. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 46 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Statement Terminator Every statement in Java ends with a semicolon (;).
  • 47. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 47 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Reserved words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class.
  • 48. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 48 Blocks A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Class block Method block
  • 49. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 49 Special Symbols Character Name Description {} () [] // " " ; Opening and closing braces Opening and closing parentheses Opening and closing brackets Double slashes Opening and closing quotation marks Semicolon Denotes a block to enclose statements. Used with methods. Denotes an array. Precedes a comment line. Enclosing a string (i.e., sequence of characters). Marks the end of a statement.
  • 50. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 50 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } { … }
  • 51. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 51 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } ( … )
  • 52. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 52 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } ;
  • 53. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 53 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } // …
  • 54. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 54 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } " … "
  • 55. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 55 Programming Style and Documentation  Appropriate Comments  Naming Conventions  Proper Indentation and Spacing Lines  Block Styles
  • 56. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 56 Appropriate Comments  Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.  In addition to line comments (beginning with //) and block comments (beginning with /*), Java supports comments of a special type, referred to as javadoc comment (begin with /** and end with */). They can be extracted into an HTML file using the JDK’s javadoc command. Use javadoc comments (/** ... */) for commenting on an entire class or an entire method.
  • 57. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 57 Naming Conventions  Choose meaningful and descriptive names.  Class names: – Capitalize the first letter of each word in the name. For example, the class name ComputeExpression.
  • 58. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 58 Proper Indentation and Spacing  A consistent indentation style makes programs clear and easy to read, debug, and maintain. Indentation is used to illustrate the structural relationships between a program’s components or statements.  Spacing – Use blank line to separate segments of the code.
  • 59. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 59 Block Styles  There are two popular styles, next-line style and end-of-line style, as shown below. public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style Next-line style
  • 60. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 60 Programming Errors  Syntax Errors – Detected by the compiler  Runtime Errors – Causes the program to abort  Logic Errors – Produces incorrect result
  • 61. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 61 Syntax Errors public class ShowSyntaxErrors { public static main(String[] args) { System.out.println("Welcome to Java); } }
  • 62. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 62 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } }
  • 63. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved. 63 Logic Errors public class ShowLogicErrors { public static void main(String[] args) { System.out.println("Celsius 35 is Fahrenheit degree "); System.out.println((9 / 5) * 35 + 32); } }