Java Basic Programs

In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list.

To execute a Java program, one has to:

  • Install JDK
  • Set the jdk/bin path in the PATH environment variable.

Before executing any Java program, ensure the above two points without installing JDK execution of Java program is not possible.

Let’s create a Hello-World Java program.

Filename: HelloWorldExample.java

Output:

Explanation: Everything in Java should be written inside a class. Otherwise, the compiler will punish. In the above code snippet, we have used the following keywords. Let’s discuss each of them.

class: It is a keyword in Java that is used to define a class. The class encapsulates the whole code into itself. The class keyword is always followed by the class name. In our case, the class name is HelloWorldExample.

public: It is an access specifier which tells that the class is accessible by any other class available in the Java universe.

static: It is a keyword ensures that the main method is a class method. A class method is a method that can be called with the help of the class name.

void: It is also a keyword. It denotes nothing. If a method has the keyword void it means nothing is returned by the method.

public static void main(String[] args): It is the entry point of any Java program. A program can never have more than one entry point. The main method is invoked by the JVM.

It is mandatory to declare the main() method as public. Otherwise, the program won’t compile. This is because any other specifier (such as private, protected, etc.) reduces the visibility of the main() method, and the JVM cannot find it. If we make the main() method private or protected, the JVM shows an Error:Main method not found in class FactorialProgramExample, please define the main method as: public static void main(String[] args).

The name of the file containing the above source code must match with the class name containing the public access specifier, and the extension of the file should be .java. Thus, the complete file name would be: HelloWorldExample.java

After compiling the HelloWorldExample.java file, we get a .class file, namely, HelloWorldExample.class. A Java .class file contains Java bytecode. The .class file is then executed by JVM to generate the output.

Note: The number of .class files will always be equal to number of classes present in the .java file.

System.out.println() is the Java statement that prints whatever we passed as an argument. Here, the class name is System, out is the object of the PrintStream class and println() is the method of the PrintStream class. The last letters ‘ln’ of the println() method is for the new line. It means after printing the argument, the cursor jumps to the next line. In the above code, if we add one more println-statement like the following:

We will get the output as:

If we remove the ‘ln’ from the first println-statement, we will get:

Hello-WorldHello-World

To compile a .java file we use the javac command and to run it we use java command. Thus,

For compilation: javac HelloWorldExample.java

For execution: java HelloWorldExample

In the main method, String[] args is the command-line argument. The command-line argument is an array of the type String. Its value can be set in the command prompt when we execute the Java program. Thus, another way to print Hello-World will be:

FileName: HelloWorldExample1.java

Output:

At the execution time, we have passed the command line-argument Hello-World. Then, in the code, we are iterating over the String array args to print the Hello-World.

Similarly, we can also write other basic Java programs such as:

  • Fibonacci Series in Java
  • Prime Number Program in Java
  • Palindrome Program in Java
  • Factorial Program in Java
  • Armstrong Number in Java
  • How to Generate Random Number in Java
  • How to Print Pattern in Java
  • How to Compare Two Objects in Java
  • How to Create Object in Java
  • How to Print ASCII Value in Java

We will cover the above Java programs one by one in the coming section.

Pin It on Pinterest

Share This