200 likes | 213 Views
Learn the basics of Java programming with this guide on creating and running your first Java program. Follow step-by-step instructions to write a simple program, save it, compile, and execute to see the output message "Welcome to Java!"
E N D
Hands-on Introduction to JAVA First Java Program Michael Fung, CS&E, The Chinese University of HK
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Welcome main Java Virtual Machine The First Java Program Welcome to Java! Message sender Michael Fung, CS&E, The Chinese University of HK
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
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
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
End Note • Readings and References • Chapter 1 Michael Fung, CS&E, The Chinese University of HK