JAVA Input and Output

Input - Output in JAVA is done through various streams and I/O packages provides by JAVA. These streams supports all kinds of objects , data-types , character , files etc so that it can easily perform Input -Output operation on them.

JAVA Input and Output

In this tutorial, we will be covering everything you need to know about Input and Output operator in JAVA. It is very important to learn about taking user input and to display it. It is something that every programmer must learn while learning any programming language.

Before exploring various Input-Output streams , lets see a little about 3 default streams in JAVA:

  1. System.in
  2. System.out
  3. System.err

System.in: It is standard Input stream which is used to read the user input given through keyboard or any other input device. It is mostly used input streams such as DataInputStream , Scanner etc. (we will read about all these streams in our next blog)

System.out: It is the standard Output stream which is used to display the data inputted by the user on the screen. Below is the list of various print functions we use with output stream along with examples.

  1. print() : This method is used to display text or result on the screen.The input is in form of string and the cursor remains at the end of the text. It does not take the control to the next line.
    Syntax:
System.out.print(text/message/parameter);

       Example:

// code to illustrate print statement in JAVA
import java.io.*;
class sample
{
public static void main(String args[])
{
System.out.print("DigitalDealSolution");
System.out.print("DigitalDealSolution");
System.out.print("DigitalDealSolution");
}
}

Output:

DigitalDealSolution DigitalDealSolution DigitalDealSolution 

   b. println() : This method is used to display text or result on the screen. It prints the text or message and then transfers the cursor to the next line. It transfers the control to the next line for printing.

Syntax:

System.out.println

Example:

//  java code to illustrate println method
import java.io.*;
class sample
{
public static void main(String args[])
{
System.out.println("DigitalDealSolution");
System.out.println("DigitalDealSolution");
System.out.println("DigitalDealSolution");
}
}

Output:

DigitalDealSolution
DigitalDealSolution
DigitalDealSolution

c. printf :  This method is used to format the output in JAVA. It can take multiple arguments whereas println() and print() takes single argument only.

Syntax:

System.out.printf("Format with"+"precision : PI = %.3f\n",Math.PI);

System.err : It is the standard error stream which is used to display all the error data that the code will throw during program execution. It also uses the below mentioned to output error:

  1. print()
  2. println()
  3. printf()

In this tutorial we have discussed about standard streams used for input-output. In the upcoming tutorial we will discuss about different type of streams depenfing on type of operation. We wish you best of luck for future tutorials and if you have any doubt you can ask in the comment box below.

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0