4

I'm studying to do my java OCA test, using the book "Java SE7 Programming Essentials" by Michael Ernest. This is my code for one of the answers to a question below:

public class Point3D {
    int x, y, z;

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return this.x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return this.y;
    }

    public void setZ(int z) {
        this.z = z;
    }

    public int getZ() {
        return this.z;
    }

    public String toString(Point3D p) {
        String result = p.getX() + "," + p.getY() + "," + p.getZ();
        return result;
    }

    public static void main(String args[]) {
        Point3D point = new Point3D();
        point.setX(5);
        point.setY(12);
        point.setZ(13);
        System.out.println(point.toString(point));
    }
}

My code works, but in the last line, I think I've made my code in a weird way, shouldn't there be a way to make just point.toString() and not point.toString(point) return the String representation of the point? Can anyone explain to me how to fix it?

I'm sure it's a simple answer, just trying to understand it because I suspect it points to a hole in my java knowledge.

jlordo
  • 37,490
  • 6
  • 58
  • 83
localhost
  • 1,253
  • 4
  • 18
  • 29

13 Answers13

9

Your method public String toString(Point3D p) is not overriding Object.toString(), which is the standard way to obtain a String representation of an Object.

The point of overriding it is to allow any subclass of Object to provide an appropriate representation of the object as a String. The Java API uses this method in a number of circumstances, mainly when needing to transform an Object into a String (for instance when doing System.out.println(object), or performing String concatenation:

String s = "The point is " + pointObject;

Implement it as ay89 suggested:

@Override
public String toString() { 
    String result = getX() + "," + getY() + "," + getZ(); 
    return result;
} 

Please note the usage of the @Override annotation. If you used it on your method, the compiler would have warned you that something was wrong with its definition.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • Sonarlint warns me that you should return things like this without assigning them to a variable, as it's just a waste of memory, so this could be a one-liner `return result = getX() + "," + getY() + "," + getZ();` – Doug Noel May 10 '20 at 21:44
5

why don't you use

public String toString() {
    String result = getX() + "," + getY() + "," + getZ();
    return result;      
}

then you can just use System.out.println(point)

Ankit
  • 6,554
  • 6
  • 49
  • 71
3

I would use String.format instead of concatenation for clarity:

public String toString() {
    return String.format("(%d, %d, %d)", x, y, z);      
}

With this method you can change the way the output looks in a way that does not require much "mental reconstruction" from the readers of your code. For example, should you decide to change the output to, say, {x=1, y=2, z=3} you can simply rewrite your format line as follows:

return String.format("{x=%d, y=%d, z=%d}", x, y, z);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

OK, I worked it out myself - posting the question proved to be much the same as Rubber Ducking for me.

I needed to add another method:

public String toString() {
    String result = this.getX() + "," + this.getY() + "," + this.getZ();
    return result;      
}
localhost
  • 1,253
  • 4
  • 18
  • 29
2

You need to override the toString() method. All objects in Java have a toString method (it's a method on the Object class).

The default Object implementation just returns the hashCode of that object (so if you don't override it, that's what you will get as well), but if you override, you can do something more meaningful with data specific to your object.

@Override  
public String toString() {
   return this.getX() + "," + this.getY() + "," + this.getZ(); 
}
Alex Florescu
  • 5,096
  • 1
  • 28
  • 49
1

You could simply use this instead of the parameter p. So change your method to public String toString() and change the p to this in the function itself.

Rob
  • 4,927
  • 12
  • 49
  • 54
0

Try this

public String toString() {
    String result = this.getX() + "," + this.getY() + "," + this.getZ();
    return result;
}
Mohan Raj B
  • 1,015
  • 7
  • 14
0

This should fix it:

public String toString() {
String result = this.getX() + "," + this.getY() + "," + this.getZ();
return result;      
}
Jops
  • 22,535
  • 13
  • 46
  • 63
  • Haha! When I thought I'd be the first to answer for the first time, 3 people beat me to it. :) Oh, life. – Jops Mar 25 '13 at 12:38
0

do this way

 public String toString() {
        String result = this.getX() + "," + this.getY() + "," + this.getZ();
        return result;
    }

this is the internal reference to the calling object(point in your case) and thus using it will invalidate the need for the argument point

Code2Interface
  • 495
  • 2
  • 7
0

This code:

@Override
public String toString() {
    return "{ x: " + x + ", y: " + y + ", z: " + z + " }";
}

Would output this:

{ x: 13, y: 2, z: 10 }

As your get methods return the attributes, by using the attributes directly you spare the method calls.

Also, recent compilers will optimize this concatenation with StringBuilder automatically.

For more information check this post.

I hope it helps

Community
  • 1
  • 1
rbento
  • 9,919
  • 3
  • 61
  • 61
0
public String toString() {
    String result = this.x+ "," + this.y + "," + this.z;
    return result;      
}

Use the code like above.

Janny
  • 681
  • 1
  • 8
  • 33
0

Your toString method should be public String toString() [without any args] which means overridden from Object class

If you do so you can do just: System.out.println(point);

G.S
  • 10,413
  • 7
  • 36
  • 52
0

For overriden/implemented methods use the @Override annotation, which would have given you an error on the wrongly parametrized toString.

Furthermore it makes more sence not to use the getters in toString.

In the println or String concatenation, Object.toString is invoked automatically, do not call it explicitly (to show you know that).

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("Point3D(").append(x).append(", ")
        .append(y).append(", ").append(z).append(")";
    return sb.toString();
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138