Date post: | 06-Jan-2018 |
Category: |
Documents |
Upload: | joanna-anthony |
View: | 215 times |
Download: | 0 times |
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.)
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: 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
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
• 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!
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
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
• 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)
Compiling and Running Java Programs
• A little different!• DOS command-line tools• Compiler: javac Case is significant!
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
public class HelloWorld
{ ...
javac HelloWorld.java
HelloWorld.class
HelloWorld.java
File of bytecodes
Running
HelloWorld.class
java HelloWorld
Run the interpreter
Or use an IDE
• JCreator