Ad

Java Program - Rules

Some important points to remember before writing a Java program are:

  1. All Java code must be written inside a class.
  2. Java is a case-sensitive programming language, so be careful with small and capital letters.
  3. When saving a program, the file name must be the same as the main() class name, followed by the '.java' extension.
  4. Java program execution begins with the main() method, which is a mandatory part of every Java application.
  5. The return type of the main() method is always void.
  6. Class Name - For naming a class, the first letter should be in uppercase. If a class name consists of multiple words, the first letter of each subsequent word should also be uppercase (PascalCase). Example: MyFirstJavaClass
  7. Method Names - All method names should start with a lowercase letter.If a method name consists of multiple words, the first letter of each subsequent word should be uppercase (camelCase). Example: public void myMethodName().

Java Program

Java programs can be written in any text editor, such as Notepad, Notepad++, NetBeans, Eclipse, etc. However, before installing any editor, you must install the Java Virtual Machine (JVM). In this tutorial series, I will be using Visual Studio Code editor for writing programs.

Example 1: Write a Java first program to display the message "Hello World".

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

Save the program as "First.java", Open the 'Command Prompt', navigate to the directory where you saved the program, and type the following commands:

  • javac First.java - to compile the program.
  • java First - to execute the program.
Code Explanation:
  • class: class is a keyword used to declare a class. As previously stated, Java is a pure object-oriented programming language, therefore, all code must be placed within a class. Unlike C++, a class definition in Java does not end with a semicolon.
  • void main: This is similar to the main() function in C/C++. Every Java application must include the main() method. This method serves as the entry point for program execution. Java application can contain multiple classes, but only one main() method.
  • { }: Braces, or curly brackets, define the scope of a code block.
  • public: The keyword public is an access modifier that makes the main() method accessible from all other classes.
  • static: static is a keyword that declares the main() method as static. This means there is no need to create an object to invoke the static method. The main() method must always be declared as static because the interpreter uses it before any objects are created.
  • String args[]: This declares a parameter named 'args', which is an array of type String. String[] args is used to pass command-line arguments to the program.
  • System.out.println(): This method is used to print output to the console.
Valid Java main method signature
public static void main(String[] args)  
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String args[])
public static final void main(String[] args)
final public static void main(String[] args)
Invalid Java main method signature
public void main(String[] args)  
static void main(String[] args)  
public void static main(String[] args)  
abstract public static void main(String[] args)

Example 2: Find the Sum of 2 numbers.

class Sum
{
    public static void main( String args[] )
    {
        int a=10, b=20, c;
        c = a + b;
        System.out.println("Sum = " + c);
    }
}
Sum = 30

Having a semicolon at the end of a class in Java is optional.