• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Outputs Weird Code

 
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why does Java output these weird codes MyPoint@15db9742.

Class


Main


Output
 
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not overriding the toString()-method -> the implementation of Object is used (which returns className@hashcode).

(A few other things:
You should check your getY-method.
There is a bit duplicated code, your distance(MyPoint) method could delegate to the distance(int,int) method, your no-args constructor could delegate to your other constructor.
Your toString(int,int) method could be static [or removed].
Do you have to write the points mutable [immutable objects are preferable]?)
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tobias.  We just learned immutable, encapsulation, and couple things. Our teacher is forcing us to use certain things to learn them. This is the way she forced us to made this program but I cannot get it run properly for some reason...
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you think you are overriding a method preceded it with the annotation @Override. Your toString method shou‍ld therefore read:-Now see what the compiler has to say about that.

Don't use squares and square roots to calculate distances. Use this instead. Don't use Math.pow(x, 2) for squares; you will usually get faster execution with x * x
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell, I am obligated to use Math.pow and Math.sqrt..

I replaced toString method you wrote with mine but issue still exists...
 
Rancher
Posts: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

these weird codes MyPoint@15db9742.  


That String is the default value returned by the Object class's toString() method:  classname@hexstring
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:


Enhance the part where the constructors are by removing redundancy from the code:
Note, that same as any other ordinary methods you write in order to improve clarity and code re-usability, you can do so the same with constructors.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really need to have this MyPoint object mutable?

I'm referring to method:

One more thing: for the indentation, please use 4 spaces instead tabs, which are most likely are set to 8 space characters in your IDE you use.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the hexstring is usually that object's hashCode(). You can try this to see:
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:
I replaced toString method you wrote with mine but issue still exists...



It wouldn't compile if you did what Campbell suggested.
The point being that the signature of toString has no parameters.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:It wouldn't compile if you did what Campbell suggested.


I think with that particular example he gave he was referring to an @Override annotation, just to show what benefit it would bring on the table using it.
 
Ranch Hand
Posts: 39
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

your above method in the class MyPoint takes two arguments ,int x and int y .
when you called it at the main :-

you haven't provided any arguments so java hase directed you
to the super class method toString() that belong to the Object class witch return a different output like the one you have seen !
I advise you to rethink a gain if you really need such a method in your class, because it actually does 'nothing important' in your program.
 
Recaip Sanli
Ranch Hand
Posts: 75
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone!

I found that problem was this ->
public String toString(int x, int y) {
     return "(" + x + "," + y + ")";
  }

Should be like this ->
  public String toString() {
     return "(" + this.x + "," + this.y + ")";
  }

Because teacher wanted us to use "this." instead of sending in values
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:I found that problem was this ->
public String toString(int x, int y) {
     return "(" + x + "," + y + ")";
  }

Should be like this ->
  public String toString() {
     return "(" + this.x + "," + this.y + ")";
  }

Because teacher wanted us to use "this." instead of sending in values



It's not the teacher's decision. The Java language describes the toString() method, with no parameters. All objects have this method and many of the classes in the standard API use it, expecting the programmer to have overridden it to produce useful output.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Recaip Sanli wrote:


In your case "this" is implied because toString() is a method of your class and x is a field of the same class. So you could have written it as
reply
    Bookmark Topic Watch Topic
  • New Topic