"HelloWorld" in Java

 

To do these exercises, you must have the Java SDK (Software Development Kit) installed on your computer, and a development tool such as TextPad or WebSphere.  For instructions on downloading and installing the SDK and TextPad, click here.

The basic unit of coding in an object-oriented languages is the class.  This line of our "HelloWorld" program

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

identifies the class with the keyword class followed by the name of the class, HelloWorld, which must be the same as the name of the file.

The keyword public before class indicates that the code in the class can be called by code in other classes.  This will be explained in more detail later.  For now, just note that almost all classes must be labeled public.

 

The code inside the class is delimited by braces:

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

 

Inside the class, the line highlighted below identifies a method, which is similar to a procedure in COBOL.  The method below is named "main".  Like a class, a method is delimited with braces { }.

 Every Java application must have a procedure named main, because that is where execution  starts, and the main procedure must be marked with the keywords public and static.  Details of the meaning of both these keywords will be covered later, but for now, note that public here also means available to other classes.

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

 

 

Like COBOL procedures, Java methods can have parameters and return values.  COBOL passes parameters with USING.  Java passes parameters by placing them in parentheses after the name of the method.  The parameter for the main method is always String argv {}.  We will explain how it can be used later.

        public static void main ( String argv [ ] )

 

 

Also like COBOL procedure, Java methods can return a value.  The type of value to be returned is placed immediately before the name of the method.  In the case of main, the keyword void here indicates that the main method does not return any value -- it is void.

        public static void main ( String argv [ ] )

 

 

Inside the method is a command to write the string "Hello World".  Strings in Java are one or more characters surrounded by double quotes.

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

 

 

The command to print is System.out.println.  The command is built into Java, and like other built-in commands, it is stored in a namespace. In this case, the namespace is java.io.   Java requires that the name of the namespace be identified as the first line of the code, following the keyword import

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

 

 

All commands in Java must be terminated with a semicolon.  Lines which are just

    import java.io.*;

    public class HelloWorld

    {

        public static void main ( String argv [ ] )

        {

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

        }

    }

 

 

The Java language is case-sensitive, so main is not the same as Main or MAIN, and public cannot be written as Public or PUBLIC, etc.

However, Java is not generally space-sensitive.  Unlike COBOL, lines can be indented or not, and can start in any column.  For example, these 3 methods are all equally acceptable:

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

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

public static 
void main(String argv[]) { System.out.println("Hello")

; }

    

 

Exercise 1:

Use TextPad to write a Java application named "Exercise1" that prints this:

 

My name is:

<your name>

and I am learning Java.

 

For the solution code, click here.

 

 

Instead of using 3 different "System.out.println" lines, there is another way to place output on different lines.  You can use "\n" inside a string, and it will create a newline in the output.  For example:

    System.out.println("Hello\nWorld");

will output

    Hello

    World

 

Exercise 2:

Re-write the Java application from Exercise 1 using \n to create newlines. Rename your class "Exercise2".

For the solution code, click here.

 

 

For the next syntax lesson, click here.