Writing Console Output in Java refers to writing the output of a Java program to the console or any particular file.
The methods used for streaming output are defined in the PrintStream class. The methods used for writing console output are print(), println() and write().
Methods of Writing Console Output in Java-print() and println()
Both print() and println() methods are used to direct the output to the console. These methods are defined in the PrintStream class and are widely used. Both these methods are used with the help of the System.out stream.
The basic differences between print() and println() methods are as follows:
- print() method displays the string in the same line whereas println() method outputs a newline character after its execution.
- print() method is used for directing output to console only whereas println() method is used for directing output to not only console but other sources also.
Now, let us understand it in a better way with the help of some examples.
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:
Example of println() method:
Let us take the same example above and use println() method in place of print() 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:
Observe the difference in the output.
Both these functions work best for output of strings as well. Let us have a look at another example.
Displaying strings using print() and println() methods
It is very easy to display strings using print() and println() methods. For displaying any particular string, the string to be displayed on screen is written within the double quotation marks (i.e. “ “).
For example: System.out.println(“Hello!! How are you?”);
In addition, if you want to display the value of any particular variable used in the program, then you have to append the variable name along with the string with a plus (+) symbol.
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:
The write() method
Alternatively, you can make use of 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);
Where b is an integer of low order eight bits.
Let us have a look at a simple example.
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:
You wonder: “Can I pay someone to do my Java homework for me? – You are not alone, coding experts from AssignmentCore will help with your Java assignments online.