Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction
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
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
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?
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
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. bj4_01_04
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Schematic Diagram of a Computer
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 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
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. The ENIAC
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
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 Load the value 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 Machine Code
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Java Virtual Machine fetch sequence of numbers decode them execute the associated sequence of commands Java Virtual Machine (JVM)
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
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 … High-level Programming Language
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 Designed by James Gosling, Patrick Naughton and others in The Java Programming Language
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
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. VersionYearImportant New Features Inner classes Swing, Collections Performance enhancements Assertions, XML 52004Generic classes, enhanced for loop, auto-boxing, enumerations 62006Library improvements Small language changes and library improvments Java Versions Java was revised and extended many times
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Integrated Development Environment
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. HelloPrinter in an IDE
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
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.
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.
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(;)
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
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
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. bj4_syn_01_01
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
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
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( ); 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
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
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. The Compilation Process
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. The Edit-Compile-Test Loop
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
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved.
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