CSc 201 - Intro to Java

Download Report

Transcript CSc 201 - Intro to Java

CSc 201
Introduction to Java
George Wells
Room 007, Hamilton Building
Email: [email protected]
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
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
Introduction to Java
Chapter 1: Getting Started
• What is Java?
– Recent programming language (1995)
– Based on C++
– Great for teaching (and learning!)
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
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!
History (cont.)
• Performance at last: Java 1.3 (May 2000)
– Not Java 3!
• Oracle buys Sun Microsystems (2010)
• Current version
– Java 1.6 (also called Java 6!)
– Standard Edition (SE)
– also Micro and Enterprise Editions (ME and EE)
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
Python Equivalent!
print "Hello, world\n"
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
Looking at the program:
The main method
/* Comment 1:
The
program’s
Written by
George
Wells entry
-- 6 point
January 2011 */
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello, world\n");
// Comment 2
} // main
} // class HelloWorld
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 HelloWorld
A single statement
Statements
• Terminated by semicolons:
System.out.println("Hello, world\n");
• Free format
– Examples:
a = b
+ c
+ d;
One statement
Four
statements
Use one statement
per line!
a = 1; b = 2; c = a * b + 3; d = 4;
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");
Braces
// Comment 2
} // main
} // class HelloWorld
A Compound Statement
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
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
Input and Output
• Output:
– System.out is an output stream (connected
to the screen by default)
– println is a method to display information
• Input:
– System.in is an input stream (connected to
the keyboard by default)
– More complicated! (wait until Chapter 10)
Compiling and Running Java
Programs
• A little different!
• DOS command-line tools
Case is significant!
• Compiler: javac
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
Compiling
HelloWorld.java
public class HelloWorld
{ ...
javac HelloWorld.java
File of bytecodes
HelloWorld.class
Running
HelloWorld.class
Run the
interpreter
java HelloWorld
Or use an IDE
• JCreator