First Java Program And Input Output In Java

After Detailed Explanation of Java language and Classes and Object, We are going to explain the First Java Program and Basic Input Output in Java Language.

Hello World Program

Now I am going to explain the Very first basic program of Java i.e “Hello World”.

// This is a simple Java program.
class HelloWorld
{
    // Your program begins with a call to main().
    // Prints "Hello, World" to the terminal window.
    public static void main(String args[])
    {
        System.out.println("Hello, World");
    }
}

Output:

Hello, World

As you see the above code is consists of 3 main things, Class, Main Method, and some comments. We are going to explain each of the lines. Hope you know about class and objects. If you don’t know just read below.

Maybe if you are new You will face some problem but try to remember the Hello World program for a while and everything will be clear slowly-slowly

Class definition

The Keyword which is used to create the class is Class. The Class name always starts with the Capital Letter

class HelloWorld 

HelloWorld is the name of the class. You can also give any name because it is a identifier.

Inside the curly braces, the definition of the class is present which include constructors, Variables, methods etc.

Note: Class in Java never ends with semicolon after curly braces as in C++.

Main method

As we discussed in Java Page that Main method is compulsory so that JVM will execute it because JVM searches for main() method.

public static void main(String args[])

public

public is access specifier, there are three types of access specifier named as Public, private, and protected. but main method always started with public because JVM will search and execute it.

static

static is keyword which allow methods to be called without objects. JVM execute main method because of static keyword.

Void

This simply means that Main method does not return anything.

main()

Java developers make the main method compulsory because of JVM first searches for this method.

If you do not include main method you will face this error

Error: Main method not found in class, please define the main method as:
public static void main(String[] args)

strings[]

The main method accept the single argument which is string.

Note: If you do not include string array as argument you will face same error which arise when you did not include main method

Like in C/C++, main method is the entry point for your application and will subsequently invoke all the other methods required by your program.

Inside the curly braces after the main method, the body of main() is start

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

This line outputs the string “Hello, World” followed by a new line on the screen.

The built-in println( ) method is responsible for the Output.

The System is a predefined class that provides access to the system, and out is the variable of type output stream that is connected to the console.

Comments

/* This is a simple Java program. 
Call this file "HelloWorld.java". */

They can either be multi-line or single line comments same as in C/C++.

This is a multi-line comment. This type of comment must begin with /* and end with */.

For single line you may directly use // as in C/C++.

Input Output In Java

Now, we are going to learn the Input Output Library in Java

1.Using Buffered Reader Class[Java.io.BufferedReader]

Buffered Method is introduced in Java Version 1.0. In this we wrapped the System.in (Standard Input Stream) inside the InputStreamReader which is again wrapped inside the BufferedReader.

BufferedReader has two constructor

BufferedReader(Reader in)

It Creates a buffering character-input stream that uses a default-sized input buffer.

BufferedReader(Reader in, int sz)

It Creates a buffering character-input stream that uses an input buffer of the specified size.

Advantage

  • Input is sufficiently Buffered in BufferedReader for efficient reading

Disadvantage

  • Code is somehow hard to remember
  • No other methods to deal with Integers, float etc.

// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test 
{
    public static void main(String[] args) throws IOException 
    {
        //Enter data using BufferReader
        BufferedReader reader = 
                   new BufferedReader(new InputStreamReader(System.in));
       
       //Reading Integer
        int a = Integer.parseInt(reader.readLine());
 // Reading data using readLine
        String name = reader.readLine();

        // Printing the read line
        System.out.println(a);
        System.out.println(name);        
    }
}

Sample Code Link on BufferedReader.

There are many methods in BufferedReader which we discussed later.

we use functions like Integer.parseInt(), Double.parseDouble() to store other data types accordingly.

Using Scanner Class

This is the my one of the favourite and also most prefered method in Java for taking Input

The main feature of Scanner class that you will not need to use parse method for diffrent type of Input.

It has in built method to take Integer, float and others data types as input

It is the easiest way of taking input but not the efficient way.

Advantages:

  • In build methods for parsing primitives (nextInt(), nextFloat(), …) input.
  • To Find tokens you can use regular expressions.

Disadvantage

  • The reading methods are not synchronized

// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;

class GetInputFromUser
{
    public static void main(String args[])
    {
        // Using Scanner for Getting Input from User
        Scanner in = new Scanner(System.in);

        String s = in.nextLine();
        System.out.println("You entered string "+s);

        int a = in.nextInt();
        System.out.println("You entered integer "+a);

        float b = in.nextFloat();
        System.out.println("You entered float "+b);
    }
}

Here is the compiled code of Scanner class

Leave a Comment

Your email address will not be published. Required fields are marked *