Page 1 of 1
About Question enthuware.ocajp.i.v7.2.935 :
Posted: Sun Mar 02, 2014 11:04 pm
by fasty23
would you please explain more, How when a method is Not explicitly called, performed?
in this test the toString method was not called, but it was performed.
The explanation said :
"The method print()/println() of OutputStream takes an Object and prints out a
String that is returned by calling toString() on that object."
So because of using print() method, the toString was perform, How?
tnx
Re: About Question enthuware.ocajp.i.v7.2.935 :
Posted: Mon Mar 03, 2014 12:23 am
by admin
The code written inside the print/println methods of PrintStream calls String.valueOf(Object) ( see this
http://docs.oracle.com/javase/7/docs/ap ... ng.Object) )
and the code inside String.valueOf calls toString.
Re: About Question enthuware.ocajp.i.v7.2.935 :
Posted: Sun Sep 03, 2017 4:22 am
by shambhavi
Code: Select all
interface in
{
int a=9;
}
class sample implements in {
public static void main(String[] args) {
in s=new sample();
System.out.println(s);
Object o=s;
}
}
In the above code, s is an interface type. interfaces do not inherit from Objects right. Then how come the println() that takes an Object is able to take s as parameter and produce output without error.
Also, how can s be assigned to o ? at compile time it should complain right ?
is this another exception , since interfaces would anyways point to Objects ?
Re: About Question enthuware.ocajp.i.v7.2.935 :
Posted: Sun Sep 03, 2017 4:40 am
by admin
Yes, interfaces do not inherit from Object but you don't instantiate an interface. You always instantiate some or the other class and every class ultimately extends Object. Therefore, no matter what object points s points to, it will always be an instance of Object. That is why there is no issue with the println method.
Re: About Question enthuware.ocajp.i.v7.2.935 :
Posted: Mon Nov 30, 2020 6:40 am
by hrozhek
Explanation says that "Now, the other feature of print/println methods is that if they get null as input parameter, they print "null"". It's not correct in this wording, because when they get null as input parameter (not the parameter which points to null only at runtime), code will not compile with "Error: reference to println is ambiguous both method println(char[]) in java.io.PrintStream and method println(java.lang.String) in java.io.PrintStream match". And in case it will called with char[] or with null casted to char[], we'll get NPE.