1 / 20

Hands-on Introduction to JAVA

Hands-on Introduction to JAVA. First Java Program. 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. */

tiffanyl
Télécharger la présentation

Hands-on Introduction to JAVA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hands-on Introduction to JAVA First Java Program 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!"); } } 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!"); } } 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! 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! 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 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! 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!"); } } 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! Michael Fung, CS&E, The Chinese University of HK

  10. 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. */ publicstaticvoid main (String [ ] args) { System.out.println("Welcome to Java!"); } } 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!"); } } 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!"); } } 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!"); } } Michael Fung, CS&E, The Chinese University of HK

  14. 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!"); } } Michael Fung, CS&E, The Chinese University of HK

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

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

  17. What has happened? Java Program [Welcome.java] 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 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. 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 Michael Fung, CS&E, The Chinese University of HK

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

More Related