Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 119,729 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,331 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Hello World tutorial

2 Pages V  1 2 >  
Reply to this topicStart new topic

> Hello World tutorial, A rambling introduction to Java

Rating  5
Jhin
Group Icon



post 28 Jul, 2005 - 08:02 AM
Post #1


Once you know one programming language, others should be fairly simple to pick up. The difference between languages is mostly syntax.

Java programs consist of modules called classes. Each class contains methods that perform tasks and return information when they have finished. Like most languages there is a large collection of pre-defined classes and methods in what is called the Java Class Library (also known as the Java API).

By using Java API classes and methods instead of writing your own will improve your java program's portability as these methods and classes are included in every java implementation.

Besides, who wants to reinvent the wheel? And it's always good not to try and manipulate the language in a strange way. Troubleshooting and debugging will be simpler if you write your Java code in a straightforward manner.

So let's look at HelloWorld.java
CODE

/*  Program name HelloWorld.java
who's sole purpose is
to
introduce java programming */

public class HelloWorld{
    public static void main (String args[])    
    {
     System.out.println("Hello World"); // note the semi-colon after the statement
    } // end of the main method
} // end of Hello World


You will notice the multi-line comment at the very beginning of the code. Comments can be written 3 ways. A single line comment is denoted by a //, where as a multi-line comment begins with /* and ends with */. All text between /* and */ are ignored by the compiler. The third and final format for a comment is a documentation comment which is delimited by /** */. If you forget to close off the end of a multi-line comment or close it incorrectly, it will appear as a syntax error to the compiler.

If you are familiar with C or C++ you will notice the similarities between their comments and Java€™s. Only the document comment is special to Java. It was designed to allow programmers to embed documentation for their programs in the programs themselves. The javadoc utility program (in the Java2 SDK) reads those comments and uses them to prepare the program documentation.

Line 4 of the code;

CODE
public class HelloWorld{


This is the beginning of a class definition for a class called HelloWorld. Every Java program has at least one class definition, because a class is basically a java program. The class keyword is immediately followed by the class name. HelloWorld is the class name in the above definition. The HelloWorld class must be stored in a file called HelloWorld.java or it will not be compiled correctly.

Most often class names begin with a capital letter; it makes it much simpler to identify class names if you follow this naming convention. It is important to note that Java is case sensitive so helloWorld and HelloWorld are 2 different class names. Also, class names can contain underscores ( _ ) and dollar signs ( $ ), but may not begin with a digit ( 0-9 ) nor contain spaces. I would recommend avoiding using $ as a class identifier, because the compiler often uses dollar signs to create identifier names.

Now the keyword public is a member access modifier. It determines if other classes and methods have access to the java application. A member with a public access modifier is accessible wherever the program has a reference to HelloWorld. You can only ever have one public class in a Java file. Defining more than one public class will result in a syntax error.

It has always been my belief that when you open a bracket { you should immediately close it } and then program within them. This way you will never run into the problem of unclosed methods or classes. If you are missing a bracket the compiler will report an error.

The next line is important as well:

CODE
public static void main (String args[])


This is the core of the Java program. This is the main method within the HelloWorld class. It is the first thing executed within the program. If there is no main defined as shown the java interpreter will not execute the application. And you will receive an error similar to this:

CODE
In class NoMain: void main(String argv[]) is not defined


In order to begin the content of this method you must have the { } to contain it.

Again the public modifier shows that the main method can be called by any object. The static modifier on the other hand states that the information stored is class wide information. In other words, all objects within the class share the same piece of data.

The void keyword tells you that this method will perform whatever task it is given but not return any values. There are many different methods of returning information that I'll discuss in other tutorials.

Now:

CODE
System.out.println("Hello World"); // note the semi-colon after the statement


System.out is known as the standard output object. It allows the Java application to display strings and other types of information in the command window from which the application executes. So this line above will print Hello World in the command window. Once println is complete it will put the cursor on the next line. Similar to when you hit the enter key.

You will notice that this line of code is the only one in the program to end with a semicolon ( ; ). The semicolon indicates the end of a statement. Every statement in Java must end in a semicolon.

Now to compile and execute the code you will need at least 2 items from the Java SDK. First, and foremost, the javac.exe which is the java compiler program. It will turn the source code above into files that can be read and understood by the java interpreter.

Next is the java.exe which is the java interpreter. It will read the files created by the java compiler and execute the code therein.

Once you are done your code to compile the program you would use the following in the command prompt:

CODE
javac HelloWorld.java


If the compiler runs into no errors in the code, the preceding command creates a new file called HelloWorld.class. This file will then be interpreted by the java interpreter when it is told to execute the file.

To execute the file type

CODE
java HelloWorld


And that's it!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

lilygrace
**



post 13 Sep, 2005 - 01:54 AM
Post #2
great tut!! especially for beginner like me.. hope you to write more!
Go to the top of the page
+Quote Post

dhana
*



post 5 Jun, 2006 - 03:24 AM
Post #3
Nice definition for starters like me keep going little bit inner.[font=Verdana]
Go to the top of the page
+Quote Post

TaintedNobodies
*



post 13 Sep, 2006 - 09:40 AM
Post #4
thanks...this helped me understand stuff...ALOT
Go to the top of the page
+Quote Post

alpha02
Group Icon



post 13 Jan, 2007 - 05:15 PM
Post #5
Really nice tutorial!
Go to the top of the page
+Quote Post

NeoGreen
**



post 13 Mar, 2007 - 07:53 PM
Post #6
Outstanding Tutorial. Very helpful. smile.gif
Go to the top of the page
+Quote Post

LOVESOMEONE3495
*



post 19 Sep, 2007 - 08:17 AM
Post #7
Nice tutorial......specially for any begineer.....
Go to the top of the page
+Quote Post

jjhaag
Group Icon



post 9 Oct, 2007 - 02:28 AM
Post #8
thanks for the nice intro - it was very clear. however, i received an error about the string argument to the main method - with my version of the jre (1.6.0.2) in eclipse it needed to be changed to String (capitalized first letter). don't know if it was a typo or just a peculiarity of my system.

-jjh
Go to the top of the page
+Quote Post

1lacca
Group Icon



post 9 Oct, 2007 - 05:58 AM
Post #9
That's definitely a typo, I'll see if we can correct it.

Update: fixed.
Go to the top of the page
+Quote Post

wael245
*



post 19 Nov, 2007 - 09:51 AM
Post #10
im wael and im new member ijust read ur tutorial so thank you very much
Go to the top of the page
+Quote Post

xpertpan
*



post 15 Dec, 2007 - 12:58 PM
Post #11
Nice tutorial.... thank you Jhin
Go to the top of the page
+Quote Post

jannat
*



post 15 Jan, 2008 - 10:13 PM
Post #12
Very helpful Tut ....made the concept Crystal clear icon_up.gif
Thanks alot!
Go to the top of the page
+Quote Post


2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/15/08 04:27PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month