Java Hello world

Hello world in Java

//Hello.java
class Hello {
  public static void main(String args[]) {
    System.out.println("Hello World !!! Welcome to Java");
  }
}

Explanation

In Java, Every java program is written with the help of the class. The class is created as given in the code snippet below...

class Hello {
    //class code written here
}

A class is created by using class keyword followed by a name of the class.

A class can contain various methods(functions). In the above example main() is a method.

A method is the collection of statements that performs a specific task. A method can be called (used) when you want to perform the task.

Every java program has a starting point/method which is called when you run your program from the command line and the starting point is the main() method. As you run your program java interpreter starts execution from the main() method. The other methods are called directly or indirectly from the main() method. The main method is written as given below...

public static void main(String args[]) {
    //Method code
}

main() is the method name that receives an argument(Input) of string type array. We will discuss in further lessons why an array of a string is used as an argument in the main() method.

public, static, and void are the keywords in java which is used with the main() method.

The keyword void is the return type of main() method which indicates that the method returns nothing.

public indicate that method is accessible from anywhere outside the class.

static indicates that the method can be called without an object.

All keywords in java are written in lowercase.

Note: If you don't understand the meaning of the public, static, and void keyword. Don't worry we will discuss all these with great detail in upcoming lessons.

class and method names in Java

Observe Hello,String, and System the above example, Both are the class names in Java and their first letter is in uppercase.

In Java, class names are written in a camel-case in which the first letter of each word is written in uppercase as shown in the following figure.

Camel case class name

In java, the main() is a method name. A method name is also written in the camel case but the name starts with a lowercase letter then the first letter of each word is written in uppercase as shown in the following figure...

camel-case-method-name

Compile a java program

To compile a java program invoke the javac(java compiler) from the command line. Complete steps to run the javac command are listed below...

  1. Open Command prompt.
  2. Switch to the directory where you saved your program. For example, if your program is stored in d:\java\basic\Hello.java path. Then go to the specified path by using the following commands.
  3. d: Enter => cd java\basic Enter
  4. To compile your program type javac Hello.java.
  5. If your program is successfully compiled then bytecode(Hello.class) of the program is generated.

Important Points

  1. Generally, a program is saved with the same name as the class name, but it is not mandatory.
  2. If your program is saved with the other name then javac accepts the program name. For example, if the above program is saved with p1.java then to compile the above program command should be javac p1.java.
  3. After successful compilation, the compiler generates a bytecode.
  4. bytecode has the same name as the class name even program name can be anything.
  5. A java program can contain multiple classes then the compiler generates a separate bytecode for each class with the same name of the class irrespective of the program name. For example, if a program has two classes, class A {} and class B{} the after compile A.class and B.class will be generated.

Running a java program

To run a java program, java command is used which invokes java interpreter and starts executing your program started from the main() method. Follow the steps to run your java program...

  1. java Hello Enter will start executing the above program. Here Hello is the bytecode name that is generated by the compiler but no need to use .class extension when running bytecode.

Important points

  1. If your program contains multiple classes then multiple bytecodes are generated.
  2. Each class has its own bytecode but all bytecode can't start execution from the command line.
  3. The interpreter start execution from the main() method, so only bytecode which has the main() method should be used with the java command-line tool.