Your First Program in Java: Printing a Line of Text

Published on Slideshow
Static slideshow
Download PDF version
Download PDF version
Embed video
Share video
Ask about this video

Scene 1 (0s)

Your First Program in Java: Printing a Line of Text.

Scene 2 (7s)

Class Names and Identifiers. By convention, class names 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. Some valid identifiers are Welcome1, $value, _value, m_inputField1 and button7. The name 7button is not a valid identifier because it begins with a digit, and the name input field is not a valid identifier because it contains a space. Normally, an identifier that does not begin with a capital letter is not a class name. Java is case sensitive —uppercase and lowercase letters are distinct—so value and Value are different (but both valid) identifiers. In this lesson every class we define begins with the public keyword. For now, we simply require this keyword. For our application, the file name is Welcome1.java. You’ll learn more about public and non-public classes in coming lessons. Note: public class must be placed in a file that has the same name as the class (in terms of both spelling and capitalization) plus the .java extension; otherwise, a compilation error occurs. For example, public class Welcome must be placed in a file named Welcome.java . A left brace , must end each class declaration..

Scene 3 (1m 4s)

Declaring a Method. “public static void main( String[] args )” The 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. For a Java application, one of the methods must be called main and must be defined otherwise, the Java Virtual Machine (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. Later, we’ll see how a method can return information. For now, simply mimic main’s first line in your Java applications. In line 7, the String[] args in parentheses is a required part of the method main’s declaration..

Scene 4 (1m 36s)

Performing Output with System.out.println. System.out.println ( "Welcome to Java Programming!" ); instructs the computer to perform an action—namely, to print the string of characters contained between the double quotation marks (but not the quotation marks themselves). 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, but as you’ll see later, this does not restrict you from using long strings in your code. The System.out object is known as the standard output object . It allows a Java applications to display information in the command window from which it executes. In recent versions of Microsoft Windows, the command window is the Command Prompt . In UNIX/Linux/Mac OS X, the command window is called a terminal window or a shell . Many programmers call it simply the command line . Method System.out.println displays (or prints) a line of text in the command window. The string in the parentheses is the argument to the method. When System.out.println completes its task, it positions the output cursor (the location where the next character will be displayed) at the beginning of the next line in the command window. This is similar to what happens when you press the Enter key while typing in a text editor—the cursor appears at the beginning of the next line in the document. The entire line 9, including System.out.println , the argument "Welcome to Java Programming!" in the parentheses and the semicolon ( ; ), is called a statement . A method typically contains one or more statements that perform its task . Most statements end with a semicolon. When the statement executes, it displays Welcome to Java Programming! in the command window..

Scene 5 (2m 43s)

Compiling and Executing Your First Java Application.

Scene 6 (3m 2s)

To compile the program, type. “ javac Welcome1.java” If the program contains no syntax errors, this command creates a new file called Welcome1.class (known as the class file for Welcome1) containing the platform-independent Java bytecodes that represent our application. When we use the java command to execute the application on a given platform, the JVM will translate these bytecodes into instructions that are understood by the underlying operating system and hardware..

Scene 7 (3m 23s)

Error-Prevention Tip. When attempting to compile a program, if you receive a message such as “bad command or filename,” “ javac : command not found” or “' javac ' is not recognized as an internal or external command, operable program or batch file,” then your Java software installation was not completed properly. If you’re using the JDK, this indicates that the system’s PATH environment variable was not set properly. Please carefully review the installation instructions in the Before You Begin section of this book. On some systems, after correcting the PATH, you may need to reboot your computer or open a new command window for these settings to take effect. When attempting to run a Java program, if you receive a message such as “Exception in thread "main" java.lang.NoClassDefFoundError : Welcome1,” your CLASSPATH environment variable has not been set properly. Please carefully review the installation instructions in the Before You Begin section of this book. On some systems, you may need to reboot your computer or open a new command window after configuring the CLASSPATH..