System.out.println() - How does it works in Java


System.out.println()

Some Points you can infer from statement-
System - is a Java class - 'S' is in caps signifies it
println() - method signature can be called by class name if static, if not then by a reference of class variable
out - Not a class signature, so must be instance reference variable, so println is a class method not static

in actual -
System - java class under java.lang package

out - reference variable of PrintStream  class
 public final static PrintStream out = null;

println() - overloaded method in PrintStream Class accepts -- all literals string object etc

Some Ques-
1-System.out.println(null); 
    compile time error because of ambiguity of method which method to call many full-fill
2-
char[] chrarr = null;
System.out.println(chrarr); 
   Run time Null pointer exception


3- A a = new A();
System.out.println(a); 
    toString method of A is called


4- String s = "null";
    System.out.println(s);
This will print null

5- String s = null;
    System.out.println(s);
This will print null


6-System.out.println(1); -- this will print 1



Comments