1

We can print an array easily using System.out.println(though array is also a kind of object in java) but why we can't print an object directly without override toString() method?

For example:

int array[]=new int(2);
array[0]=10;
System.out.println(array[0]); //it works
Object obj=new Object();
System.out.println(obj); //it shows the classname and some value i don't know
user9999
  • 113
  • 2
  • 10
SHB
  • 585
  • 6
  • 14
  • 2
    What other information is there in that object to show??? Even if there was information, how will the JVM know which information to show??? – Codebender Jul 05 '15 at 10:48
  • Yes you are right...we can't override toString of any class without subclassing them.I did mistake in my question but my goal was to learn why we can easily print the value of an array(though in java array is one kind of object)?And in general if an object contains any value then we can't print the values of that object directly? I am a novice java learner i might have been mistaken to question and sorry for that.... – SHB Jul 05 '15 at 11:06

5 Answers5

1

"it shows the classname and some value i don't know"

Well, that's how an Object instance is printed, and for instances of Object class (i.e. not sub-classes of Object) you can't override toString.

For your custom sub-classes of Object, you must decide yourself how you wish the String representation of the object to look like (by overriding toString). Java doesn't decide it for you.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • We can't override toString (or any other method) of any class without subclassing them.. Can we?? – Codebender Jul 05 '15 at 10:51
  • @user2703995 In your example, it's the value of an element in a primitive array. Java handles printing of primitive types, since you can't handle it yourself (there's no `toString` method to override for primitives). – Eran Jul 05 '15 at 10:54
  • @Codebender Yes, I mentioned Object class in particular since the OP's code creates an instance of `Object`. – Eran Jul 05 '15 at 10:55
1

What information would you expect out of an object.The main info it has is its class name and its hashCode(), which is the info you get when you do System.out.println(obj);. This is why if we want some specific info about the object to be printed we override the toString() of Object class in our own class. The default behaviour of toString() is to print Object class name @ hex representation of hashCode().

PS :- Also array are treated as Object , they also give you the hashCode() when printed, except for char[] , which is because println(char[]) has functionality for printing its content unlike other arrays like int[], byte[] etc.

AnkeyNigam
  • 2,810
  • 4
  • 15
  • 23
0

The print array example that you used only prints an int. You are requesting the value at index 0, then printing the returned int to the console. Your example is the equivalent of creating your own object and printing the result of a method you called. For example:

class A {
    public String getValue() 
        return "my text";
    }
}

A a = new A();
system.out.println(a.getValue());

To answer your question, system.out.println is a method that will take an object and use a standard method to pull the text from the given object. If Object didn't do this, you would still be required to create your own toString() equivalent method in order to print out information from an object.

If you would rather have toString() print out all of the information contained in an object, you would end up getting a bunch of really useless information.

0

Object is the super class of every other classes in java.It contains a method hashCode() which returns a 32-bit integer value whose value is dependent on the contents of the instance of the class. Also it contains another method toString() which returns a string representation of the object. The default implementation of toString() method returns the hashcode prefixed with the classname ...i.e as given below :
classname@hashCode
The string representation of object is usually printed when we pass the reference of an object to System.out.println().
We can change the string representation of the instance by overriding the toString() method in the class of the instance.

Akhil Job
  • 419
  • 6
  • 18
0

If you are looking for content of instances of 3rd party classes, or your classes but don't want to override toString, you are probably doing that for some debug purposes. Of course you can do that without modifying anything. You need to use Reflection and define some static utility toString method that would iterate all fields.

It's strange why that's omitted in accepted answer. You can use this functionality while overriding in your classes also, the difference for 3rd party classes is that you have to explicitly ignore non public access modifiers.
Get a look at this answered question: Printing all variables value from a class

user9999
  • 113
  • 2
  • 10