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
About Question enthuware.ocajp.i.v7.2.935 :
Moderator: admin
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.935 :
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.
and the code inside String.valueOf calls toString.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.935 :
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 ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.935 :
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.
-
- Posts: 2
- Joined: Mon Nov 30, 2020 6:32 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.935 :
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.
Who is online
Users browsing this forum: Bing [Bot] and 6 guests