• This class is filer output Stream  class, It adds functionality to another output stream , namely the ability to print representation of various data values conveniently.
  • It is the most convenient class in writing data to another output stream, because of below three reasons.

  1. Unlike other output stream's write() method , write() method of this class never throws an IOException.
  2. Calling flush() method is optional, it is called automatically.
  3. In addition to default write() methods  it is defined new methods called print() and println(). These two methods are overloaded to print all types of java data type values including object. Below are the overloaded print() and println() methods list.
  1. public void print(int x);
  2. public void print(long x);
  3. public void print(float x);
  4. public void print(double x);
  5. public void print(char x);
  6. public void print(boolean x);
  7. public void print(char[] x);
  8. public void print(String x); 
  9. public void print(Object x); 
  1. public void println(int x);
  2. public void println(long x);
  3. public void println(float x);
  4. public void println(double x);
  5. public void println(char x);
  6. public void println(boolean x);
  7. public void println(char[] x);
  8. public void println(String x); 
  9. public void println(Object x);  
  10. public void println();
  • PrintStream class object is created in System class using a static variable called out.
  • So we can access above methods from our class using System class's PrintStream object as shown below.
  • System.out.print("abc");
  • System.out.println("abc");
  • System is predefined class in java.lang package.
  • out is static referenced variable of type PrintStream class. It is created in System class to hold PrintStream class object.
  • println() and print() methods are non static methods defined in PrintStream class to print data.

Difference between print() and println():

  • print() method places cursor in the same line after printing data, so that next coming output will be printed in the same line.
  • But println() method places cursor in the next line after printing data so that next coming output will be printed  in the next line.
What is the output of below program?

  1. package com.instanceofjavaforus;
  2.  
  3. public class PrintDemo{
  4.  
  5.  
  6.   public static void main(String[] args) {
  7.  
  8.        System.out.print("A");
  9.        System.out.println("B");
  10.        System.out.println('C');
  11. }
  12. }




  1. AB
  2. C


Rule #1:

  • We can not call print method without passing arguments because we do not have no argument print method so it leads to Compilation Error.
  • But we can call println() method without passing arguments because we have no argument println() method , it prints new line.
What is the output of below program?

  1. package com.instanceofjavaforus;
  2.  
  3. public class PrintDemo{
  4.  
  5.  
  6.   public static void main(String[] args) {
  7.  
  8.        System.out.println("A");
  9.        System.out.println();
  10.        System.out.println("B"); 
  11.        System.out.print();   // Compilation Error
  12.       System.out.println("B"); 
  13.  
  14. }
  15. }

Rule #2:

  • We cannot call print() or println() methods by passing null literal directly.
  • System.out.println(null);
  • It leads to Compilation Error : Ambiguous error. 
  • Compilation Error -This is because you can pass an Object or a String or char[]. Since null can fit in both, the compiler doesn't know which method to use, leading to compile error.
  • Method Overloading:
        1.public void prinltln(String str) { }
        2.public void prinltln(char[] ch){ }
        3.public void prinltln(Object ch){ }
What is the output of below program?

  1. package com.instanceofjavaforus;
  2.  
  3. public class PrintDemo{
  4.  
  5.  
  6.   public static void main(String[] args) {
  7.  
  8.        System.out.println(null); // Compilation Error
  9.        System.out.print(null); // Compilation Error
  10.      
  11.  
  12. }
  13. }

 

Difference between println(Object) and writeObject(Object) methods?

  • writeObject(Object) method serializes the given object and sends its state to underlying output stream. 
  • where as println(Object) method does not perform serialization instead it prints the passed object information that is returned by the toString() method.
  • It means println(Object) method internally calls toString() on given object to print the given object information .
  • If toString() method is not defined in the passed object's class it will call Object class toString() method which is originally defined.
  • toString() method in object class returns current object classname@hashcode in hexadecimal string format.

What is the output of below program?
  • toString() method is not overridden in Example class

  1. package com.instanceofjava;
  2.  
  3. public class Example{
  4.  
  5.   int x, int y;
  6.  
  7.   public static void main(String[] args) {
  8.  
  9.     Example obj = new Example();
  10.        obj.x=1;     
  11.        obj.y=2;
  12.        System.out.println(obj);
  13.       
  14. }
  15. }




  1. com.instanceofjava.Example@53601a4f



What is the output of below program?
  • toString() method is not overridden in Example class

  1. package com.instanceofjava;
  2.  
  3. public class Example{
  4.  
  5.   int x, int y;
  6.  
  7. public String toString(){
  8.    return "x="+x+" "+"y="+y;
  9. }
  10.  
  11.   public static void main(String[] args) {
  12.  
  13.     Example obj = new Example();
  14.      obj.x=1;
  15.      obj.y=2;

  16.        System.out.println(obj);
  17.       
  18. }
  19. }



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu