Jump to content
  • Advertisement
Sign in to follow this  
cignox1

[java] how to know the class of an object

This topic is 4908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When overriding the equals functions you should follow the definition from the java api docs Object.equals( Object o ) And make sure you are consistant with its definition. I would definitely advise against the top way of checking for class equivalence. Or you could catch the ClassCastException like so.


public boolean equals( Object o ) {
boolean toRet = true;
try {

if( !bigInteger.equals( ((BigInteger)o).toString() ) )
toRet = false;

}catch(ClassCastException cce) {
toRet=false;
}
return toRet;
}




However, you are doing things in a very confusing and incohesive manner.

I would have done it like this

public boolean equals( Object o ) {
return bigInteger.equals(o);
}


There is no need to compare using the toString() method. Why would you do something like that. BigInteger.equals() already compares itself to the object checks if its a BigInteger class and if the values are equal.

Share this post


Link to post
Share on other sites
Advertisement
Oh! so why don't you turn the bigInteger into a BigInteger class instead of comparing the strings.


public boolean equals( Object o ) {
return (new BigInteger(bigInteger)).equals(o);
}



or just store the bigInteger as a BigInteger which should be what you do anyways since your variable name happens to be 'bigInteger'.

Is there as reason for storing it as a string since you can just use the toString() of BigInteger to get the string representation if you need it.

Share this post


Link to post
Share on other sites
Yeah...

How do you think a very long list of digits would be stored? I can think of a few ways: a very big array, a linked list, or a string. I chose the latter to be the underlying data structure for a very big integer.

Share this post


Link to post
Share on other sites
Actually I would choose to store them not as a String since each char of a string is 8 bits in ASCII format or more for extended character formats. I would choose to store the number using bytes. And it appears the BigInteger class does exactly that also giving you the option to manipulate the individual bits which allows you to use the BigInteger class like a bit array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!