The variables that 'hold' objects don't actually 'hold' the object itself. Instead it holds a reference to the real object which lives on the heap.
What you are doing is comparing two references, which are obviously not equal since they refer to different objects.
So what you can do is use the 'equals' method on the java.util.Arrays class. Like:
This method will compare each element in the array to the elements in the other array.
Matt, for your information, there already is a class that does what you want: java.math.BigInteger. Unless this class is for educational purposes I would use that one.
Your isEqualTo method looks horrible. You already have a HugeInteger object, this, yet you create a copy anyway. You then use == to compare the two objects, which will always return false because that uses reference equality and you've just assigned a new object to reference d.
Also, instead of creating your own equality method, you should override the equals method (and therefore also the hashCode method). A quick attempt:
There is a discussion about using instanceof though. On the one hand, using instanceof in a non-final class or a non-final method can cause the symmetry rule of Object.equals to be broken. On the other hand, using class comparison (o.getClass() == getClass(), don't forget to check if o == null) can break the Liskov substitution principle.
I understand the Arrays.equals(array1, array2) but how do i access the array that was created along with the new HugeInteger object "h"? and compare said array to my array "b". And yes this is for educational purposes.
posted 6 years ago
Matt Wilde wrote:I understand the Arrays.equals(array1, array2) but how do i access the array that was created along with the new HugeInteger object "h"? and compare said array to my array "b". And yes this is for educational purposes.
Well, that's a little dependent on how you've organized your Java project. To answer that question, we will need a little more context!
posted 6 years ago
well what you see in the first post is what i've got. So i'm just trying to access the integer array that was made with object "h".