Hello World

 Java can do just about anything. You can build a variety of applications for both desktop and mobile. You can write software on one platform and run it on virtually any other platform. And you can make games or create sophisticated Graphical User Interfaces (GUIs).

Java has done so much already, who knows what the future holds. Maybe Java will pilot ships through outer space, automate every business in the world, leaving us all unemployed. Or maybe finally give intelligent life to computers, forcing humanity into a desperate fight for survival against the machines. Cool. All this unlimited potential and it begins with just a single program; “Hello World.”

To write your first program you will need to create a new text file. Name it “HelloWorld.java”. In your new Java text file, write this:

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

Then save your file.

Next, we need to compile your code. Launch the Command Prompt.

Note: If you do not know how to use the Command Prompt, take a little time now and learn. There are a number of quick online tutorials that will get you going posthaste.

From the Command Prompt, navigate to the directory containing your “HelloWorld.java” file. Then type “javac HelloWorld.java” and press enter. Example:

C:\Users\UserName\Files>javac HelloWorld.java

If it worked you will see a new file in your directory called “HelloWorld.class”. But there is also a very good chance you got an error. Something like:

'javac' is not recognized as an internal or external command,
operable program or batch file.

If you got that message, don’t panic. I can explain what happened and how to fix it.

The javac program, pronounced “java-see”, is the Java compiler that turns your human readable code into machine language that the computer understands. Now we just need to tell the computer where to find the binary files for the javac compiler.

Do you remember where you installed Java? It is probably in some directory like:

C:\Program Files\Java\jdk-14.0.1\bin

We need to set a path to those binary files by typing “set path= C:\Program Files\Java\jdk-14.0.1\bin” in the Command Prompt. Example:

C:\Users\UserName\Files>set path= C:\Program Files\Java\jdk-14.0.1\bin

Now try typing “javac HelloWorld.java” and press enter.

C:\Users\UserName\Files>javac HelloWorld.java

You should have a new file “HelloWorld.class” in your directory. To test your program type “java HelloWorld” in the Command Prompt.

C:\Users\UserName\Files>java HelloWorld

You should see the message “Hello World!” appear in the Command Prompt.

Note: Setting the path the way I have shown is only a temporary solution. When you close the Command Prompt, the path is lost and you will have to reset the path again next time. For a more permanent solution you can add this path to the PATH environment variable. And there are a number of online tutorials that explain how to do this.

Lets talk about our “HelloWorld” program and how it works. First consider this code:

public static void main(String[] args) {

}

This is the main entry point of our program where all of our instructions get carried out. The program’s instructions fit between the open and close brackets.

System.out.println("Hello World!");

This is just an instruction to the computer to display the text “Hello World!”

Look at the first part of our program.

public class HelloWorld {

}

You can see how the main entry part of our program fits inside this class. That is because everything in Java belongs to a class. There cannot be anything in Java except that it belongs to a class. You should also note that we named our code file “HelloWorld.java” and our class is named HelloWorld. When you write Java code, your file name and class name must match. Also, all classes must begin with a capital letter. You cannot have a class “helloWorld”, but you can have a class “Helloworld”.
We will discuss what “public”, “static”, and “void” mean in upcoming tutorials.

Challenge:

Write the “Hello World” program over and over again until you are able to write it from memory without peeking at the example. Also, change the output text to say silly things. Have fun with it!

Next Topic: Comments


Comments

Popular Posts