











Input Output In Java


Java Basic Input and Output
To make I/O operations quick, Java uses the idea of a stream. All the classes needed for input and output operations are included in the java.io package.
Input Output Streams
- System.out: standard output stream
- System.in: standard input stream
- System.err: standard error stream
System.in:
System.out:
System.err:
This is the standard error stream, which is used to display any standard output device or the full set of error data that a program might produce.
Java Output
Syntax:
System.out.println(); or System.out.print(); or System.out.printf();
Difference between println(), print() and printf()
print():
Java uses this method to display text on the console. This method receives this text as a parameter in the form of a String. With this technique, the text is printed on the console and the cursor is left where it is at the console. From right here, the subsequent printing begins.
Example code :
// Java code for print() Statement import java.io.*; class Main { public static void main(String[] args) { System.out.print("prepinsta "); // Now all the print statement will System.out.print("prepinsta "); // print in the same line System.out.print("prepinsta "); } }
Output :
prepinsta prepinsta prepinsta
println():
In Java, a text can also be displayed on the console using this method. The text is printed on the console, and the cursor is then moved to the beginning of the subsequent line. From the following line, the next printing begins.
Example code :
// Java code for println() Statement import java.io.*; class Main { public static void main(String[] args) { System.out.println("prepinsta "); // Now all the println statement will System.out.println("prepinsta "); // print in the next line System.out.println("prepinsta "); } }
Output :
prepinsta prepinsta prepinsta
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
printf():
As it is similar to printf in C, this method is the simplest of all. It should be noted that while printf() may accept multiple arguments, System.out.print() and System.out.println() only accept one argument each. The output in Java is formatted using this.Example code :
// Java code for print() Statement import java.io.*; class JavaFormat { public static void main(String args[]) { String data = "Hello World!"; System.out.printf("Printing a string: %s\n", data); } }
Output :
Printing a string: Hello World!
Java Input
Example code :
import java.util.Scanner; //importing of Scanner class public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); //object creation of scanner class System.out.print("Enter an integer number: "); int number = input.nextInt(); //Taking input from the user System.out.println("You have entered " + number); input.close(); // closing the scanner object } }
Output :
Enter an integer number: 23 You have entered 23
Login/Signup to comment