Java Basic Program - Hello World

<<Previous

Next >>




Java programs are case sensitive. That is, classes, interfaces, variables, keywords or any type names in upper case is not the same as the names in lower case. For Example, Employee is different from employee or EMPLOYEE or eMployee.

Simplest Java Program

One of the simplest java programs is shown below:

		public class ClassName{
			public static void main(String s[]){
				System.out.println("My First Program");
			}
		}
	

Java File Naming Criteria

Java programs need to be saved in a file with:

  • File name having the same name as "public class" declared and
  • Extension as ".java"

Java Class Naming Criteria

In order for java class name to be valid and to compile correctly, the following criteria needs to be satisfied

  • class names can start with alphabets or underscore
  • class names should not start with digits but can contain digits
  • class names should not contain any special characters other than underscore

Java Class Naming Conventions

For Java class names, following conventions are followed in all Java programs and in all libraries but there is nothing that enforces these.

  • Use Upper case letters for each word in the class name
  • Use Nouns as ClassNames
  • Use complete meaningful names and avoid short forms
  • Avoid using Numbers in Class Names

Compiling a simple Java program

We can execute the following from the windows or Unix command line

javac ClassName.java

javac command compiles each of the java classes into a bytecode file with the ".class" extension

Executing a Java class with main() method

We can use the following command to execute a class

java ClassName

java command interprets the java class and runs the main() method defined within the ClassName

main() method

main() method should have the same signature as shown below. Variable name args can be any variable name following the naming rules and conventions

  public static void main(String args[]){

  }
 

main method should

  • always be public, static
  • should return void and
  • should accept a String[] argument

You can have main() methods with other signatures but those methods are regular methods and are not a starting point for execution.


<<Previous

Next >>