Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction."— Presentation transcript:

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

2 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 Chapter Goals

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

4 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 What Is Programming?

5 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 The Anatomy of a Computer

6 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 The Anatomy of a Computer

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

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

9 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. The first usable electronic computer designed by John Mauchly and J. Presper Eckert of the University of Pennsylvania Completed by 1946 Contained about 18000 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 The ENIAC

10 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 The ENIAC

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

12 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 Translating Human-Readable programs to Machine Code

13 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Java Virtual Machine (JVM) – a typical sequence of machine instructions is: 1.Load the contents of memory location 40. 2.Load the value 100. 3.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: 21 40 16 100 163 240 Compiler translates high-level language to machine code Machine Code

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

15 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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. Communicate the command sequence to the computer

16 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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 21 40 16 100 163 240 … High-level Programming Language

17 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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. The Java Programming Language

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

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

20 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. VersionYearImportant New Features 1.01996 1.11997Inner classes 1.21998Swing, Collections 1.32000Performance enhancements 1.42002Assertions, XML 52004Generic classes, enhanced for loop, auto-boxing, enumerations 62006Library improvements 102010Small language changes and library improvments Java Versions Java was revised and extended many times

21 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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

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

23 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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! ch01/hello/HelloPrinter.java

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

25 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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! HelloPrinter.java Start a new class 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 HelloPrinter.java comment

26 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. main method 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.

27 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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.

28 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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: } HelloPrinter.java Start a new class Body of main method Statement inside the curly brackets are executed one by one. Each statement ends in a semicolon(;)

29 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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: } System.out 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

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

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

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

33 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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!"); Self Check

34 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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. Self Check 1.13

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

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

37 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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. Self Check 1.15

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

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

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

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

42 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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). Source files and Class files

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 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. 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


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

Similar presentations


Ads by Google