1. Introduction to Java
OOSSE Programming with Java
Lecture 1
Dec 21, 2012 OOSSE - Java Lecture 1 1
2. Objectives
In this lecture, we will:
• Introduce Java and the Java Virtual Machine
• Discuss the basic object oriented concepts
• Define classes and instances
• Review the structure and syntax of a Java program
• Introduce the Scanner class and simple I/O
Dec 21, 2012 OOSSE - Java Lecture 1 2
3. Introduction to Java
• Java is a high level object oriented programming language
• Java was designed to be:
– Simple
– Object oriented
– Distributed
– Robust
– Secure
– Architecture-Neutral
– Portable
– Multithreaded
Dec 21, 2012 OOSSE - Java Lecture 1 3
4. A Simple Java Application
// A simple Hello World Java Application
public class Hello
{
public static void main (String[] args)
{
System.out.println ("Hello World");
System.out.println ("Welcome to Java");
}
}
Dec 21, 2012 OOSSE - Java Lecture 1 4
5. The Java Source File
• A Java program can be developed using any simple editor to
generate the Java source file
• For example Notepad could be used
• The source file is simple text but must be saved with an
extension of java
Dec 21, 2012 OOSSE - Java Lecture 1 5
6. Compiling a Java Application
• The Java source file is in a suitable format for humans to
read but must be compiled into bytecode before it can be
used
• Sun provide a compiler called javac that is used to
compile the source code into bytecode
• The compiler can be invoked from a command prompt in a
windows environment
– javac Hello.java
• The result of a successful compilation is a class file
containing the bytecode
– Hello.class
Dec 21, 2012 OOSSE - Java Lecture 1 6
7. Executing a Java Application
• The bytecode can be executed on the Java Virtual Machine
using the interpreter provided by Sun: java Hello
• Note the bytecode is held in a file called Hello.class but the
extension class is not included in the call to the interpeter:
java Hello
Dec 21, 2012 OOSSE - Java Lecture 1 7
9. The Java Virtual Machine
• The bytecode produced by the Java compiler is not
targeted at a specific machine
– It is targeted at the Java Virtual Machine
• The Java Runtime Environment executes the virtual
machine
– The Java Bytecode is executed on the virtual machine
• Hence the bytecode is portable
– It will execute on any machine that is running the Java
Virtual Machine
– The JVM needs to be target specific
Dec 21, 2012 OOSSE - Java Lecture 1 9
10. Pitfalls
• Java is case sensitive
– Hello and hello are not the same
• All Java programs must have the java extension
• The extension is specified when the compiler is used
– javac Hello.java
• The bytecode produced by the compiler is held in a file
with a .class extension but the extension is NOT specified
when the interpreter is used
– java Hello
Dec 21, 2012 OOSSE - Java Lecture 1 10
11. Java and Classes
• Java is an object oriented programming language
• All code is wrapped in the form of a class:
public class Hello
{
…
}
• Note the keywords public and class
• The class is given a name, Hello in this case, and must be
saved in a file called Hello.java
• Code similar to this will appear in each of your
applications
– The name of the class will change
Dec 21, 2012 OOSSE - Java Lecture 1 11
12. The Method main
• Classes use methods to specify what can be done
• A main method is required in a Java application and
defines where the application begins
• The structure of main is fixed and will be the same in all
applications:
public static void main (String[] args)
{
…
}
• The syntax will be discussed in detail later
– For now please just accept that it must be as it is
Dec 21, 2012 OOSSE - Java Lecture 1 12
13. The Body of main
• The code that you write to specify what the application
should do makes up the body of main
• A block of code; that is one or more program statements
wrapped in braces { }
• For example:
{
System.out.println ("Hello World");
System.out.println ("Welcome to Java");
}
• Note that each statement is terminated by a semicolon
Dec 21, 2012 OOSSE - Java Lecture 1 13
14. Simple Input – The Scanner Class
Dec 21, 2012 OOSSE - Java Lecture 1 14
15. Input in Java – the Scanner class
• Java 1.5 introduces adequate support for input
– Prior to this input from the keyboard was not trivial
• The Scanner class can be used for keyboard input
• Consider the following code extract:
// build an object that knows how to obtain keyboard data
Scanner kybd = new Scanner(System.in);
int num1;
// input the next integer and assign to num1
num1 = kybd.nextInt();
• The object kybd knows how to obtain the next integer from
the keyboard that is identified by System.in
Dec 21, 2012 OOSSE - Java Lecture 1 15
16. Objects and Classes
• Using classes makes performing complex tasks simple
• In order to use a class you need to know:
– Where the class is located
– How to build an instance of the class – an object
– What instances of the class can do
– How to ask the instance to do something
• A class has a set of methods that define the functionality
that it can provide
– For example nextInt is a method of the Scanner class
• A method is invoked by sending a message to an object
– num1 = kybd.nextInt();
Dec 21, 2012 OOSSE - Java Lecture 1 16
17. Using the Scanner Class
• The Scanner class is contained in a Java package
– A package is a library of classes
• The package must be imported into an application that
uses the Scanner class
– So that the compiler can find it
import java.util.*; // the package containing Scanner
public class TestScan
{
public static void main (String [] args)
{
// build an instance of Scanner, that is an object
Scanner kybd = new Scanner(System.in);
Dec 21, 2012 OOSSE - Java Lecture 1 17
18. Methods of the Scanner Class
• Some of the methods of the Scanner class are:
– nextInt() reads an integer
– nextDouble() reads a double
– next() reads a word
– nextLine() reads the rest of the current input line
• The two methods next and nextLine both return a value of
type String
– String name;
– name = kybd.next(); // assumes kybd is an instance of
// the Scanner class
Dec 21, 2012 OOSSE - Java Lecture 1 18
19. Pitfalls
• The nextLine method inputs the REST of a line of text
– It starts wherever the last input finished
• Consider the following section of code:
Scanner kybd = new Scanner(System.in);
String s1, s2;
int num1 = kybd.nextInt();
s1 = kybd.nextLine();
s2 = kybd.nextLine();
• What would s1 and s2 be if you entered the following?
42
The answer to
Life the Universe and everything
Dec 21, 2012 OOSSE - Java Lecture 1 19
20. Pitfalls
• The variable s1 would be set to an empty string
• The variable s2 would be set to “The answer to”
• Why?
• What would s1 and s2 be if you entered the following?
42 The answer to
Life the Universe and everything
Dec 21, 2012 OOSSE - Java Lecture 1 20
21. Coding Style Guidelines
• Java is a free format language but layout makes a huge
difference to understanding
• Adopt a good program layout to improve readability
– Generous use of space
– Vertical alignment of keywords
– Indentation as appropriate
• Use meaningful comments
– Level of intent
– Particularly where the code is not obvious
• Use meaningful identifiers
• Avoid complex program structures where possible
• Try not to sacrifice clarity and simplicity for efficiency
Dec 21, 2012 OOSSE - Java Lecture 1 21
22. Summary
In this lecture we have:
• Introduced Java and the Java Virtual Machine
• Discussed the basic object oriented concepts
• Defined classes and instances
• Reviewed the structure and syntax of a Java program
• Introduced the Scanner class and simple I/O
Dec 21, 2012 OOSSE - Java Lecture 1 22
Notes de l'éditeur
Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.
Add extra code for tracing and verification – such as conditionally compiled diagnostic code MENTION THIS LATER in the course