-2

I'm a beginner and I am not able to understand how println() function actually works. When I pass primitive variable to it . It prints value of it. When I pass an object why does it print default toString() method definition? i.e.

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

So when I am trying to print out an array.

 System.out.println(Arrays.toString(array))

Am I overriding default method definition of toString of Object class or just converting Array to String using toString() method of Arrays class.

import java.util.Arrays;


public class RevString 
{
    public static void main(String[] args)
    {
        String str = "abcde";
        int x=2;
        String[] e =str.split("---");
        System.out.println(e);
        System.out.println(Arrays.toString(e)+ " " +"Primitive is " + " " + x);

    }

}
Javasee
  • 13
  • 3
  • 1
    It does use `toString()` method. Unless it's overridden somewhere in the inheritance chain, the one from `Object` will be used. – PM 77-1 Jul 12 '14 at 21:59
  • Possible duplicate of [How to support println in a class?](http://stackoverflow.com/questions/27647567/how-to-support-println-in-a-class) – Raedwald Mar 26 '16 at 14:13

4 Answers4

3

toString() method if not overridden, will inherit from the object class. so you need to override it it in your class to be capable of showing the members o your object.

Current IDEs have this option automatic, so you just right click and generate toString method with the class members

look at this.

GingerHead
  • 8,130
  • 15
  • 59
  • 93
2

When I pass an object why does it print default toString() method definition?

Because it's defined in the Java specification Chapter 5 § 1.11:

Any type may be converted to type String by string conversion.

A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression (§15.9):

  • If T is boolean, then use new Boolean(x).

  • If T is char, then use new Character(x).

  • If T is byte, short, or int, then use new Integer(x).

  • If T is long, then use new Long(x).

  • If T is float, then use new Float(x).

  • If T is double, then use new Double(x).

This reference value is then converted to type String by string conversion.

Now only reference values need to be considered:

  • If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

  • Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

Community
  • 1
  • 1
MrLore
  • 3,759
  • 2
  • 28
  • 36
0

.toString takes the variable's value and changes it to a String. I do not believe it actually changes the libraries definition. This may help: Override valueof() and toString() in Java enum

Community
  • 1
  • 1
NebulaeGuy
  • 169
  • 9
0

There is actually a println() method for every primitive and one for Objects. Primitives are printed as String.valueOf('primitive') whereas Objects are printed as 'object'.toString()

succcubbus
  • 874
  • 1
  • 7
  • 24