Presentation is loading. Please wait.

Presentation is loading. Please wait.

Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.

Similar presentations


Presentation on theme: "Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open."— Presentation transcript:

1 Output in Java Hello World!

2 Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open and closing braces which indicate the start and end of the file. All contents of the Java file will be placed inside the braces.  The term class is a reserved Java keyword. Java knows exactly what this word means (which his why it appears in blue.  The name of the program is HelloWorld. This must match the name of the Java file itself!

3 The main() method  Like a maze needs a starting point, a Java program needs a place to begin its execution.  The main() method tells Java where the starting point of your program will be.  Without a main() method, your program won’t do anything! class HelloWorld { public static void main(String[] args) { // Code goes here. }

4 The main() method - Explained class HelloWorld { public static void main(String[] args) { // Code goes here. } Things to note:  The main() method is indented within the class. After an opening brace, all subsequent lines should be indented! The contents of the main() method make up the body.  Comments are lines that begin with two forward slashes (//). They are used to write notes within code that will not be executed by Java.

5 Output – System.out.println  Java provides two commands for output: System.out.print()&System.out.println()  Any text put inside of the parentheses will be printed to screen. This text must be in quotations.  The print() method simply prints the text to screen, whereas the println() method prints the text to screen and then moves the cursor to a new line. class HelloWorld { public static void main(String[] args) { // Prints Hello World to screen. System.out.println(“Hello World!”); }

6 Output – System.out.println - Explained class HelloWorld { public static void main(String[] args) { // Prints Hello World to screen. System.out.println(“Hello World!”); }  All statements in the main method are indented – this pattern will continue in all of your programs!  Write one statement per line! This makes your code easy to read.

7 Output – Example class OneLine { public static void main(String[] args) { System.out.print(“This text is on the same line…”); System.out.print(“…as this text”); }  The above code demonstrates how the print() method prints to the same line, whereas the println() method moves the cursor to a new line.

8 Special Characters  Java provides special codes, called escape sequences, which can be used to print certain spacing and characters in output statements.  The chart below shows some of these escape sequences: Escape Sequence Resulting Output \nMoves the cursor to a new line \tInserts a Tab (space) \\Inserts a backward slash “\” \’Inserts a single quotation \”Inserts a double quotation

9 Special Characters - Examples  The following code prints the numbers 1, 2, and 3 on their own lines in two different ways: using println() and using escape sequences class OneTwoThree { public static void main(String[] args) { // Print 1, 2, and 3 using println(). System.out.println(“1”); System.out.println(“2”); System.out.println(“3”); // Print 1, 2, and 3 using escape sequences. System.out.println(“1\n2\n3”); }

10 Some Useful Hints: Using NetBeans 1. Set Up NetBeans for Matching Braces Go to Tools -> Options. Navigate to the Formatting tab. Select Java from the Languages dropdown and Braces from the Category dropdown. Under Braces Placement, ensure that New Line is selected for each of the three categories (Class Declaration, Method Declaration, and Other: ). 2. Fix Your Formatting With One Click! To ensure that the format (spacing) of your program is correct, go to Source -> Format. If you notice that an opening brace does not have a matching closing brace, your program has formatting errors!

11 Some Useful Hints: Using NetBeans 3. Running a Different Program If you’ve written two programs in one project, NetBeans does not know which of the two main() methods to run. You will have to explicitly tell it as follows: Right-click on your Project and select Properties. Under the Run category, find the Main Class: field. Click Browse and choose the program you want to run.


Download ppt "Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open."

Similar presentations


Ads by Google