Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hands-on Introduction to JAVA

Similar presentations


Presentation on theme: "Hands-on Introduction to JAVA"— Presentation transcript:

1 Hands-on Introduction to JAVA
First Java Program b Michael Fung, CS&E, The Chinese University of HK

2 The First Java Program Type all carefully and save it to a file named Welcome.java class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

3 The First Java Program Java program source files (.java) contain definition of classes class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

4 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! b Michael Fung, CS&E, The Chinese University of HK

5 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! b Michael Fung, CS&E, The Chinese University of HK

6 The First Java Program This is a block of comments, for human, not for computer class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } It explains to you what happens b Michael Fung, CS&E, The Chinese University of HK

7 The First Java Program /* and */ pair encloses a comment block
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! b Michael Fung, CS&E, The Chinese University of HK

8 The First Java Program This is a method of the class Welcome, named main( ) class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

9 The First Java Program There MUST be a pair of parentheses following ALL method names 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! b Michael Fung, CS&E, The Chinese University of HK

10 Michael Fung, CS&E, The Chinese University of HK
The First Java Program Let's leave these first. Let's leave these first. class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

11 The First Java Program Standard properties of the main( ) method
class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

12 The First Java Program A statement (instruction) to display a message
class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

13 The First Java Program After every statement, there must be a semi-colon! class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

14 Michael Fung, CS&E, The Chinese University of HK
The First Java Program How to ask the computer to act according to the instructions in this program? class Welcome { /* The Welcome Program Illustrates a simple program displaying a message. */ public static void main (String [ ] args) { System.out.println("Welcome to Java!"); } b Michael Fung, CS&E, The Chinese University of HK

15 Michael Fung, CS&E, The Chinese University of HK
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? b Michael Fung, CS&E, The Chinese University of HK

16 Michael Fung, CS&E, The Chinese University of HK
The First Java Program Welcome to Java! Welcome main Java Virtual Machine Message sender b Michael Fung, CS&E, The Chinese University of HK

17 What has happened? Java Program [Welcome.java] Compile
Java Compiler [javac] Compile Java Byte Code [Welcome.class] native code Translate Java Virtual Machine (JVM) [java] It locates the class method main() from the class Welcome and starts execution from there b Michael Fung, CS&E, The Chinese University of HK

18 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. b Michael Fung, CS&E, The Chinese University of HK

19 Software Life Cycle Computer programming is not just writing programs after learning a language. requirements specification program design conception design analysis 70% of the software cost is related to software maintenance in the operation phase. Well-designed and constructed software is easier to maintain. coding actual program operation debugging testing b Michael Fung, CS&E, The Chinese University of HK

20 Michael Fung, CS&E, The Chinese University of HK
End Note Readings and References Chapter 1 b Michael Fung, CS&E, The Chinese University of HK


Download ppt "Hands-on Introduction to JAVA"

Similar presentations


Ads by Google