Download The First Java Program

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Object-Oriented Programming
Using Java
First Java Program
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
1
The First Java Program
class Welcome {
Type all carefully and save it to a
file named Welcome.java
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
2
The First Java Program
class Welcome {
Java program source files (.java)
contain definition of classes
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
3
The First Java Program
Curly braces pair enclose a block
of code, class Welcome here
class Welcome {
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
Don’t miss me!
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
4
The First Java Program
Curly braces pair enclose a block
of code, method main( ) here
class Welcome {
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
Don’t miss me!
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
5
The First Java Program
class Welcome {
This is a block of comments, for
human, not for computer
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
It explains to you what happens
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
6
The First Java Program
class Welcome {
/* and */ pair encloses a comment block
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
Don’t miss me!
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
7
The First Java Program
class Welcome {
This is a method of the class
Welcome, named main( )
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
8
The First Java Program
class Welcome {
There MUST be a pair of
parentheses following ALL
method names
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
Don’t miss me!
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
9
The First Java Program
class Welcome {
Let's leave these first.
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
10
The First Java Program
class Welcome {
Standard properties of the
main( ) method
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
11
The First Java Program
class Welcome {
A statement (instruction) to display
a message
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
12
The First Java Program
class Welcome {
After every statement, there must
be a semi-colon!
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!")
;
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
13
The First Java Program
class Welcome {
How to ask the computer to act
according to the instructions in
this program?
/* The Welcome Program
------------------Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
System.out.println("Welcome to Java!");
}
}
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
14
The First Java Program
 Change to the directory containing the file
Welcome.java
 Type
javac Welcome.java
It generates a new file Welcome.class
 Type (without .class)
java Welcome
 What’s the result?
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
15
The First Java Program
Welcome
main
Java Virtual Machine
Welcome to Java!
Message sender
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
16
What has happened?
Java Program [Welcome.java]
Compile
Java Compiler
[javac]
Java Byte Code [Welcome.class]
Translate
native code
Java Virtual Machine (JVM) [java]
It locates the class method main() from the class
Welcome and starts execution from there
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
17
Big Program For Simple Task?
 Yes, it just displays a little message.
 When writing business letters, we conform to
grammar and format.
 Likewise for programming!
 For more complicated tasks, such overheads are
relatively trivial.
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
18
Software Life Cycle
Computer programming is not just writing
programs after learning a language.
conceptio
n
analysis
70% of the software
cost is related to
software maintenance
in the operation phase.
Well-designed and
constructed software is
easier to maintain.
2008-2009 1b
requirements
specification
design
program
design
coding
operation debugging testing
actual
program
Michael Fung, CS&E, The Chinese University of HK
19
End Note
Readings and References
– Chapter 1
2008-2009 1b
Michael Fung, CS&E, The Chinese University of HK
20