Wednesday, March 6, 2013

HelloWorld Program in Java with Example

First of all, welcome to the exciting world of Java programming. If you are ready to write your first Java program i.e. HelloWorld in Java, it means you already crossed major hurdles to start Java programming, in terms of installing JDK and setting PATH for Java. If you haven't done so then you can follow those tutorial to install JDK in Windows 7 and 8 and setting PATH for Java. Before we start writing HelloWorld in Java, few notes about editors. Many Java beginners insist or try to use Eclipse or Netbeans IDE from very start of first program, which is not a good idea. In order to learn syntax and semantics of Java programming, you should code in notepad, word pad or any other text editor. I started coding in DOS Editor, but I won't suggest that to you. Notepad++ is best of both world, it support Java syntax highlighting, which helps to understand keywords better, and its also a pleasant coding experience. If you don't have notepad++, you can download it form http://notepad-plus-plus.org/, its FREE. Once you have JDK, PATH and Notepad++ installed, it’s time to code your first Java program, HelloWorld.



HelloWorld in Java

HelloWorld example program in Java
Here are step by step guide to code, compile and run Helloworld in Java from command prompt. Yes, we are going to run Java program from command prompt, if you are not  very familiar then see that link.

1) Create a Java source file as HelloWorld.java in Notepad++. It's rule in Java to name source file on name of public class inside that file.

2) Create a class, HelloWorld as public class HelloWorld{}. It's Java coding convention to start name of class with capital letter.

3) Now, add a main method inside HelloWorld class. as public static void main(String args[]){}. Like C programming language, execution of Java program from start method. There is meaning of every single keyword used with main method, and you can check why main is public static and void in Java to know this. By the way there are couple of syntax to declare standard main method, but we will stick with this, as it's standard one and simplest of all.

4) Now it's time to write code to print HelloWorld in console. You can write System.out.println("HelloWorld"); inside main to print HelloWorld in Java. Remember to put semi colon at end of statement, as statement in Java ends with semi colon. System is inbuilt class which comes with JDK and it’s part of rich Java API. DOT operator is used to access field and methods, which means out is a filed inside System class, which has println() method, and which does the job of printing characters or string to console.

5) If you look at println() method it  accepts a String literal, which is enclosed in double quotes in Java, and prints it on console and also adds a new line. here is complete code example of HelloWorld program in Java

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

6) Save your Java source file and ready to compile it.

7) Open command prompt in Windows, by typing cmd on run window and type javac, if you see,  javac not recognized as internal or external command, means PATH for Java is not set and we need to correct that. See this tutorial to set Java Path on Windows. If you see javac output and options means we are ready to compile.

8) Now go to the directory, where you have put your Java source file, HelloWorld.java and type : javac HelloWorld.java , this command will instruct Java compiler to compile Java source file and create class files, which contains Java bytecode for execution in JVM. Compiler will show compilation error, if there is any syntax, semantics or compiler error in Java source file. If you see error, then we need to resolve that. Just paste it here and we can work it out.

9) If everything goes well, we could see, HelloWorld.class file in current directory. It means you are ready to run your hello world program in Java. Next step is to run Java program.

10) Type java HelloWorld on command prompt, make sure you run this command from current directory, where HelloWorld.class file is present. You see just name of class without extension, and it will instruct JVM to find, load and execute HelloWorld class. If there is no runtime exception, than you can see "HelloWorld Java" as output in console.


That's all on How to create, compile and run HelloWorld program in Java. I highly recommend to use notepad++ for coding at least for few months, to learn Java programming. If you face any error while compiling or running this Java program, then please post here. We will try to resolve and explain it here.

Thank you and happy coding in Java.

Related Java Programming Tutorials from Java67

No comments:

Post a Comment

Java67 Headline Animator