Java Tutorial/Hello World!: Difference between revisions

Content deleted Content added
Wiki13 (discuss | contribs)
m Reverted edits by 208.108.139.14 (Talk) to last version by Dave Braunschweig using rollback
Tag: Rollback
Line 2:
 
Now let's see the program:
 
== Hello World Java Program ==
 
<source lang="java">
public class HelloWorld {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
System.exit( 0 ); //success
}
}
</source>
 
== Compiling the Program ==
 
Type this program up in your text editor and save it as <code>HelloWorld.java</code>. Go to your console window (in Windows, go to the start menu and click "run", then type "cmd.exe"; in Linux, open a terminal). Use the cd command (<code>cd dirname</code> or .. to go up one directory) to navigate to the directory where HelloWorld.java was saved. Compile the program using the command <code>javac HelloWorld.java</code>. Remember that Java is case-sensitive, so even when you compile your program, you have to make sure you are typing in the file name exactly.
 
Compiling the program will produce the file HelloWorld.class, the JVM version of your program. This code is machine independent.
 
Your first impression of this program is probably "This is really long just to display a line of text." While this may be true compared to other programming languages, this program shows a lot about the Java programming language, way beyond simply displaying a line of text.
 
== Running the Program ==
 
To run the compiled version of the HelloWorld program, on the command line simply type:
 
<code>java HelloWorld</code>
 
Do not add the ".class" extention to the name; the Java interpreter will already be able to locate the file based on the class name you provided, and adding the extention incorrectly will simply confuse it.
 
== Line-by-line analysis ==
 
Let us examine the program line by line. The first line
 
<source lang="java">public class HelloWorld {</source>
 
declares a class called HelloWorld. (Almost) everything in Java is contained in something called a '''class'''. Classes are the basis of '''Object-Oriented Programming''' (OOP) which we will discuss in a later lesson.
 
<br>The second line
 
<source lang="java">public static void main( String[] args ) {</source>
 
declares a method called main(String[]). Most of the actual code in Java is found within methods. Methods are one of the elements of classes. This particular method is executed when the HelloWorld class is executed using the <code>java</code> command. Note the <code>String[] args</code>. This means that the "arguments" will be passed into the main method - essentially, within this method, you can edit and see arguments.
 
<br>The third line
 
<source lang="java">System.out.println( "Hello World!" );</source>
 
does pretty much what you think it does: it writes "Hello World!" to your screen. <code>println</code> means to print the string (text) provided and then add a line break (like the enter key).
 
<br>The fourth line
 
<source lang="java">System.exit( 0 ); //success</source>
 
exits the program with return code of 0, meaning success. Anything besides 0 means that an error was encountered. Note the <code>//success</code> after <code>System.exit( 0 );</code>. This is a comment, meaning that it will not be read by the compiler (javac). Comments all begin with a "//", and anything after the comment (on the same line) will not be read. Comments are used so that you or anyone reading your code know what the code is doing.
 
The remaining lines are to close the method and the class.
 
==Excercises==
 
Play with the Hello World program, and:
* Change the text displayed
* Display multiple lines
* Play around with comments
* Try deleting or changing parts of the program like changing "public" to "private" and recompile to see what happens.
* Try adding \n\t\t\t at the beginning of the Hello World!!! string.
 
Example:
 
<source lang="java">
public class HelloWorld {
public static void main( String[] args ) {
System.out.println("Hello, my name is Noah!");
System.out.println("I will be your virtual assistant!");
System.out.println("Please, be my guest");
//System.out.println("this won't be printed!")
//this is a comment
System.exit(0); //another comment
}
}
</source>
 
{{Java Tutorial/Navigation|Choosing the right text editor|Variables}}
 
[[Category:Java tutorial]]