FIRST JAVA PROGRAM
JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public class ClassName { classMembers }
JAVA PROGRAMS Every program (NB! Not every class ) has one method called main. Sometimes a program might only have a main method. public static main (String[] args) { statement1;. statement2; }
/** * The FirstProgram class implements an application that * simply displays “Hello World!” to the standard output. */ public class Welcome { public static void main(String[] args) { System.out.println(“Hello World!”); //Display the string }
Class name –Starts with a capital letter. –E.g. FirstProgram Method name –Starts with a lower case letter. –E.g. main, getAge Methods consist of: –Comment at beginning (good idea) –Heading (method name) –Body consists of {sequence of statements}
Comments in Java Code /* text */ Compiler ignores everything from /* to */. /** documentation */ Indicates documentation comment (doc comment). Compiler ignores everything. JDK javadoc tool uses doc comments when preparing automatically generated documentation. // text Compiler ignores everything from // until end of line.
Defining a Class The skeleton of every program is a class definition. Keyword class begins the class definition. Variables & methods of the class are enclosed by {….} (begin & end the class definition block). This app. has no variables & has a single method called main.
The main Method Entry point of every Java application. To run a program you specify the name of it’s class (i.e. FirstProgram ). The interpreter invokes the main method defined within that class. Controls the flow of the program, allocates whatever resources are needed & runs any other methods that provide the functionality of the program.
The method System.out.println Displays a string of characters in a DOS window. After printing, moves the cursor to new line. Sometimes prefer to continue printing on the same line. Use: print instead of println To start printing on a new line –Insert the characters \n in a string of characters. System.out.print(“Hello World!\nSee you later\n”);
Explanation of Code... Code to begin the program: Start by writing a class. public - indicates that the method main should be accessible from outside. File name must be exactly the same as the PUBLIC class (Welcome). static - means that the method main is a class method. Relates to the class itself. void - the method main will not leave a result value. main - reserved word. Name of the method. Followed by its parameters. (String [ ] args) - parameters. Gets a handle to an array of strings.
Explanation of Code... Code to display a text string: System.out.println("Hello world."); - System - standard class found in the Java package java.lang in the Java API (Application Programming Interfaces. Contains resources of a general nature (e.g. output stream). –Note the “dot” operator. –.out - a static object of the class System. - is an output stream with the name System.out –System.out normally called standard output. - will automatically be written in a text window on the screen.
Explanation of Code... Code to display a text string: System.out.println("Hello world."); –println - is a method of the object out - if it is a method, must pass it some parameters - double-quoted text inside the parentheses is an argument to the method –general syntax: Object_Name.Method_Name(Arguments)
Your First Cup Of Java Create a Java source file –Text editor (Notepad) –.java extension Compile the source file into a byte code file –javac filename.java from command line Run the program (byte code) - interpret –java filename (no extension specified)
Environments available? TextPad jGrasp
Types of Errors Syntax Run-Time Logic
Syntax Errors a “grammatical” error caught by compiler (“compiler-time error”) automatically found, usually the easiest to fix cannot run code until all syntax errors are fixed error message may be misleading Example: Misspelling a command, for example “rturn” instead of “return”
Run-Time Errors An execution error (during run-time) Not always so easy to fix Error message may or may not be helpful Example: Division by zero - if your program attempts to divide by zero it automatically terminates and prints an error message.
Logic Errors Just because it compiles and runs without getting an error message does not mean the code is correct! An error in the design (the algorithm) or its implementation –code compiles without errors –no run-time error messages –but incorrect action or data occurs during execution Generally the most difficult to find and fix Need to be alert and test thoroughly –think about test cases and predict results before executing the code
Logic Error Examples Algorithm Error: –averageOfFiveScores = SumOfScores/2 (should divide by 5) Implementation Error: –typed in wrong symbol in source code - sum = a - b; (should be “sum = a + b;)