Output in Java Programming
In this lesson, we will understand how to print outputs in Java programming using System.out.print() statement with the help of some examples.
Output in Java
The System.out.print() or System.out.println() statements are used in Java programs to print text and the values of variables on the console.
- System.out.print() - This statement is used to print text or values on the same line on the console. It does not move the cursor to the next line after printing.
- System.out.println() - This statement is used to print text or values, each on a new line on the console. It automatically moves to the cursor to the next line after printing.
In simple terms, a console is a screen or interface where you can see messages and type commands to interact with a computer.
Let's see some examples for more understanding.
Java program to print the text Welcome to Dremendo on the screen.
public class Example
{
public static void main(String[] args)
{
System.out.print("Welcome to Dremendo");
}
}
Welcome to Dremendo
Here, you can see that we have printed the text Welcome to Dremendo on the screen using the System.out.print statement.
Java program to print Sunday, Monday and Tuesday on separate lines on the screen.
public class Example
{
public static void main(String[] args)
{
System.out.println("Sunday");
System.out.println("Monday");
System.out.println("Tuesday");
}
}
Sunday Monday Tuesday
Here, you can see that we have printed the text Sunday, Monday, and Tuesday on separate lines on the screen using the System.out.println statement.
Java program to print Sunday, Monday and Tuesday on separate lines on the screen using System.out.print statement.
public class Example
{
public static void main(String[] args)
{
System.out.print("Sunday\n");
System.out.print("Monday\n");
System.out.print("Tuesday");
}
}
Sunday Monday Tuesday
The \n character is used in Java to insert a new line within a string. It stands for a newline character, which causes text to be printed on a new line. This can be used in conjunction with System.out.print to achieve similar results to System.out.println.
In the example above, \n is used to move the cursor to a new line after printing Sunday and Monday.
Java program to print integer data type values on the screen.
public class Example
{
public static void main(String[] args)
{
int a=5, b=26;
System.out.print("Value of a = " + a + " Value of b = " + b + "\n\n");
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
}
}
Value of a = 5 Value of b = 26 Value of a = 5 Value of b = 26
In the above program, we have printed two integer values in two different ways. First, on the same line with multiple spaces between them, and second, on separate lines. The + operator is used to concatenate (join) the string with the integer values to print them on the same line.
Java program to print float and double data type values on the screen.
public class Example
{
public static void main(String[] args)
{
float a = 5.237f;
double b = 26.14253678;
System.out.print("Value of a = " + a + " Value of b = " + b + "\n\n");
System.out.printf("Value of a = %.2f\n", a); // display the number upto 2 decimal places
System.out.printf("Value of b = %.4f\n", b); // display the number upto 4 decimal places
}
}
Value of a = 5.237 Value of b = 26.14253678 Value of a = 5.24 Value of b = 26.1425
Just like the previous example this time, we have printed a float and a double type values on the screen.
In Java, to control the number of digits after the decimal point when printing float or double values, we use format specifiers like %.2f or %.4f with System.out.printf() to display the values with 2 and 4 digits after the decimal point, respectively.
Java program to print a char data type value on the screen.
public class Example
{
public static void main(String[] args)
{
char a='#';
System.out.println(a);
}
}
#
This time we have printed a char data type value of the variable a on the screen.
Escape Sequence Character in Java
Escape sequence characters are those that does not represent itself when used inside a character constant or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
In Java, all escape sequences characters start with a backslash (\) followed by one or more characters that determine the interpretation of the escape sequence.
Common Escape Sequences
Here are some of the most common escape sequences in Java:
| Escape Sequence | Meaning | Description |
| \n | Newline | Move the cursor to the next line. |
| \t | Horizontal tab | Insert a horizontal tab space. |
| \\ | Backslash | Insert a single backslash. |
| \" | Double quote | Insert a double quote. |
| \' | Single quote | Insert a single quote. |
| \b | Backspace | Delete or overwrite the previous character by moving the cursor back one position. |
| \r | Carriage Return | Moves the cursor to the beginning of the line. It may overwrite the text that was there before. |
public class Example
{
public static void main(String[] args)
{
// Using various escape sequences
System.out.print("Hello World\n"); // Newline
System.out.print("Hello\tWorld\n"); // Horizontal tab
System.out.print("\\\n"); // Backslash
System.out.print("\"Hello\"\n"); // Double quote
System.out.print("\'Hello\'\n"); // Single quote
System.out.print("Hell\bo World\b\n"); // Backspace
System.out.print("Carriage return \rHello\n"); // Carriage return
}
}
Hello World Hello World \ "Hello" 'Hello' Helo World Helloage return
On Windows terminal, the \b escape sequence moves the cursor back one space but does not delete the character. Any characters printed after the backspace overwrite previously written characters.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.