Java: successful build but the output is “user_pac

2019-08-09 06:38发布

问题:

I am learning java and trying to execute a simple script. I have a class Point as the following:

package user_package;

public class Point {
    float x;
    float y;
    float z;

    public Point(float x, float y, float z){
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static Point add(Point p1, Point p2){
        return new Point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
    }
}

Then I have the main file like this:

import user_package.Point;
import static user_package.Point.add;

class Tutorial{
    public static void main(String[] args){

        float x1 = 1, y1 = 1, z1 = 1;
        float x2 = 2, y2 = 2, z2 = 2;

        Point p1 = new Point(x1, y1, z1);
        Point p2 = new Point(x2, y2, z2);

        Point p3 = add(p1, p2);

        System.out.println(p3);
    }
}

I do this in Netbeans. It gives me no errors and the build is successful but the output is:

user_package.Point@68e26d2e

I tried to search myself but nothing found. Please tell me what the problem is and how I can solve it.

回答1:

When you write

 System.out.println(p3);

There toString() calls on the Object you passed.

Since you didn't provide any implementation there,

You have to ovveride the toString() method in your Point class.

As per docs of toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.

So

 user_package.Point@68e26d2e  is the textual representation of Point class.

To get the required output, write the logic in overridden toString method in Object class and return the string there.

package user_package;

    public class Point {
        float x;
        float y;
        float z;

        public Point(float x, float y, float z){
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public static Point add(Point p1, Point p2){
            return new Point(p1.x + p2.x, p1.y + p2.y, p1.z + p2.z);
        }

        @Override
        public String toString() {
          StringBuilder result = new StringBuilder();
             result.append(this.x).append(",");  
             result.append(this.y).append(",");
             result.append(this.z)
             return result.toString();
        }
    }


回答2:

You have not overridden toString method for Point class so the Object class toString method is called. If you want meaningful textual representation of your object, override toString

public String toString(){

   return "stringYouWantToReturn";

}

In your case, add this to Point class

public String toString() {
        return "Point [x=" + x + ", y=" + y + ", z=" + z + "]";
    }

When you print an object using println method, toString() is implicitly called.



回答3:

You're printing object of class Point. If println() method doesn't know how to print a class, it prints its name. That's why you get such result.

You can do it either like this:

System.out.println(p3.x + ", " + p3.y + ", " + p3.z);

Or as SURESH ATTA suggested, by overriding toString() method for your Point class (or in other words - learning your Point class to know how to behave when you want to print it).



回答4:

You need to override toString() function in Point class or create user defined function in Point class. Using toString() method

public class Point {

public String toString()
{
//your code goes here
return "Point "+ "X : "+ x +"Y : "+ y + "Z : "+z;
}
}


回答5:

As the previous answers already implicitly stated

  System.out.println(p3);

actually is

  System.out.println(p3.toString());

and since you are not providing an own implementation for the toString() method. The default implementation of Object is called. Which results in your given output. In order to change that behaviour you have to override toString()

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