Loading

Pages

Wednesday, September 19, 2012

Java Tutorial Part2 - Hello World

In comparison with some of the languages you may be familiar with, Java hello world needs more lines of code and would seem verbose.

Here is how we can print the customary "Hello World" in some scripting languages:

Ruby:
puts "Hello World";

PHP:
<?php 
 Print "Hello World";
?> 

Perl:
print "Hello World"; 

Python:
print "Hello World" 

Now, let us see the Java version.

public class HelloWorld{
  public static void main(String []args){
     System.out.println("Hello World");
  }

}

Do the following steps to compile and run the Java version.

1)Type the above Java code in your favourite editor and save it as HelloWorld.java. This is our source file.

2)From command line/shell go to the folder where you saved the file and type
   javac HelloWorld.java.  A new file  HelloWorld.class is generated in the
  same folder as the source file.

3)Execute the program by entering  java HelloWorld at the command prompt.

You should be able to see the output now.

No comments: