Download presentation
Presentation is loading. Please wait.
Published byRoberta Blake Modified over 9 years ago
1
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
2
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
3
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
4
A simple Java program CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 // //Sample –Comments, not processed by Java public class HelloWorldApp –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 // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); }
5
A simple Java program CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 { } –Code braces that group code statements together // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); }
6
A simple Java program CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.1 public static void main(String args[ ]) –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 // // Sample public class HelloWorldApp { public static void main (String args[ ]) { System.out.println (“Hello World!”); }
7
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
8
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
9
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
10
Documentation Comments // /**/ Javadoc /***/ CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.3
11
Documentation Comments // /**/ Javadoc /***/ CPRG 215 Module 1.3- Write Your First Java Program Copyright SAIT Topic 1.3.3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.