Java write output to console in java means writing the output of java program. Which refers to the console or any particular file. Methods that are used for the streaming output can also be defined in the PrintStream class. The different methods that can be used for writing console outputs can also be print(), println(), write().

Also Read: Questions on python programming: Top 11

Methods of writing Java write output to console in Java-print() and println()

Java write output to console

The pair of println() and print() also direct the output to console. PrintStream class is where these methods are defined and widely used. The pair of these methods can be used with the help of System.out.stream.

The Basic Difference between Print() and println() methods are as follows:

  • Print() In this method the string is displayed in the same line. Whereas in Println() the method outputs a newline character after the execution.
  • Println() is also used to direct output to consoles and other sources, but Print() can only be used to direct output to the console.

Let us give you an example to explain this situation a little better:

Example of print() method:

// Sample program to display numbers from 1-10 using print method
class printEg
{
            public static void main(String args[])
            {
                        int a;
                        for (a=1 ;  a<=10 ;  a++)
                        {
                                    System.out.print(a);
                        }
            }
}

OutPut of the program given above:

Java write output to console

Now let us give you a example of Println() method:

We will take the example given above and just change the print() function with the println() function method.

// Sample program to display numbers from 1-10 using println method
class printlnEg
{
            public static void main(String args[])
            {
                        int a;
                        for (a=1 ; a<=10 ; a++)
                        {
                                  System.out.println(a);
                        }
            }
}

OutPut of the program given above:

Java write output to console

You can clearly observe the difference between Print() and Println(). The pair of these print() and println() both works best for string outputs. Lets jump into another example to understand these things a little better.

Displaying string using print() and println() methods:

As you can see it is very simple to display strings using print() and println() methods. Now if you have to display any particular string then the string to be displayed on the screen is written within the double quotation marks (i.e. ” “).

Let us also give you an example of the situation we have given above  System.out.println(“Hola!! What’s up?”).

Now if you want something extra to display other than the text given above. Then you have to append the variable name along with the string with a (+) symbol.

Writing Console Output in Java

Example for displaying any string output:

// Sample program to display sum of numbers from 1-10
class Eg1
{
            public static void main(String args[])
            {
                        int a, total = 0;
                        for (a=1 ; a<=10 ; a++)
                        {
                                    total = total + a;
                        }
                        System.out.println("Sum of first 10 numbers is: " + total);
            }
}

Output for the above program:

string output

Let us see the Write() method:

If you want an alternate option then you can also the write() method for directing the output of your program to the console. The easiest syntax of the write() method is:

void write(int b);

Here b is an integer of low order eight bits.

So, here is an example to explain you things a little better.

class writeEg
{
            public static void main(String args[])
            {
                        int a, b;
                        a = 'Q';
                        b = 65;
                        System.out.write(a);
                        System.out.write('\n');
                        System.out.write(b);
                        System.out.write('\n');
            }
}

Output of the code given above:

Conclusion:

Java write output to console is a method of writing a java program output. And how different methods are used for writing console. Different outputs like print(), println() and write(). Here is everything you need to know and Hope you find this information useful. Thank you for the read.

Categorized in:

Tagged in: