0

I'm currently reading the book "Big Java Early Objects".

In the section "construction objects in Java" They gave this example below.

import java.awt.Rectangle;

/**
   This example demonstrates constructors.
*/
public class ConstructorDemo
{
   public static void main(String[] args)
   {
      // Constructs and prints a rectangle
      System.out.println(new Rectangle(5, 10, 20, 30));

      // Constructs a rectangle and saves it in a variable
      Rectangle box = new Rectangle(5, 10, 20, 30);
      System.out.print("box: ");
      System.out.println(box);

      // The constructor with no arguments
      box = new Rectangle();
      System.out.print("box: ");
      System.out.println(box);
   }
}

I think this code should print out a Rectangle. But nothing happens, it only prints out this in the Console:

java.awt.Rectangle[x=5,y=10,width=20,height=30]
box: java.awt.Rectangle[x=5,y=10,width=20,height=30]
box: java.awt.Rectangle[x=0,y=0,width=0,height=0]

Is something wrong with my Eclipse? How can I make this work?

7
  • 7
    Why would it print out a rectangle? What do you mean by print out a rectangle? Commented Mar 13, 2014 at 19:15
  • 3
    It won't draw a rectangle. Commented Mar 13, 2014 at 19:16
  • 1
    It is calling Rectangle#toString method, and that String is being printed in the console. Commented Mar 13, 2014 at 19:16
  • It should draw a rectangle on the screen, and it should also color it to your liking :) Commented Mar 13, 2014 at 19:18
  • I would re-read that section of the book to understand what the code is intended to demonstrate. That code will most definitely NOT "draw" anything on the screen. Commented Mar 13, 2014 at 19:23

2 Answers 2

2

If you look at the code for Rectangle.toString(), which is called automatically by System.out.println, you will see that it prints out the parameters that make up the Rectangle object. However, it will not draw it:

return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";

You're not explicitly calling toString(), but impliclty you are:

See:

System.out.println(foo); // foo is a non primitive variable

System is a class, with a static field out, of type PrintStream. So you're calling the println(Object) method of a PrintStream.

It is implemented like this:

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

As we see, it's calling the String.valueOf(Object) method. This is implemented as follows:

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

And here you see, that toString() is called.

Your code is operating normally.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think there is anything wrong with your Eclipse and the output is in fact correct. If you are expecting to see an actual drawing of a Rectangle then you need to re-look at Rectangle class. Calling System.out.print on a Rectangle Object calls the .toString method. This in turn is a representation of the coordinates and dimensions of the Rectangle you created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.