• (2 Pages)
  • +
  • 1
  • 2

Hello World tutorial A rambling introduction to Java Rate Topic: ***** 2 Votes

#1 Jhin  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 257
  • Joined: 06-July 05

Posted 28 July 2005 - 08:02 AM

*
POPULAR

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
/*  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;

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:

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:

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:

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:

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

java HelloWorld


And that's it!

Is This A Good Question/Topic? 6
  • +

Replies To: Hello World tutorial

#2 lilygrace  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 17-August 05

Posted 13 September 2005 - 01:54 AM

great tut!! especially for beginner like me.. hope you to write more!
Was This Post Helpful? 0
  • +
  • -

#3 dhana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-June 06

Posted 05 June 2006 - 03:24 AM

Nice definition for starters like me keep going little bit inner.[font=Verdana]
Was This Post Helpful? 0
  • +
  • -

#4 TaintedNobodies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 11-September 06

Posted 13 September 2006 - 09:40 AM

thanks...this helped me understand stuff...ALOT
Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Posted 13 January 2007 - 05:15 PM

Really nice tutorial!
Was This Post Helpful? 0
  • +
  • -

#6 NeoGreen  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 243
  • Joined: 28-April 06

Posted 13 March 2007 - 07:53 PM

Outstanding Tutorial. Very helpful. :)
Was This Post Helpful? 0
  • +
  • -

#7 LOVESOMEONE3495  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-September 07

Posted 19 September 2007 - 08:17 AM

Nice tutorial......specially for any begineer.....
Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Posted 09 October 2007 - 02:28 AM

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
Was This Post Helpful? 0
  • +
  • -

#9 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Posted 09 October 2007 - 05:58 AM

That's definitely a typo, I'll see if we can correct it.

Update: fixed.
Was This Post Helpful? 0
  • +
  • -

#10 wael245  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 07

Posted 19 November 2007 - 09:51 AM

im wael and im new member ijust read ur tutorial so thank you very much
Was This Post Helpful? 0
  • +
  • -

#11 xpertpan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 22-November 07

Posted 15 December 2007 - 12:58 PM

Nice tutorial.... thank you Jhin
Was This Post Helpful? 0
  • +
  • -

#12 jannat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-October 07

Posted 15 January 2008 - 10:13 PM

Very helpful Tut ....made the concept Crystal clear :^:
Thanks alot!
Was This Post Helpful? 0
  • +
  • -

#13 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Posted 19 February 2008 - 11:13 AM

Thanks a lot for writing this!!! It helped me out a ton!!!
Was This Post Helpful? 0
  • +
  • -

#14 vinaydc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 17-March 08

Posted 24 March 2008 - 04:22 AM

awesome tutorial friend..hope it helps me to start java.
Was This Post Helpful? 0
  • +
  • -

#15 Ebraham  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-March 08

Posted 28 March 2008 - 08:12 AM

please..could any one tell me what's the program use for java programming
thanks alot for Jhin :^:

This post has been edited by Ebraham: 28 March 2008 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2