Java Output
Output Methods
System.out.println() is a method in Java or a statement that is used to display a message,
data, or string on the screen as the output. It displays the arguments that are passed to it.
System: This is the final class that is defined in java.lang package.
out: This is an instance of PrintStream type, and the access specifiers are final and public.
println(): This is a method in the PrintStream class.
Syntax :
System.out.println(argument); or
System.out.print(argument);
println() : Prints text or values to the console, followed by a new line
class Solution {
public static void main(String[] args)
{
System.out.println("You are");
System.out.println("Welcome to");
System.out.println("Java");
}
}
Output :
You are
Welcome to
Java
print() : The print() method does not insert a new line at the end of the output.
public class Main {
public static void main(String[] args) {
System.out.print("Hello Man! ");
System.out.print("I will print on the same line.");
}
}
Output :
Hello Man! I will print on the same line.
Print() vs println() :
print() - It prints string inside the quotes.
println() - It prints string inside the quotes similar like print() method. Then the cursor moves to the
beginning of the next line.
printf() : The printf() method outputs a formatted string.
Data from the additional arguments is formatted and written into placeholders in the
formatted string, which are marked by a % symbol. The way in which arguments are formatted
depends on the sequence of characters that follows the % symbol.
Placeholders
The placeholders have the form %[arg$][flags][width][.precision]conversion. The
components in [square brackets] are optional.
Example :
public class Main {
public static void main(String[] args) {
System.out.printf("Hello %s!", "Man");
}
}
Output : Hello Man!
Example :
public class Main {
public static void main(String[] args) {
System.out.printf("Hello %s! One kilobyte is %,d bytes.", "Man", 1024);
}
}
Output : Hello Man! One kilobyte is 1,024 bytes.
Example :
public class Main {
public static void main(String[] args) {
// Default
System.out.printf("%f%n", 123456.78);
// Two decimal digits
System.out.printf("%.2f%n", 123456.78);
// No decimal digits
System.out.printf("%.0f%n", 123456.78);
// No decimal digits but keep the decimal point
System.out.printf("%#.0f%n", 123456.78);
// Group digits
System.out.printf("%,.2f%n", 123456.78);
// Scientific notation with two digits of precision
System.out.printf("%.2e", 123456.78);
}
}
Output :
123456.780000
123456.78
123457
123457.
123,456.78
1.23e+05
Overloads of println() method
Method Overloading in Java allows us to make different methods to have the same name but
with different parameters or signatures where each signature is different by the number of input
parameters. With the use of println(), we know that this is a single method of PrintStream class with
which users can print the various type of elements by accepting different numbers and types of
parameters.
System.out.println()
System.out.println(character)
System.out.println(double)
System.out.println(int)
System.out.println(string)
Example :
class Solution {
public static void main(String[] args) {
/*Here, we Declare different data types*/
int num = 5;
char ch = 'T';
String str = "Welcome";
double d = 5.1;
float f = 10.2f;
boolean bool = false;
/*Different overloads of the println() method*/
System.out.println();
System.out.println(ch);
System.out.println(num);
System.out.println("CN");
System.out.println(str);
System.out.println(bool);
System.out.println(f);
System.out.println(d);
}
}
Output :
T
5
CN
Welcome
false
10.2
5.1
Input Methods
Input refers to text written by the user read by the program. Input is always read as a string.
For reading input, we use the Scanner tool that comes with Java. The tool can be imported for use in
a program by adding the command import java.util.Scanner; before the beginning of the main
program's frame (public class ...). The tool itself is created with Scanner scanner = new
Scanner(System.in);.
import java.util.Scanner; // import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
String userName;
// Enter username and press Enter
System.out.println("Enter username");
userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName);
}
}
Output :
Enter username
Man
Username is: Man
In the example above, we used the nextLine() method, which is used to read Strings. To read
other types, look at the table below:
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Example : nextLine() - String , nextInt() , nextDouble()
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter name, age and salary:");
// String input
String name = myObj.nextLine();
// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();
// Output input by user
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
Output :
Enter name, age and salary:
Man
25
6000
Name: Man
Age: 25
Salary: 6000