02_the hello world application
TRANSCRIPT
-
8/3/2019 02_The Hello World Application
1/32
2. The Hello World Application
Java
Summer 2008 Instructor: Dr. Masoud Yaghini
-
8/3/2019 02_The Hello World Application
2/32
The Hello World Application
OutlineIntroduction
Creating Your First ApplicationA Closer Look at the "Hello World!" ApplicationReferences
-
8/3/2019 02_The Hello World Application
3/32
Introduction
-
8/3/2019 02_The Hello World Application
4/32
The Hello World Application
The "Hello World!" ApplicationYour first application, HelloWorldApp, willsimply display the greeting "Hello World!".We can use: An Integrated Development Environments (IDE) or
A simple text editor.
-
8/3/2019 02_The Hello World Application
5/32
The Hello World Application
"Hello World!" for Microsoft WindowsTo write your first program, you'll need: The Java SE Development Kit 6 (JDK 6) A text editor
You can download the Windows version ofJDK6 from:http://java.sun.com/javase/6/download.jsp
We use a text editor to create and edit sourcefiles. We will can use Notepad , a simple editor
included with the Windows platforms.
-
8/3/2019 02_The Hello World Application
6/32
The Hello World Application
Update the PATH variableSet the PATH variable if you want to be able toconveniently run the JDK executables(javac.exe, java.exe, javadoc.exe, etc.) fromany directory without having to type the fullpath of the command.If you don't set the PATH variable, you need tospecify the full path to the executable everytime you run it, such as: C:> "\Program Files\Java\jdk1.6.0_\bin\javac"
MyProgram.java
-
8/3/2019 02_The Hello World Application
7/32
The Hello World Application
Update the PATH variableTo set the PATH permanently, add the full pathof the jdk1.6.0_\bin directory to thePATH variable.Set the PATH as follows on MicrosoftWindows: Click Start > Control Panel > System on Windows
XP or Start > Settings > Control Panel > Systemon Windows 2000.
Click Advanced > Environment Variables . Add the location of bin folder of JDK installation for
PATH in User Variables and System Variables.A typical value for PATH is: C:\Program
Files\Java\jdk1.6.0_\bin
-
8/3/2019 02_The Hello World Application
8/32
The Hello World Application
Update the PATH variablePATH environment variable is a series ofdirectories separated by semi-colons (;) and isnot case sensitive.Microsoft Windows looks for programs in thePATH directories in order, from left to right.Add the path to the right end of the PATH inthe User Variables.
The new path takes effect in each newcommand window you open after setting thePATH variable.
-
8/3/2019 02_The Hello World Application
9/32
Creating Your First Application
-
8/3/2019 02_The Hello World Application
10/32
The Hello World Application
Creating Your First ApplicationTo create this program, you will: Create a source file .
A source file contains code, written in the Javaprogramming language.
Compile the source file into a .class file .
The Java programming language compiler (javac) takesyour source file and translates its text into instructions thatthe Java virtual machine can understand.The instructions contained within this file are known as
bytecodes . Run the program .
The Java application launcher tool (java) uses the Javavirtual machine to run your application.
-
8/3/2019 02_The Hello World Application
11/32
The Hello World Application
Create a Source FileStart your editor, and in a new document, typein the following code:
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output.
*/ class HelloWorldApp {
public static void main(String[] args) { System.out.println("Hello World!"); // Display the string.
} }
-
8/3/2019 02_The Hello World Application
12/32
The Hello World Application
Create a Source FileType all code, commands, and file namesexactly as shown.Save the code in a file with the nameHelloWorldApp.java .
-
8/3/2019 02_The Hello World Application
13/32
The Hello World Application
Compile the Source File into a .class FileBring up a shell, or "command," window.You can do this from the Start menu by choosingCommand Prompt (Windows XP), or by choosing Run .. . and then entering cmd .
-
8/3/2019 02_The Hello World Application
14/32
The Hello World Application
Compile the Source File into a .class FileTo compile your source file, change your currentdirectory to the directory where your file is located.If your source directory is java on the D drive, type thefollowing command at the prompt and press Enter:
D:
cd C:\java
-
8/3/2019 02_The Hello World Application
15/32
The Hello World Application
Compile the Source File into a .class FileIf you enter dir at the prompt, you should seeyour source file:
-
8/3/2019 02_The Hello World Application
16/32
The Hello World Application
Compile the Source File into a .class FileAt the prompt, type the following command and press Enter:
javac HelloWorldApp.java
The compiler has generated a bytecode file,HelloWorldApp.class .
At the prompt, type dir to see the new file that was generated
-
8/3/2019 02_The Hello World Application
17/32
The Hello World Application
Run the ProgramIn the same directory, enter the followingcommand at the prompt:
java HelloWorldApp
-
8/3/2019 02_The Hello World Application
18/32
The Hello World Application
javac & java are Case SensitiveBoth the compiler (javac) and launcher (java)are case-sensitive.In other words, HelloWorldApp is notequivalent to helloworldapp.
-
8/3/2019 02_The Hello World Application
19/32
A Closer Look at the
"Hello World!" Application
-
8/3/2019 02_The Hello World Application
20/32
The Hello World Application
A Closer Look at the "Hello World!" Application
Here again is its code:
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output.
*/ class HelloWorldApp {
public static void main(String[] args) { System.out.println("Hello World!"); //Display the string.
} }
-
8/3/2019 02_The Hello World Application
21/32
The Hello World Application
A Closer Look at the "Hello World!" Application
The "Hello World!" application consists of threeprimary components: Source code comments, The HelloWorldApp class definition, and The main method.
The following explanation will provide you witha basic understanding of the code
-
8/3/2019 02_The Hello World Application
22/32
The Hello World Application
A Closer Look at the "Hello World!" Application
The following bold text defines the comments:
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output.
*/ class HelloWorldApp {
public static void main(String[] args) { System.out.println("Hello World!"); //Display the string.
} }
-
8/3/2019 02_The Hello World Application
23/32
The Hello World Application
Source code commentsComments are ignored by the compiler but are usefulto other programmers.
The Java programming language supports three kindsof comments: /* text */
The compiler ignores everything from /* to */.
/** documentation */ This indicates a documentation comment (doc comment, forshort).The compiler ignores this kind of comment, just like it ignorescomments that use /* and */.The javadoc tool uses doc comments when preparingautomatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
-
8/3/2019 02_The Hello World Application
24/32
The Hello World Application
HelloWorldApp class definitionHelloWorldApp class definition:
class name { ...}
The keyword class begins the class definitionfor a class named name, and the code for eachclass appears between the opening and losingcurly braces marked in bold above.
We will classes in detail later.For now it is enough to know that everyapplication begins with a class definition.
-
8/3/2019 02_The Hello World Application
25/32
The Hello World Application
The main MethodThe following bold text begins the definitionof the main method:
/** * The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.*/ class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string. }
}
-
8/3/2019 02_The Hello World Application
26/32
The Hello World Application
The main MethodIn the Java programming language, everyapplication must contain a main method whosesignature is:public static void main(String[] args)
It's the entry point for your application and willsubsequently call up all the other methodsrequired by your program.
The modifiers public and static can be writtenin either order ( public static or static public ) butthe convention is to use public static
-
8/3/2019 02_The Hello World Application
27/32
The Hello World Application
The main MethodThe main method accepts a single argument:an array of elements of type String.
public static void main( String[] args )
You can name the argument anything youwant, but most programmers choose "args" or"argv."This array is the mechanism through which theruntime system passes information to yourapplication.
-
8/3/2019 02_The Hello World Application
28/32
The Hello World Application
The main MethodEach string in the array is called a command-line argument.Command-line arguments let users affect theoperation of the application without recompilingit.The "Hello World!" application ignores itscommand-line arguments, but you should beaware of the fact that such arguments do exist.
-
8/3/2019 02_The Hello World Application
29/32
The Hello World Application
The main MethodFinally, the following line uses the Systemclass from the API to print the "Hello World!"message to standard output:
System.out.println("Hello World!");
-
8/3/2019 02_The Hello World Application
30/32
References
-
8/3/2019 02_The Hello World Application
31/32
The Hello World Application
ReferencesS. Zakhour, S. Hommel, J. Royal, I.Rabinovitch, T. Risser, M. Hoeber, The JavaTutorial: A Short Course on the Basics , 4thEdition, Prentice Hall, 2006.
-
8/3/2019 02_The Hello World Application
32/32
The End