1
class Customer {
    public String name;

    public Customer(String name) {
        this.name = name;
    }
}

public class Main {
    public static void main(String[] args) {
        Customer c = new Customer("Sally");
        System.out.println(c);
        System.out.println(c.name);
    }
}
  • It's a simple question and we all know the output, but there's one quesiton about it
  • Please see the code above and the Picture here Passing Value

  • Question:

    1. if I print out a reference of a object like this: System.out.println(c);, the IDE would generate a memory address for me: Customer@60e53b93
    2. But, in the video tutorial(screenshot, see the image link Passing Value) enter image description here

, the object have a member variable which is a reference to a String -> "Sally", why when I do System.out.println(c.name), trying to print out a reference(name), it give a real String object("Sally "). Shouldn't it print a memory address of the real Object("Sally")???

trincot
  • 317,000
  • 35
  • 244
  • 286
Reacher
  • 356
  • 4
  • 16

3 Answers3

4

First Question Answer:- 1.out is the static variable of PrintStream in System Class. Which is declared in System class as

   public final static PrintStream out = null;

And when you call System.out.println((Object)c); which internally call the println method of PrintStrem Class. The implementation of the println mehtod in PrintStream class is as below:-

   public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }
  public void print(Object obj) {
        write(String.valueOf(obj));
    }

By this code it is clear that out will call the String ValueOf method. and Implemetation of ValueOf() method in String Class as below:-

 public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

2.String class has Override the toString method of the Object Class as Below :-

    /**
     * This object (which is already a string!) is itself returned.
     *
     * @return  the string itself.
     */
    public String toString() {
       return this;
     }

The toString() Method in Object Class:-

  /**
   * Returns a string representation of the object. In general, the
   */
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Your class Customer didn't override the toString() method of Object class so when you Call System.out.println(c); it will call the toString method of Object Class. Thats y you will get Customer@60e53b93 as a output.enter image description here

Hasnain Ali Bohra
  • 2,130
  • 2
  • 11
  • 25
2

System.out is a object of class PrintStream. And PrintStream.println() calls String.valueOf() for objects. And String.ValueOf(object) calls object.toString(). And for a String the PrintStream.println() prints out the string. Where as for the Customer class you are defaulting to the Object.toString() causing to the print the Customer@ string.

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
1

Note: println() is a overloaded method in System class. When you call

    Customer c = new Customer("Sally");
    System.out.println(c);

This will call the println() method that take object as a parameter. If you refer documentation you will see this:

public void println(Object x)

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

Parameters: x - The Object to be printed.

When you call println(c.name), name is a String variable, this will call the println() method that take String as the parameter:

System.out.println(c.name);

In documentation:

println

public void println(String x)

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

Parameters: x - The String to be printed.

Also refer these: q1 and this q2.

Blasanka
  • 21,001
  • 12
  • 102
  • 104