Introduction to Computing
Using Java
First Java Program
2019 1b Michael Fung, CS&E, The Chinese University of HK 1
The First Java Program
Type all carefully and save it to
class Welcome { a file named [Link]
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 2
The First Java Program
Java program source files (.java)
class Welcome { contain definition of classes
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 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
class Welcome {
here
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
} Don’t miss me!
2019 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( )
class Welcome {
here
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
} Don’t miss me!
2019 1b Michael Fung, CS&E, The Chinese University of HK 5
The First Java Program
This is a block of comments, for
class Welcome { 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) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 6
The First Java Program
/* and */ pair encloses a comment block
class Welcome {
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/ Don’t miss me!
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 7
The First Java Program
This is a method of the class
class Welcome { Welcome, named main( )
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 8
The First Java Program
There MUST be a pair of
parentheses following ALL
class Welcome { method names
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
} Don’t miss me!
2019 1b Michael Fung, CS&E, The Chinese University of HK 9
The First Java Program
Let's leave these first.
class Welcome {
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 10
The First Java Program
Standard properties of the
class Welcome { main( ) method
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 11
The First Java Program
A statement (instruction) to
class Welcome { display a message
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 12
The First Java Program
After every statement, there
class Welcome { must be a semi-colon!
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!") ;
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 13
The First Java Program
How to ask the computer to
act according to the
class Welcome { instructions in this program?
/* The Welcome Program
-------------------
Illustrates a simple program displaying
a message.
*/
public static void main (String [ ] args) {
[Link]("Welcome to Java!");
}
}
2019 1b Michael Fung, CS&E, The Chinese University of HK 14
The First Java Program
Change to the directory containing the file
[Link]
Type
javac [Link]
It generates a new file [Link]
Type (without .class)
java Welcome
What’s the result?
2019 1b Michael Fung, CS&E, The Chinese University of HK 15
The First Java Program
Welcome
Welcome
main Java Virtual Machine
Welcome to Java!
Message sender
2019 1b Michael Fung, CS&E, The Chinese University of HK 16
What has happened?
Java Program [[Link]]
Compile Java Compiler
[javac]
Java Byte Code [[Link]] Translate native code
Java Virtual Machine (JVM) [java]
It locates the class method main() from the
class Welcome and starts execution from there
2019 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.
2019 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.
requirements program
conception
specification design design
analysis
70% of the software
cost is related to coding
software maintenance
in the operation phase.
Well-designed and
actual
constructed software is
easier to maintain. operation debugging testing program
2019 1b Michael Fung, CS&E, The Chinese University of HK 19
End Note
Readings and References
– Chapter 1
2019 1b Michael Fung, CS&E, The Chinese University of HK 20