Java Programming Basics and IDEs
Java Programming Basics and IDEs
Objectives
Chapter 1: Introduction to ✦ To understand computer basics, programs, and operating systems (§§1.2–1.4).
Computers, Programs, and Java ✦ To describe the relationship between Java and the World Wide Web (§1.5).
✦ To understand the meaning of Java language specification, API, JDK, and IDE
(§1.6).
✦ To write a simple Java program (§1.7).
CS1: Java Programming ✦ To display output on the console (§1.7).
Colorado State University ✦ To explain the basic syntax of a Java program (§1.7).
✦ To create, compile, and run Java programs (§1.8).
✦ To use sound Java programming style and document programs properly (§1.9).
Original slides by Daniel Liang ✦ To explain the differences between syntax errors, runtime errors, and logic
errors (§1.10).
Modified slides by Chris Wilcox ✦ To develop Java programs using NetBeans (§1.11).
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
1 2
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
5 6
rights reserved. rights reserved.
1
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
7 8
rights reserved. rights reserved.
animation animation
// This program prints Welcome to Java! // This program prints Welcome to Java!
public class Welcome { public class Welcome {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!");
} }
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
9 10
rights reserved. rights reserved.
animation
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
11 12
rights reserved. rights reserved.
2
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
13 14
rights reserved. rights reserved.
// This program prints Welcome to Java! // This program prints Welcome to Java!
public class Welcome { public class Welcome {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!");
} }
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
15 16
rights reserved. rights reserved.
// This program prints Welcome to Java! // This program prints Welcome to Java!
public class Welcome { public class Welcome {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!");
} }
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
17 18
rights reserved. rights reserved.
3
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
19 20
rights reserved. rights reserved.
{ …} ( … )
// This program prints Welcome to Java! // This program prints Welcome to Java!
public class Welcome { public class Welcome {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!");
} }
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
21 22
rights reserved. rights reserved.
; // …
// This program prints Welcome to Java! // This program prints Welcome to Java!
public class Welcome { public class Welcome {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!");
} }
} }
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
23 24
rights reserved. rights reserved.
4
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
27 28
rights reserved. rights reserved.
✦ Spacing
public static void main(String[] args)
{
System.out.println("Block Styles");
– Use blank line to separate segments of the code. }
}
End-of-line
style
public class Test {
public static void main(String[] args) {
System.out.println("Block Styles");
}
}
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
29 30
rights reserved. rights reserved.
5
8/22/18
ShowSyntaxErrors Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
31 32
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
33 34
rights reserved. rights reserved.
Companion
Website Compiling and Running Java Displaying Text in a Message
from Eclipse Dialog Box
✦ See Supplement II.D on the Website for details you can use the showMessageDialog method in the
JOptionPane class. JOptionPane is one of the many
predefined classes in the Java system, which can be
reused rather than “reinventing the wheel.”
WelcomeInMessageDialogBox Run
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
35 36
rights reserved. rights reserved.
6
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
37
where x is a string for the text to be displayed.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
38
rights reserved. rights reserved.
Bus
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
39 40
rights reserved. rights reserved.
CPU Memory
The central processing unit (CPU) is the brain of a computer. It Memory is to store data and program instructions for CPU to
retrieves instructions from memory and executes them. The CPU execute. A memory unit is an ordered sequence of bytes, each holds
speed is measured in gigahertz (GHz), with 1 gigahertz equaling 1 eight bits. A program and its data must be placed in memory before
billion cycles per second. The speed of the CPU has been improved they can be executed. A memory byte is never empty, but it can be
continuously. If you buy a PC now, you might get an Intel Core i7 uninitialized. The current content of a memory byte is overwritten
running at 2.8 to 4.0 gigahertz. whenever new information is placed in it. If you buy a PC today, it
might have 8 gigabytes (Gb) of memory.
Bus Bus
e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor,
and Tape and NIC Mouse Printer and Tape and NIC Mouse Printer
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
41 42
rights reserved. rights reserved.
7
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
43 44
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
45 46
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
47 48
rights reserved. rights reserved.
8
8/22/18
Machine language is a set of primitive instructions Assembly languages were developed to make programming
built into every computer. The instructions are in easy. Since the computer cannot understand assembly
the form of binary code, so you have to enter binary language, however, a program called assembler is used to
convert assembly language programs into machine code.
codes for various instructions. Program with native For example, to add two numbers, you might write an
machine language is a tedious process. Moreover instruction in assembly code like this:
the programs are highly difficult to read and ADDF3 R1, R2, R3
modify. For example, to add two numbers, you
might write an instruction in binary like this:
1101101010011010
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
49 50
rights reserved. rights reserved.
The high-level languages are English-like and easy to learn Ada Named for Ada Lovelace, who worked on mechanical general-purpose computers. The Ada
language was developed for the Department of Defense and is us ed mainly in d efense projects.
and program. For example, the following is a high-level BASIC Beginner’s All-purpose Symbolic Instruction Code. It was d esigned to be learned and used easily
by beginners.
language statement (C, C++, Java, Python) that computes C Developed at Bell Laboratories. C combines the power of an ass embly language with the ease of
the area of a circle with radius 5: C++
use and portability of a high-level language.
C++ is an object-oriented language, based on C.
area = 5 * 5 * 3.1415; C# Pronounced “C Sh arp.” It is a hybrid of Java and C++ and was developed by Microsoft.
COBOL COmmon Business Oriented Language. Used for business applications.
FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications.
Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform -
independent Internet app lications.
Pascal Named for Blaise Pascal, wh o pioneered calculating machin es in the seventeenth century. It is a
simple, s tructured, general -purpose language p rimarily for teachin g programming.
Python A simple gen eral-purpose scripting language good for writing short programs .
Visual Visual Basic was developed by Microsoft and it enables the pr ogrammers to rapidly develop
Basic graphical user interfaces.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
51 52
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
53 54
rights reserved. rights reserved.
9
8/22/18
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
55 56
rights reserved. rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
57 58
rights reserved. rights reserved.
Companion
Java’s History Website Characteristics of Java
✦ Java Is Simple
✦ James Gosling and Sun Microsystems
✦ Java Is Object-Oriented
✦ Oak ✦ Java Is Distributed
✦ Java, May 20, 1995, Sun World ✦ Java Is Interpreted
✦ Java Is Robust
✦ HotJava ✦ Java Is Secure
– The first Java-enabled Web browser ✦ Java Is Architecture-Neutral
✦ Early History Website: ✦ Java Is Portable
✦ Java's Performance
http://www.java.com/en/javahistory/index.jsp ✦ Java Is Multithreaded
✦ Java Is Dynamic
www.cs.armstrong.edu/liang/JavaCharacteristics.pdf
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
59 60
rights reserved. rights reserved.
10
8/22/18
Companion Companion
Website Characteristics of Java Website Characteristics of Java
✦ Java Is Simple Java is partially modeled on C++, but greatly ✦ Java Is Simple Java is inherently object-oriented.
simplified and improved. Some people refer to Although many object-oriented languages
✦ Java Is Object-Oriented ✦ Java Is Object-Oriented
Java as "C++--" because it is like C++ but began strictly as procedural languages,
✦ Java Is Distributed with more functionality and fewer negative ✦ Java Is Distributed Java was designed from the start to be
aspects. object-oriented. Object-oriented
✦ Java Is Interpreted ✦ Java Is Interpreted
programming (OOP) is a popular
✦ Java Is Robust ✦ Java Is Robust programming approach that is replacing
traditional procedural programming
✦ Java Is Secure ✦ Java Is Secure
techniques.
✦ Java Is Architecture-Neutral ✦ Java Is Architecture-Neutral
✦ Java Is Portable ✦ Java Is Portable One of the central issues in software
development is how to reuse code. Object-
✦ Java's Performance ✦ Java's Performance oriented programming provides great
✦ Java Is Multithreaded ✦ Java Is Multithreaded flexibility, modularity, clarity, and
reusability through encapsulation,
✦ Java Is Dynamic ✦ Java Is Dynamic inheritance, and polymorphism.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
61 62
rights reserved. rights reserved.
Companion Companion
Website Characteristics of Java Website Characteristics of Java
✦ Java Is Simple Distributed computing involves several ✦ Java Is Simple You need an interpreter to run Java
computers working together on a network. programs. The programs are compiled into
✦ Java Is Object-Oriented ✦ Java Is Object-Oriented
Java is designed to make distributed the Java Virtual Machine code called
✦ Java Is Distributed computing easy. Since networking ✦ Java Is Distributed bytecode. The bytecode is machine-
capability is inherently integrated into independent and can run on any machine
✦ Java Is Interpreted ✦ Java Is Interpreted
Java, writing network programs is like that has a Java interpreter, which is part of
✦ Java Is Robust sending and receiving data to and from a ✦ Java Is Robust the Java Virtual Machine (JVM).
file.
✦ Java Is Secure ✦ Java Is Secure
✦ Java Is Architecture-Neutral ✦ Java Is Architecture-Neutral
✦ Java Is Portable ✦ Java Is Portable
✦ Java's Performance ✦ Java's Performance
✦ Java Is Multithreaded ✦ Java Is Multithreaded
✦ Java Is Dynamic ✦ Java Is Dynamic
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
63 64
rights reserved. rights reserved.
Companion Companion
Website Characteristics of Java Website Characteristics of Java
✦ Java Is Simple Java compilers can detect many problems ✦ Java Is Simple
that would first show up at execution time
✦ Java Is Object-Oriented ✦ Java Is Object-Oriented
in other languages.
✦ Java Is Distributed ✦ Java Is Distributed
Java has eliminated certain types of error-
✦ Java Is Interpreted ✦ Java Is Interpreted
prone programming constructs found in Java implements several security
✦ Java Is Robust other languages. ✦ Java Is Robust mechanisms to protect your system against
✦ Java Is Secure ✦ Java Is Secure harm caused by stray programs.
Java has a runtime exception-handling
✦ Java Is Architecture-Neutral feature to provide programming support ✦ Java Is Architecture-Neutral
✦ Java Is Portable for robustness. ✦ Java Is Portable
✦ Java's Performance ✦ Java's Performance
✦ Java Is Multithreaded ✦ Java Is Multithreaded
✦ Java Is Dynamic ✦ Java Is Dynamic
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
65 66
rights reserved. rights reserved.
11
8/22/18
Companion Companion
Website Characteristics of Java Website Characteristics of Java
✦ Java Is Simple ✦ Java Is Simple
✦ Java Is Object-Oriented ✦ Java Is Object-Oriented
✦ Java Is Distributed ✦ Java Is Distributed
✦ Java Is Interpreted ✦ Java Is Interpreted
✦ Java Is Robust ✦ Java Is Robust
✦ Java Is Secure ✦ Java Is Secure
✦ Java Is Architecture-Neutral Write once, run anywhere ✦ Java Is Architecture-Neutral
✦ Java Is Portable ✦ Java Is Portable Because Java is architecture neutral,
With a Java Virtual Machine (JVM),
Java programs are portable. They can
✦ Java's Performance you can write one program that will ✦ Java's Performance be run on any platform without being
run on any platform.
✦ Java Is Multithreaded ✦ Java Is Multithreaded recompiled.
✦ Java Is Dynamic ✦ Java Is Dynamic
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
67 68
rights reserved. rights reserved.
Companion Companion
Website Characteristics of Java Website Characteristics of Java
✦ Java Is Simple ✦ Java Is Simple
✦ Java Is Object-Oriented ✦ Java Is Object-Oriented
✦ Java Is Distributed ✦ Java Is Distributed
✦ Java Is Interpreted ✦ Java Is Interpreted
✦ Java Is Robust ✦ Java Is Robust
✦ Java Is Secure ✦ Java Is Secure
✦ Java Is Architecture-Neutral ✦ Java Is Architecture-Neutral
✦ Java Is Portable Java’s performance Because Java is ✦ Java Is Portable
architecture neutral, Java programs are
✦ Java's Performance portable. They can be run on any ✦ Java's Performance Multithread programming is smoothly
✦ Java Is Multithreaded platform without being recompiled. ✦ Java Is Multithreaded integrated in Java, whereas in other
languages you have to call procedures
✦ Java Is Dynamic ✦ Java Is Dynamic specific to the operating system to enable
multithreading.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
69 70
rights reserved. rights reserved.
Companion
Website Characteristics of Java JDK Versions
✦ Java Is Simple
✦ JDK 1.02 (1995)
✦ Java Is Object-Oriented
✦ Java Is Distributed
✦ JDK 1.1 (1996)
✦ Java Is Interpreted
✦ JDK 1.2 (1998)
✦ Java Is Robust ✦ JDK 1.3 (2000)
✦ Java Is Secure ✦ JDK 1.4 (2002)
✦ Java Is Architecture-Neutral ✦ JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
✦ Java Is Portable ✦ JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
Java was designed to adapt to an evolving
✦ Java's Performance environment. New code can be loaded on the ✦ JDK 1.7 (2011) a. k. a. JDK 7 or Java 7
✦ Java Is Multithreaded fly without recompilation. There is no need for
developers to create, and for users to install, ✦ JDK 1.8 (2014) a. k. a. JDK 8 or Java 8
✦ Java Is Dynamic major new software versions. New features can
be incorporated transparently as needed.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
71 72
rights reserved. rights reserved.
12