Java How To Program 8/e PDF
Document Details
Tags
Summary
This document is a chapter on Java programming introducing classes and objects. It's part of a larger textbook, likely by Deitel. The file provides explanations, examples, and simple coding syntax for producing lines of text. Good programming practices are also discussed, including indentation, comments, and important errors.
Full Transcript
Java How to Program, 8/e 1 2 3 Java application programming Videos at www.deitel.com/books/jhtp8/ ▪ Help you get started with Eclipse and NetBeans integrated development environments....
Java How to Program, 8/e 1 2 3 Java application programming Videos at www.deitel.com/books/jhtp8/ ▪ Help you get started with Eclipse and NetBeans integrated development environments. 4 Java application ▪ A computer program that executes when you use the java command to launch the Java Virtual Machine (JVM). Sample program in Fig. 2.1 displays a line of text. 5 6 Comments // Fig. 2.1: Welcome1.java ▪ // indicates that the line is a comment. ▪ Used to document programs and improve their readability. ▪ Compiler ignores comments. ▪ A comment that begins with // is an end-of-line comment—it terminates at the end of the line on which it appears. Traditional comment, can be spread over several lines as in ▪ This type of comment begins with. ▪ All text between the delimiters is ignored by the compiler. 7 Javadoc comments ▪ Delimited by. ▪ All text between the Javadoc comment delimiters is ignored by the compiler. ▪ Enable you to embed program documentation directly in your programs. ▪ The javadoc utility program (Appendix M) reads Javadoc comments and uses them to prepare your program’s documentation in HTML format. 8 9 10 Blank lines and space characters ▪ Make programs easier to read. ▪ Blank lines, spaces and tabs are known as white space (or whitespace). ▪ White space is ignored by the compiler. 11 12 Class declaration public class Welcome1 ▪ Every Java program consists of at least one class that you define. ▪ class keyword introduces a class declaration and is immediately followed by the class name. ▪ Keywords (Appendix C) are reserved for use by Java and are always spelled with all lowercase letters. 13 Class names ▪ By convention, begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). ▪ A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. ▪ Java is case sensitive—uppercase and lowercase letters are distinct—so a1 and A1 are different (but both valid) identifiers. 14 15 16 17 Braces ▪ A left brace, {, begins the body of every class declaration. ▪ A corresponding right brace, }, must end each class declaration. ▪ Code between braces should be indented. ▪ This indentation is one of the spacing conventions mentioned earlier. 18 19 20 21 22 Declaring the main Method public static void main( String[] args ) ▪ Starting point of every Java application. ▪ Parentheses after the identifier main indicate that it’s a program building block called a method. ▪ Java class declarations normally contain one or more methods. ▪ main must be defined as shown; otherwise, the JVM will not execute the application. ▪ Methods perform tasks and can return information when they complete their tasks. ▪ Keyword void indicates that this method will not return any information. 23 Body of the method declaration ▪ Enclosed in left and right braces. Statement System.out.println("Welcome to Java Programming!"); ▪ Instructs the computer to perform an action Print the string of characters contained between the double quotation marks. ▪ A string is sometimes called a character string or a string literal. ▪ White-space characters in strings are not ignored by the compiler. ▪ Strings cannot span multiple lines of code. 24 System.out object ▪ Standard output object. ▪ Allows Java applications to display strings in the command window from which the Java application executes. System.out.println method ▪ Displays (or prints) a line of text in the command window. ▪ The string in the parentheses the argument to the method. ▪ Positions the output cursor at the beginning of the next line in the command window. Most statements end with a semicolon. 25 26 27 28 29 30 Compiling and Executing Your First Java Application ▪ Open a command window and change to the directory where the program is stored. ▪ Many operating systems use the command cd to change directories. ▪ To compile the program, type javac Welcome1.java ▪ If the program contains no syntax errors, preceding command creates a.class file (known as the class file) containing the platform- independent Java bytecodes that represent the application. ▪ When we use the java command to execute the application on a given platform, these bytecodes will be translated by the JVM into instructions that are understood by the underlying operating system. 31 32 33 34 To execute the program, type java Welcome1. Launches the JVM, which loads the.class file for class Welcome1. Note that the.class file-name extension is omitted from the preceding command; otherwise, the JVM will not execute the program. The JVM calls method main to execute the program. 35 36 37 Class Welcome2, shown in Fig. 2.3, uses two statements to produce the same output as that shown in Fig. 2.1. New and key features in each code listing are highlighted. System.out’s method print displays a string. Unlike println, print does not position the output cursor at the beginning of the next line in the command window. ▪ The next character the program displays will appear immediately after the last character that print displays. 38 39 Newline characters indicate to System.out’s print and println methods when to position the output cursor at the beginning of the next line in the command window. Newline characters are white-space characters. The backslash (\) is called an escape character. ▪ Indicates a “special character” Backslash is combined with the next character to form an escape sequence. The escape sequence \n represents the newline character. Complete list of escape sequences java.sun.com/docs/books/jls/third_edition/html/ lexical.html#3.10.6. 40 41 42