Thursday, April 20, 2023

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 tutorials 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 the very start of the first program, which is not a good idea. 


In order to learn the 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 the best of both worlds, it supports Java syntax highlighting, which helps to understand keywords better, and it's also a pleasant coding experience. 

If you don't have notepad++, you can download it from 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

Here is a step by step guide to coding, compile and run HelloWorld in Java from the command prompt. Yes, we are going to run a Java program from the 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 ruled in Java to name source file on the name of the public class inside that file.

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

3) Now, add the 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 the meaning of every single keyword used with the main method, and you can check why main is public static and void in Java to know this. By the way, there are a couple of syntaxes to declare a 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 the console. You can write System.out.println("HelloWorld"); inside main to print HelloWorld in Java. Remember to put semi colon at the end of the statement, as a statement in Java ends with a semi colon. The System is an inbuilt class which comes with JDK and it’s part of rich Java API. DOT operator is used to accessing 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 the 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 the 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 an internal or external command, means PATH for Java is not set and we need to correct that. See this tutorial 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. The compiler will show a compilation error if there is any syntax, semantics the or compiler error in Java source file. If you see an 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 the current directory. It means you are ready to run your hello world program in Java. The next step is to run the 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.

HelloWorld example program in Java


That's all on How to create, compile and run the HelloWorld program in Java. I highly recommend using notepad++ for coding for 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.

Related Java Programming Tutorials from Java67


Thank you and happy coding in Java.

1 comment:

  1. Hello, recently I have created "Java Plugin for Notepad++" that allows you to compile and run java in Notepad++.
    You can find more information here https://github.com/dominikcebula/npp-java-plugin

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.