Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey Peters, 2008 Copyright SAIT Please read the following sections in your textbook Core Java, Volume I–Fundamentals, Eighth Edition By Cay S. Horstmann & Gary Cornell •Chapter 3 - Fundamental Programming Structures in Java •A Simple Java Program •Comments CPRG 215 Module 1.2- Getting the Tools and Setting Up the Development Machine Copyright SAIT A simple Java program // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); } } CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 A simple Java program • // //Sample – Comments, not processed by Java • public class HelloWorldApp // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); } } – Defines this as a “class” – Every program in Java is a class – A class is like a template or blueprint that defines how an object should be built CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 A simple Java program • { } – Code braces that group code statements together // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); } } CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 A simple Java program • public static void main(String args[ ]) // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); } } – Definition for the main method – A class must have a main method in order to run – If you run a class that has no main method this message is displayed: • Exception in thread "main" java.lang.NoSuchMethodError: main CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 A simple Java program • public static void main(String args[ ]) • public – access permission - most accessible level • static – Able to run as soon as class loads into JVM – Normally methods don’t exist until we create an object from the class • void – The method does not return any data • (String args[]) – main receives a String array with command-line arguments is any are found CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 Compiling and running • Compiling: – javac HelloWorldApp.java • Running – java HelloWorldApp CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.2 Compiling and running • Troubleshooting – – – – – bad command -- path file not found -- typo? Class not found -- forgot to compile? Method not found -- typo? Case sensitivity javac: invalid argument -- forgot .java CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.2 Documentation • Comments // /* */ • Javadoc /***/ CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.3 Documentation • Comments // /* */ • Javadoc /***/ CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.3