220 likes | 235 Views
Learn Java programming fundamentals with an introductory text-printing program. Understand comments, white space, class declaration, reserved words, method structure, and more.
E N D
IT151: Introduction to Programming Introduction to Java Applications
// Text-printing program • // Created by: Siua Fonua • // Date: 14 Feb 2006 • public class Welcome • { • // main method begins execution of Java Applications • public static void main (String args[ ]) • { • System.out.println(“Welcome to IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome
Line 1 – Line 3 • Comments • Begins with // • Comments are used to improve the program’s readability • Java compiler ignores all comments • To ways of indicating a comment • // end-of-line comment • /* … */ multiple-line comments
Line 4, 12 • White space • Includes • Blank lines • Space characters • Tab characters • Makes the program easier to read • Ignored by the compiler
Line 5 • Class declaration • Every program in Java consists of at least one class • The keyword class is used to declare a class, followed by the name of the class • The name of the class is called it’s identifier • NOTE: • YOU MUST DECLARE EVERYTHING IN JAVA
Reserved Words • Also known as keywords • These words are reserved for use by Java • Examples include: • class • Public, and more to come • Don’t use any of the Java keywords for your own use. IT WILL CONFUSE THE COMPILER
Class Names • Rules for naming your class • Contains only • Letters • Digits • Underscore ( _ ) • Dollar sign ( $ ) • Does not contain spaces • Does not begin with a digit • Convention • All class names begin with a capital letter • Capitalise the first letter of each word they include
Java is case-sensitive • That is: • Uppercase and lowercase letters are distinct • Examples: • TIHE is different from Tihe or tihe • Not using the correct letters for an identifier causes a compilation error
Public classes • Every class you declared public: • Must be saved in its own file • Class name must be the same as the filename • Must be ended with the .java extension • Failing to follow these rules, result in errors • Sometimes your class will not be compiled by the compiler
Line 6, 14 • Left brace, { • Begins the body of the class • Right brace, } • Ends the body of the class • It is a syntax error if braces do not occur in matching pairs
Line 8 • The starting point of every Java applications • Your program must have this line, exactly as it is shown here! • Otherwise, your program will not be compiled by the compiler
Methods • A block of code in the program that performs a specific task • Methods are able to perform a task and returns information when they complete execution • Some methods will not return any information when they complete execution • All methods will have • Return type • Method name • List of parameters (inside a pair of parentheses)
void methods • When the keyword void appears in front of the method name, it indicates that this method will NOT return anything when it completes execution • We’ll discuss later, the methods that will return information after execution
Line 9, 13 • Left brace, { • Begins the body of the method • Right brace, } • The end of the method’s body • Whatever is between these braces, will be the actions to be performed by the method
Line 10 • Instructs the computer to perform an action • That is:- • To print a string of characters contained between the double quotation marks • White-space characters in strings are not ignored by the compiler
Standard output • System.out is called the standard output object • Allows Java to display sets of characters in the window from which Java executes
System.out.println • This method displays a line of text in the command window • Needs an argument, which is a string of characters • Outputs its argument to the command window • After completion, it positions the output cursor in the next line
Line 11 • What does this line do?
Statement • The entire line 10, is called a statement • Each statement MUST end with a semicolon • A method is typically composed by one or more statements that performs the method’s task • NOTE: omitting the semicolon at the end of a statement, is a syntax errorh
// Text-printing program • // Created by: Siua Fonua • // Date: 14 Feb 2006 • public class Welcome • { • // main method begins execution of Java Applications • public static void main (String args[ ]) • { • System.out.print(“Welcome to IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome
// Text-printing program • // Created by: Siua Fonua • // Date: 14 Feb 2006 • public class Welcome • { • // main method begins execution of Java Applications • public static void main (String args[ ]) • { • System.out.println(“Welcome\n to\n IT151”); • System.out.println(“Enjoy programming with Java”); • } // end main method • } // end class Welcome
Escape characters • The backslash (\) is called an escape character in Java • \n • \t • \r • \\ • \”