Dremendo Tag Line

Output in Java Programming

Input / Output in Java

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.println() or System.out.print() statement is used in the Java program to print text as well as the value of the variables on the screen.

Here System is a class name, out is an object of PrintStream class which is a public and static member field of the System class and println() or print() is a public method of the PrintStream class which can be accessed by the object out.

  • System.out.println - This statement is used to print text or values in separate lines on the screen. This statement breaks the line after printing text or values on the screen.
  • System.out.print - This statement is used to print text or values in the same line on the screen. This statement does not break the line after printing text or values on the screen.

Let's see some examples for more understanding.

Example 1

Java program to print Hello World text on the screen.

public class Example
{
    public static void main(String args[])
    {
        System.out.print("Hello World");
    }
}

Output

Hello World

Here you can see we have printed the text Hello World on the screen using System.out.print statement.

Example 2

Java program to print Sunday, Monday, Tuesday and Wednesday in 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\n\n");
        System.out.println("Wednesday");
    }
}

Output

Sunday
Monday
Tuesday


Wednesday

Here you can see we have printed the text Sunday, Monday, Tuesday and Wednesday in separate lines on the screen using System.out.println statement. You can also see that on line number 7 we have used \n (escape sequence character for new line) to break lines twice after printing Tuesday on the screen. You can use more than one \n inside print or println method if you want to break more lines as shown in the example above.

Example 3

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.println("a=" + a + " b=" + b + "\n\n");
        System.out.println("a=" + a);
        System.out.println("b=" + b);
    }
}

Output

a=5 b=26

a=5
b=26

In the above program, we have printed two integer data type values in two different ways. First, in the same line with a space between them and second, on separate lines. We have used + on line number 6 to print the outputs in a single line and after that break the line twice using \n.

Example 4

Java program to print float data type values on the screen.

public class Example
{
    public static void main(String args[])
    {
        float a=5.23f, b=26.14f;
        System.out.println("a=" + a + " b=" + b);
    }
}

Output

a=5.23 b=26.14

In the above program, we have printed two float data type values with a space between them in the same line on the screen using + operator. We have used f at last in the numbers to convert it into float data type.

Example 5

Java program to print double data type values on the screen.

public class Example
{
    public static void main(String args[])
    {
        double a=18.413, b=57.273;
        System.out.println("a=" + a + " b=" + b);
    }
}

Output

a=18.413 b=57.273

Just like the previous example this time, we have printed two double data type values with a space between them in the same line on the screen using + operator.

Example 6

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);
    }
}

Output

#

This time we have printed a char data type value of the variable a on the screen.

Escape Sequence Character in Java

An escape sequence is a sequence of characters that does not represent itself when used inside a character 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 consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence.

Escape Sequence Character Description
\n Insert a linefeed (new line)
\r Insert a carriage return
\f Insert a form feed
\b Insert a backspace
\t Insert a tab
\\ Insert a backslash
\" Insert a double quote
\' Insert a single quote
video-poster

Test Your Knowledge


You Scored :

Percentage :


Loading