Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter One: Introduction

Similar presentations


Presentation on theme: "Chapter One: Introduction"— Presentation transcript:

1 Chapter One: Introduction
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

2 To understand the activity of programming
Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code and high level programming languages To become familiar with your computing environment and your compiler To compile and run your first Java program To recognize syntax and logic errors Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

3 Computer savvy (file management, text editing) Problem solving skills
Prerequisites Computer savvy (file management, text editing) Problem solving skills Time management High school basic math No prior programming background required Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

4 Computers are programmed to perform tasks
What Is Programming? Computers are programmed to perform tasks Different tasks = different programs Program Sequence of basic operations executed in succession Contains instruction sequences for all tasks it can execute Sophisticated programs require teams of highly skilled programmers and other professionals Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

5 The Anatomy of a Computer
Central processing unit Chip Transistors Storage Primary storage: Random-access memory (RAM) Secondary storage: e.g. hard disk Removable storage devices: e.g.: floppy disks, tapes, CDs Peripherals Executes very simple instructions Executes instructions very rapidly General purpose device Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

6 The Anatomy of a Computer
Central processing unit Chip Transistors Storage Primary storage: Random-access memory (RAM) Secondary storage: e.g. hard disk Removable storage devices: e.g.: floppy disks, tapes, CDs Peripherals Executes very simple instructions Executes instructions very rapidly General purpose device Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

7 bj4_01_04 Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

8 Schematic Diagram of a Computer
                                                                     Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

9 The first usable electronic computer
The ENIAC The first usable electronic computer designed by John Mauchly and J. Presper Eckert of the University of Pennsylvania Completed by 1946 Contained about vaccuum tubes in many cabinates Weighed more than 27 tons was roughly 2.4 m × 0.9 m × 30 m Consumed 150 kW of power Vaccuum tubes burned out at the rate of several tubes per day Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

10 Work on the ENIAC was supported by U.S. Navy
Computation of ballistic table give the trajectory of a projectile Depend on the wind resistance initial velocity atmospheric condition 1950’s the word computer referred to people who did that kind of work the ENIAC was later used for peaceful purposes such as tabulator of U.S. census data Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

11 The ENIAC Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

12 Translating Human-Readable programs to Machine Code
the processor executes machine instructions CPU from different vendors have different sets of machine instructions. Java programs contain machine instructions for a so-called “Java virtual machine” (JVM) that being enable Java applications to run on different CPUs without modification Java virtual machine is an idealized CPU that is simulated by a program run on the actual CPU Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

13 Machine instructions are encoded as numbers: 21 40 16 100 163 240
Machine Code Java Virtual Machine (JVM) – a typical sequence of machine instructions is: Load the contents of memory location 40. Load the value 100. If the first value is greater than the second value, continue with the instruction that is stored in memory location 240. Machine instructions are encoded as numbers: Compiler translates high-level language to machine code Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

14 Java Virtual Machine (JVM)
fetch sequence of numbers 21 40 decode them execute the associated sequence of commands Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

15 Communicate the command sequence to the computer
The most direct method is to place the actual numbers into computer memory It is tedious and error-prone to look up the numeric codes for thousands command and manually place the code into memory. Computer are good at automating tedious and error-prone activities. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

16 High-level Programming Language
Began to appear in the mid-1950s Programmer expresses the idea behind the task that needs to be performed Compiler translates the high-level description into machine instructions for a particular processor For example Java instruction If (intRate > 100) System.out.println(“Interest rate error”); Translate into Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

17 The Java Programming Language
Originaly named Oak Developed as a part of the Green project at the Sun Microsystems for use in consumer devices in 1991. Designed by James Gosling, Patrick Naughton and others in 1994. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

18 The Java Programming Language
Simple Safe Platform-independent ("write once, run anywhere") Rich library (packages) Designed for the internet Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

19 that contains Java code, the code automaticlly running.
Applets on a Web Page Visit web page that contains Java code, the code automaticlly running. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

20 Java Versions Java was revised and extended many times Version Year
Important New Features JDK 1.0 1996 JDK 1.1 1997 Inner classes J2SE 1.2 1998 Swing, Collections J2SE 1.3 2000 Performance enhancements J2SE 1.4 2002 Assertions, XML J2SE 5 2004 Generic classes, enhanced for loop, auto-boxing, enumerations JAVA SE 6 2006 Library improvements JAVA SE 7 2011 JAVA SE 8 2014 JAVA SE 9 2017 JAVA SE 10 2018 Small language changes and library improvments Java was revised and extended many times Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

21 You cannot hope to learn all of Java in one term
Java library You cannot hope to learn all of Java in one term Java itself is relative simple but Java has a vast library with support for Graphics User interface design Cryptography Networking Sound Database storage Many other purposes Expert Java programmers just use some parts that they need to particular projects Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

22 An Integrated Development Environment
An Integrated Development Environment Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

23 ch01/hello/HelloPrinter.java Output: Hello, World!
1: public class HelloPrinter 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: } Output: Hello, World! Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

24 HelloPrinter in an IDE Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

25 Classes are a fundamental concept in Java.
HelloPrinter.java HelloPrinter.java Start a new class 1: public class HelloPrinter 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: } comment Output: Hello, World! Classes are a fundamental concept in Java. In Java, every program consists of one or more classes The word “public” denotes that the class is usable by the public In Java, every source file can contain at one public class and the name of the public class must match the name of the file containing the class Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

26 main method The above construction declares a method called main
public static void main(String[] args) { …. } The above construction declares a method called main A method contains a collection of programming instructions Every Java application must have a main method. Most Java programs contain other methods beside main. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

27 The above construction declares a method called main
ch01/hello/HelloPrinter.java public static void main(String[] args) { …. } The above construction declares a method called main A method contains a collection of programming instructions Every Java application must have a main method. Most Java programs contain other methods beside main. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

28 HelloPrinter.java Body of main method
Start a new class 1: public class HelloPrinter 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: } Body of main method Statement inside the curly brackets are executed one by one. Each statement ends in a semicolon(;) Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

29 System.out 1: public class HelloPrinter 2: { 3: public static void main(String[] args) 4: { 5: // Display a greeting in the console window 6: 7: System.out.println("Hello, World!"); 8: } 9: } The console window is represented in Java by an object called System.out. An object is an entity that you manipulate in your program In Java, each object belongs to a class The class declares methods that specify what you can do with the objects System.out object belongs to PrintStream class PrintStream class has a method called println for printing a line of text. called Syste.out Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

30 A Simple Program public class ClassName
public static void main(String[] args) // comment Method call System class System.out object println method Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

31 To invoke a method of an object and supply any additional parameters.
Syntax 1.1 Method Call object.methodName(parameters) Example: System.out.println("Hello, Dave!"); Purpose: To invoke a method of an object and supply any additional parameters. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

32 bj4_syn_01_01 Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

33 Answer: System.out.println("Hello,"); System.out.println("World!");
Self Check How would you modify the HelloPrinter program to print the words "Hello," and "World!" on two lines? Answer: System.out.println("Hello,"); System.out.println("World!"); Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

34 Self Check 1.13 Would the program continue to work if you omitted the line starting with //? Answer: Yes – the line starting with // is a comment, intended for human readers. The compiler ignores comments. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

35 Answer: The printout is
Self Check 1.14 What does the following set of statements print? System.out.print("My lucky number is"); System.out.println( ); Answer: The printout is My lucky number is12 It would be a good idea to add a space after the is. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

36 Detected by the compiler Logic errors System.out.print("Hell");
Syntax errors System.ouch.print(". . ."); System.out.print("Hello); Detected by the compiler Logic errors System.out.print("Hell"); Detected (hopefully) through testing Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

37 Self Check 1.15 Suppose you omit the // characters from the HelloPrinter.java program but not the remainder of the comment. Will you get a compile-time error or a run-time error? Answer: A compile-time error. The compiler will not know what to do with the word Display. Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

38 The Compilation Process
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

39 Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

40 bytecodes bytecodes Class loader Execution engine Java virtual machine
program class files API class files bytecodes bytecodes Class loader Execution engine Java virtual machine Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

41 The Edit-Compile-Test Loop
   The Edit-Compile-Test Loop Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

42 Source files and Class files
The source file of a Java class is stored in a file: ClassName.java Java source files are essentially text files and can be opened in any text editor The compiled file of a Java class is stored in a file: ClassName.class Java class files are binary files (not human-readable). Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.

43 Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

44 Big Java by Cay Horstmann
Copyright © 2008 by John Wiley & Sons. All rights reserved.

45 Install Java and Eclipse Eclipse IDE:
Homework Obtain a Course Book Install Java and Eclipse Eclipse IDE: Read the following eclipse tutorials Help -> Help contents -> Workbench User Guide Basic tutorial thru Deleting Resources Java Development User Guide Basic tutorial thru Running Your Program Don’t worry about understanding details of JUnit sample project Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.


Download ppt "Chapter One: Introduction"

Similar presentations


Ads by Google