• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Outputs Weird Code  RSS feed

 
Ranch Hand
Posts: 72
2
  • Mark post as helpful
  • send pies
  • 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
  • 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: 72
2
  • Mark post as helpful
  • send pies
  • 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: 56610
172
  • Mark post as helpful
  • send pies
  • 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: 72
2
  • Mark post as helpful
  • send pies
  • 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: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 11498
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the hexstring is usually that object's hashCode(). You can try this to see:
 
Ranch Foreman
Posts: 3074
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 4935
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 72
2
  • Mark post as helpful
  • send pies
  • 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
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 3336
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!