title: toString Method Call in Java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8555771/why-is-the-tostring-method-being-called-when-i-print-an-object">Why is the toString() method being called when I print an object?</a>  </p>
  </blockquote>
  
  
  
  <p>I have this piece of code below. I understand everything else except the output using the <strong>toString method in the Room Class</strong> . In the <strong>HotelMain</strong> Class, I just called the <strong>displayRooms Method</strong> that was in the <strong>Hotel Class</strong>. But, when I ran the program, the toString output was shown in the console.</p>
  
  <p>If I'm right toString() is the textual representation of the value in the object. But, I'm not sure where I called the toString method.</p>
  
  <p>Can someone solve my dilemma? Thank You.</p>
  
  <p><strong>Hotel Class</strong></p>
  
  <pre><code>public class Hotel {
  
  private String hotelName;
  private Room[] rooms;
  
  public Hotel(String hotelName, int numberOfRooms) {
      this.hotelName = hotelName;
      this.rooms = new Room[numberOfRooms];
  }
  
  public void addRooms(Room room) {
  
      int position = -1;
  
      for (int i = 0; i &lt; this.rooms.length; i++) {
          Room tempRoom = rooms[i];
  
          if (tempRoom == null) {
              position = i;
              break;
          }
      }
  
      if (position != -1) {
          rooms[position] = room;
          System.out.println("New room added at postion " + position);
  
      } else {
          System.out.println("Addition of room failed.");
      }
  }
  
  public void displayRooms() {
  
      System.out.println("The Hotel: " + this.hotelName + " has the following rooms.");
  
      for (int i = 0; i &lt; this.rooms.length; i++) {
          Room tempRoom = rooms[i];
          if (tempRoom != null) {
              System.out.println(tempRoom);
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Room Class</strong></p>
  
  <pre><code>public class Room {
  
  private int roomNumber;
  private int numberOfBeds;
  private boolean smokingOrNonSmoking;
  
  public Room() {
  
  }
  
  public Room(int roomNumber, int numberOfBeds, boolean smokingOrNonSmoking) {
      this.roomNumber = roomNumber;
      this.numberOfBeds = numberOfBeds;
      this.smokingOrNonSmoking = smokingOrNonSmoking;
  }
  
  @Override
  public String toString() {
      return "Room [roomNumber=" + roomNumber + ", numberOfBeds="
              + numberOfBeds + ", smokingOrNonSmoking=" + smokingOrNonSmoking
              + "]";
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Hotel Main</strong></p>
  
  <pre><code>public class HotelMain {
  
  public static void main(String[] args) {
  
  Hotel hotel = new Hotel("MahaRani Chain of Hotels", 10);
  Room room1 = new Room(4, 2, true);
  Room room2 = new Room(2, 1, false);
  Room room3 = new Room(6, 3, true);
  Room room4 = new Room(6, 4, false);
  
  hotel.addRooms(room1);
  hotel.addRooms(room3);
  hotel.addRooms(room4);
  hotel.addRooms(room2);
  
  hotel.displayRooms();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Console</strong></p>
  
  <p><img src="https://i.stack.imgur.com/FsFqD.png" alt="enter image description here"></p>