-5

If the toString() method of a class is not overridden (or overloaded), and another method that returns a string is defined in the class, then what happens when I pass an instance of the class where a String is expected?

dylhunn
  • 989
  • 2
  • 8
  • 25
  • 1
    Try it. Is a simple and easy test. Otherwise read the documentation. – Davide Lorenzo MARINO Jul 04 '16 at 13:06
  • 1
    Seriously: do not write up questions for stuff that can be tested that easy. One of the core qualities of a good programmer is curiosity - the sort of curiosity that makes you **try** stuff. You learn programming by **doing** it, not by asking other people such basic things. – GhostCat Jul 04 '16 at 13:08
  • i'm in a hostel without a laptop and a computer with java compiler and i'm learning java out of curiostity out from a book – karthikeyan Jul 04 '16 at 13:10
  • @karthikeyan If you can log on to Stack Overflow, that means you must have Internet connection. So why not downlload JDK? – Sweeper Jul 04 '16 at 13:33

3 Answers3

1

If your class extends Object, you will get the result of Object#toString(), which is what will be called. If your class extends something else, it will get the first #toString in the inheritance path.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

Using the name of the class where a String is expected is not allowed. Consider the following:

class Foo {}

class Bar {
  public void baz(String a) {
    // Do something
  }

  public void binky() {
    baz(Foo); // Compiler error
  }
}

However, you could do the following:

class Foo {}

class Bar {
  public void baz(String a) {

  }

  public void binky() {
    Foo b = new Foo();
    baz(b.toString());
  }
}

Because the .toString() method of class Foo is not overridden, you will inherit the default .toString() from the Object class. This default behavior will print a string consisting of the name of the class of which the object is an instance, the at-sign character @', and the unsigned hexadecimal representation of the hash code of the object. (Thanks Kevin!)

dylhunn
  • 989
  • 2
  • 8
  • 25
  • `The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character @', and the unsigned hexadecimal representation of the hash code of the object.` It´s not a memory adress. from the documentation of `toString`. – SomeJavaGuy Jul 04 '16 at 13:17
0

Fun fact: It takes less time to test this than asking a question on Stack Overflow and getting an answer.

As you may know, every single class you create directly or indirectly inherits from the almighty Object class! If you don't override toString, calling toString will result in a call to the toString method of the nearest ancestor.

Suppose A inherits from B which inherits from C which inherits from D. Only C has overridden toString.

Calling toString on an object of...            will result in calling the toString of...
A                                             C
B                                             C
C                                             C
D                                             Object

I hope you understand that.

Actually, not only does the toString behave like this. Every method does this as well!

Now that you know Object.toString will be called. What does it do actually?

From this answer, we know that Object.toString outputs something like this:

FullyQualifiedNameOfYourClass@12345

FullyQualifiedNameOfYourClass basically means your class name, followed by the package that it is in e.g. com.example.MyClass.

12345 is the hashcode of the object, returned by hashCode() on the object.

So will Object.toString call one of your class' methods that returns a string, if you ever declared one?

Nope. Because it just doesn't. The documentation of toString clearly says that it just returns something like FullyQualifiedNameOfYourClass@12345 and nothing else.

Even if it did, it makes no sense. What if you declared two methods that return a string? Which one should it choose? Also, reflection takes time and will slow down the whole thing.

Community
  • 1
  • 1
Sweeper
  • 213,210
  • 22
  • 193
  • 313