Starting with Hello World

From the days of the famous book on C language by Kernighan and Ritchie, it is customary in teaching a programming language to start with a hello to the world. It is also perhaps the simplest program to write, so we will not violate this tradition.
To produce some output from your program, you use the notation

System.out.println("Hello, world!");

Type everything including the dots as it is. You can recognise the "print" word there reminding you that it is going to print something on your screen. The "ln" following indicates that the next output will start on a fresh line.

Replace the "YOUR PROGRAM" in the template with the above line, and try compiling and running the program. The program will look like:

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

Instead of "Hello, World!", you can put any text inside. Text enclosed in double quotation marks is called a String in Java. Do not use text containing quotation marks - they need a little special treatment. So, dont try: "Should I try " in the string"

Try, "Welcome, World" and other variants. Only simple text.

As I mentioned above, the 'ln' indicates that the next output on the screen will start on a new line. Let us try this:

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

You will see that "Hello" and "World" are coming on two different lines. Try this now:

    System.out.print("Hello, ");

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

Note that the first line is now just "print", and not "println". This tells the system not to move to the next line. This code will produce "Hello, World!" in one line.

That is our first little program….

Next Page

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License