Quantcast
Channel: Vcc to Ground » Java
Viewing all articles
Browse latest Browse all 3

1.3 The Hello World Java program

$
0
0

In the previous posts, we saw learned what is Java, its history, is and how to set up the Java Runtime Environment for us to run our Java programs. At the end of this post, you would have successfully executed your first Java program.

You will need one other tool, apart from the JDK, to write and execute Java programs. We are talking about a basic text editor, or an advanced Integrated Development Environment (IDE). There are many sophisticated IDEs available for Java, some of the popular ones are Eclipse and NetBeans. But for now, we can use a basic text editor, such as your Windows Notepad. If you want something more than that, you can try Notepad++, which is a good tool too.

Why use IDEs? IDEs are used only for the support they give the programmers. For example, IDEs provide a proper directory structure for your projects, they provide syntax highlighting, they provide auto-complete and recommendations, etc. I believe that you should move to an IDE when you are completely familiar with the basic syntax and the core methods (functions) in Java. So till then, let’s stick with Notepad or Notepad++.

Before starting the code, you need to brush up your Object Oriented Programming (OOP) concepts. We will assume that you have done that already. In Java, you start everything with a class. Even in the simple Hello World program, you need to define a class. And the structure is such that your .java file should have the name of the class that you have defined.

Now, let’s define a “Hello” class. Open your text editor and copy the code given below into a new file. Save the file with the name “Hello.java”. Since the class we have defined is “Hello”, the file name has to be “Hello.java”, or else you will get an error. Your first Java is now ready to be compiled and executed.

/* Hello, World! Java Program */
/* Vcc To Ground */
/* vcctoground.com */

public class Hello {

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

}

To compile this file, open Command Prompt, and navigate to the folder where you have saved the .java file. Once you are there, type in “javac Hello.java” against the prompt and hit Enter. You should just get the prompt back without any messages. If so, your compilation has been successful, and there are no errors in your code. If in case there are any errors or warning, the compiler will output the same in the window.

Now, after compiling the code, if you check the folder where the .java file is located, you will see a new file, “hello.class”. This is the byte code that we talked about. You can copy this file to any other which has a JVM running on it and run this file. You will get the output. To check the output, you just type in “java Hello” at the prompt. This command will run the .class file that is created. Now, your command prompt window should look something like this.

Hello World Output

Congratulations. You have now successfully executed your first Java program. You can try to edit the contents of the file and check if you get the output accordingly.  For instance, remove the word “World” in the line

System.out.println(“Hello, World”);

And instead, type in your name. For example, I would write System.out.println(“Hello, Sunny”);. Now compile the code again and run it. Did the output change? Well, it’s that simple.

In the next post, we will see what each word in this code means and what significance it has. Till then, you can try to vary the code and get different outputs, maybe learn a few DOS commands which will help you later. If you are on a Linux or a Mac machine, learn a few Terminal commands, which will be of great help in the long run.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images