Chapter 1 Introduction to Computers, Programs, and Java PDF
Document Details
Uploaded by BriskTennessine
2020
Liang
Tags
Summary
This document is a chapter on introduction to Java programming. It discusses the features of Java, and the history of Java as a programming language.
Full Transcript
Chapter 1 Introduction to Computers, Programs, and Java Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved....
Chapter 1 Introduction to Computers, Programs, and Java Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 1 Objectives 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). To display output on the console (§1.7). 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). To explain the differences between syntax errors, runtime errors, and logic errors (§1.10). Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 2 Java, the Web, and Beyond (1.5) Java can be used to develop standalone applications (e.g., Desktop applications). Java can be used to develop applications running from a Web browser. Java can also be used to develop applications for hand-held devices (e.g., Android cell phones). Java can be used to develop applications for Web servers. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 3 Java’s History Developed by James Gosling and his team at Sun Microsystems Originally called Oak (supposedly after the oak tree outside Gosling’s office) Designed in 1991 for use in embedded chips in consumer electronic appliances In1995, renamed Java and redesigned for developing Web applications HotJava was the first Java-enabled Web browser Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 4 Companion Website 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 https://media.pearsoncmg.com/ph/esm/ecs_liang_ijp_12/cw/co ntent/supplements/JavaCharacteristics.pdf Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 5 Companion Website Characteristics of Java Java Is Simple Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java Is Object-Oriented Java as "C++--" because it is like C++ but Java Is Distributed with more functionality and fewer negative aspects. 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 Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 6 Companion Website Characteristics of Java Java Is Simple Java is inherently object-oriented. Although many object-oriented languages Java Is Object-Oriented began strictly as procedural languages, Java Is Distributed Java was designed from the start to be object-oriented. Object-oriented Java Is Interpreted programming (OOP) is a popular Java Is Robust programming approach that is replacing Java Is Secure traditional procedural programming techniques. Java Is Architecture-Neutral Java Is Portable One of the central issues in software development is how to reuse code. Java's Performance Object-oriented programming provides Java Is Multithreaded great flexibility, modularity, clarity, and reusability through encapsulation, Java Is Dynamic inheritance, and polymorphism. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 7 Companion Website Characteristics of Java Java Is Simple Distributed computing involves several computers working together on a network. Java Is Object-Oriented Java is designed to make distributed Java Is Distributed computing easy. Since networking capability is inherently integrated into Java Is Interpreted Java, writing network programs is like Java Is Robust sending and receiving data to and from a file. Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 8 Companion Website Characteristics of Java Java Is Simple You need an interpreter to run Java programs. The programs are compiled into Java Is Object-Oriented the Java Virtual Machine code called Java Is Distributed bytecode. The bytecode is machine- independent and can run on any machine Java Is Interpreted that has a Java interpreter, which is part of Java Is Robust the Java Virtual Machine (JVM). Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 9 Companion Website Characteristics of Java Java Is Simple Java compilers can detect many problems that would first show up at execution time Java Is Object-Oriented in other languages. Java Is Distributed Java has eliminated certain types of error- Java Is Interpreted prone programming constructs found in Java Is Robust other languages. Java Is Secure Java has a runtime exception-handling Java Is Architecture-Neutral feature to provide programming support Java Is Portable for robustness. Java's Performance Java Is Multithreaded Java Is Dynamic Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 10 Companion Website Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java implements several security Java Is Robust mechanisms to protect your system against Java Is Secure harm caused by stray programs. Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 11 Companion Website 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 Write once, run anywhere Java Is Portable With a Java Virtual Machine Java's Performance (JVM), you can write one Java Is Multithreaded program that will run on any platform. In other words, it is Java Is Dynamic “platform-independent.” Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 12 Companion Website 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 Because Java is architecture-neutral, Java programs are portable. They can Java's Performance be run on any platform without being Java Is Multithreaded recompiled. Java Is Dynamic Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 13 Companion Website 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 has improved significantly over the years with every Java's Performance new version. Java Is Multithreaded Just-In-Time (JIT) compilation and Java Is Dynamic optimization techniques have contributed to these performance improvements. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 14 Companion Website 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 Multithreading is a program’s capability to perform several tasks simultaneously. Java's Performance Multithread programming is smoothly Java Is Multithreaded integrated in Java, whereas in other Java Is Dynamic languages you have to call procedures specific to the operating system to enable multithreading. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 15 Companion Website 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 was designed to adapt to an evolving Java's Performance environment. New code can be loaded on Java Is Multithreaded the fly without recompilation. There is no need for developers to create, and for users Java Is Dynamic to install, major new software versions. New features can be incorporated transparently as needed. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 16 Java Language Specification, API, JDK, JRE, and IDE (1.6) Language Specification – A technical definition of the Java programming language’s syntax and semantics API – Application Program Interface, or library, that contains predefined classes and interfaces for developing Java programs JDK – The Java Development Kit is a set of programs for compiling, running, and testing Java programs JRE – The Java Runtime Environment is the program for running Java programs IDE – Integrated Development Environment for developing Java programs quickly Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 17 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 Java 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 … Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 18 JDK Editions Java Standard Edition (Java SE) – Java SE can be used to develop client-side standalone applications that run on the Desktop. Java Enterprise Edition (Java EE) – Java EE can be used to develop server-side applications such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces (JSF). Java Micro Edition (Java ME). – Java ME can be used to develop applications for mobile devices such as cell phones. This book uses Java SE 11 to introduce Java programming. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 19 Popular Java IDEs Apache NetBeans – https://netbeans.apache.org/download/index.html Eclipse – https://www.eclipse.org/downloads/ IntelliJ IDEA – https://www.jetbrains.com/community/education/#stud ents Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 20 A Simple Java Program (1.7) Listing 1.1 (Note: Every Java program must have at least one class.) // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Note: Clicking the green button displays the source code Welcome with interactive animation. You can also run the code in a browser. Internet connection is needed for this button. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 21 animation Trace a Program Execution Enter main method // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 22 animation Trace a Program Execution Execute statement // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 23 animation Trace a Program Execution // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } print a message to the console Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 24 Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words (also called keywords) Comments Blocks Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 25 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. // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 26 Main Method Line 2 of the program 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. // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 27 Statement A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!“. // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 28 Statement Terminator Every statement in Java ends with a semicolon (;). // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 29 Keywords 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 Java class. // 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 30 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) { Class block System.out.println("Welcome to Java!"); Method block } } Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 31 Special Symbols Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 32 Two More Simple Examples WelcomeWithThreeMessages ComputeExpression Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 33 Creating, Compiling, and Running Programs (1.8) Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 34 Compiling Java Source Code You can port a source program to any machine with appropriate compilers. The source program may have to be recompiled, however, if the object program can only run on a specific machine. (Note: This is the case for a language such as C++.) Nowadays computers are networked to work together. Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine (JVM), as shown below. The JVM is software that interprets Java bytecode. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 35 Programming Style and Documentation (1.9) Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 36 Appropriate Comments May 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. May include your name, class section, instructor, date, and a brief description at the beginning of the program. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 37 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 and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 38 Proper Indentation and Spacing Indentation – Indent at least two spaces. Spacing – Use a blank line to separate segments of the code. Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 39 Block Styles Either next-line style or end-of-line style for braces is acceptable, but it is best to be consistent. Next-line public class Test style { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 40 Programming Errors (1.10) Syntax Errors – Errors in code construction – Detected by the compiler Runtime Errors – Errors that cause a program to terminate abnormally Logic Errors – Errors that produce incorrect results because a program does not perform the way it was intended to – Can be hard to find Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 41 Syntax Errors public class ShowSyntaxErrors { public static main(String[] args) { System.out.println("Welcome to Java); } } ShowSyntaxErrors Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 42 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } } ShowRuntimeErrors Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 43 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); } } ShowLogicErrors Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, (c) 2020 Pearson Education, Inc. All rights reserved. 44