- 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.
- Unlike other output stream's write() method , write() method of this class never throws an IOException.
- Calling flush() method is optional, it is called automatically.
- 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.
- public void print(int x);
- public void print(long x);
- public void print(float x);
- public void print(double x);
- public void print(char x);
- public void print(boolean x);
- public void print(char[] x);
- public void print(String x);
- public void print(Object x);
- public void println(int x);
- public void println(long x);
- public void println(float x);
- public void println(double x);
- public void println(char x);
- public void println(boolean x);
- public void println(char[] x);
- public void println(String x);
- public void println(Object x);
- 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.
- package com.instanceofjavaforus;
- public class PrintDemo{
- public static void main(String[] args) {
- System.out.print("A");
- System.out.println("B");
- System.out.println('C');
- }
- }
- AB
- 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.
- package com.instanceofjavaforus;
- public class PrintDemo{
- public static void main(String[] args) {
- System.out.println("A");
- System.out.println();
- System.out.println("B");
- System.out.print(); // Compilation Error
- System.out.println("B");
- }
- }
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){ }
- package com.instanceofjavaforus;
- public class PrintDemo{
- public static void main(String[] args) {
- System.out.println(null); // Compilation Error
- System.out.print(null); // Compilation Error
- }
- }
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
- package com.instanceofjava;
- public class Example{
- int x, int y;
- public static void main(String[] args) {
- Example obj = new Example();
- obj.x=1;
- obj.y=2;
- System.out.println(obj);
- }
- }
- com.instanceofjava.Example@53601a4f
What is the output of below program?
- toString() method is not overridden in Example class
- package com.instanceofjava;
- public class Example{
- int x, int y;
- public String toString(){
- return "x="+x+" "+"y="+y;
- }
- public static void main(String[] args) {
- Example obj = new Example();
- obj.x=1;
- obj.y=2;
- System.out.println(obj);
- }
- }
- x=1 y=2
You might like:
No comments