24
CSc 201 Introduction to Java George Wells Room 007, Hamilton Building Email: [email protected]

CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Embed Size (px)

DESCRIPTION

Notice (cont.) Any changes to your curriculum can only be done after discussion with the Dean –cannot change on ROSS or at Eden Grove If the Dean detects a problem with your curriculum, he will you to see him –It is essential that you respond to that as quickly as possible so we can correct the error

Citation preview

Page 1: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

CSc 201 Introduction to Java

George Wells

Room 007, Hamilton BuildingEmail: [email protected]

Page 2: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Notice from Dean of Science

• Thank you to students for the way you prepared for curriculum approval– went very smoothly for the science faculty

• HOWEVER, some science students have not yet registered their subjects– These students MUST complete their

curriculum approval in the Dean’s office TODAY

Page 3: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Notice (cont.)

• Any changes to your curriculum can only be done after discussion with the Dean– cannot change on ROSS or at Eden Grove

• If the Dean detects a problem with your curriculum, he will email you to see him– It is essential that you respond to that email as

quickly as possible so we can correct the error

Page 4: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Introduction to Java

Page 5: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Chapter 1: Getting Started

• What is Java?

– Recent programming language (1995)

– Based on C++

– Great for teaching (and learning!)

Page 6: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

History 101

• 1991: The Green Project– Intelligent “set-top boxes”– Oak

• 1993: The Internet/World Wide Web

• 23 May 1995: Java 1.0– Sun and Netscape

Page 7: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

History (cont.)

• Quick fixes!– Java 1.1– Development of libraries and language– 1.1.7

• Stability at last!– Java 1.2 (November 1998)– Integrated the library development– Marketed as Java 2!

Page 8: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

History (cont.)

Page 9: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Our First Java Program

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorld

Page 10: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Python Equivalent!

print "Hello, world\n"

Page 11: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Basics

• Case sensitivity:– String is not the same as string– MAIN is not the same as main

• Java keywords are all lower case– e.g. public class static void

Page 12: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Looking at the program:The main method

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorld

The program’s entry point

Page 13: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Looking at the program:Statements

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorldA single statement

Page 14: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

• Terminated by semicolons:

• Free format– Examples:

Statements

System.out.println("Hello, world\n");

One statementa = b + c+ d;

a = 1; b = 2; c = a * b + 3; d = 4;

Four statementsUse one statement

per line!

Page 15: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Looking at the program:Grouping parts of the program

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorld

Braces

A Compound Statement

Page 16: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Looking at the program:Comments

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorld

Page 17: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Looking at the program:Strings and Escape Sequences

/* Comment 1: Written by George Wells -- 6 January 2011 */

public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, world\n");

// Comment 2 } // main

} // class HelloWorld Escape Sequence

Page 18: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

• Output:– System.out is an output stream (connected

to the screen by default)– println is a method to display information

Input and Output

• Input:– System.in is an input stream (connected to

the keyboard by default)– More complicated! (wait until Chapter 10)

Page 19: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Compiling and Running Java Programs

• A little different!• DOS command-line tools• Compiler: javac Case is significant!

Page 20: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Compiling (cont.)

• The compiler produces a .class file– Bytecode (not machine code)– Allows portability (Java programs can run on

almost any hardware and operating system)

• Needs an interpreter– The Java Virtual Machine (JVM)– A program that “executes” bytecode

Page 21: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Compiling

public class HelloWorld

{ ...

javac HelloWorld.java

HelloWorld.class

HelloWorld.java

File of bytecodes

Page 22: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Running

HelloWorld.class

java HelloWorld

Run the interpreter

Page 23: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building

Or use an IDE

• JCreator

Page 24: CSc 201 Introduction to Java George Wells Room 007, Hamilton Building