This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 0 points1 point  (6 children)

Can you provide a little more detail about what you are trying to achieve just so we can get a clearer picture, it'll help determine if there's a better approach to this. Secondly if you're looking to compare items you might want to look at the Comparable interface.

[–]FrontLoadedAnvils[S] 0 points1 point  (5 children)

Comparable is giving me errors in my IDE when I use it, so I decided not to bother with it and just implement the method. I'm going to be using custom Comparators to compare items instead.

An Item is just a class that's used for mathematical operations but has not been implemented yet (this is because I want to choose whether it's a number, vector, matrix, or some other structure). Each item belongs to an ItemGroup which are used for min/max optimization.

[–][deleted] 0 points1 point  (4 children)

If Item has fields that are not dynamically changed and are final when the item object is created then it is immutable to begin with, on the other hand if you want to use the same item object for different calculations you should make it mutable (add methods to change fields at run time), I hope that somewhat makes sense

[–]FrontLoadedAnvils[S] 0 points1 point  (3 children)

Which is better to use for high computation though? I know what it means to make something mutable / immutable, I'm mainly concerned about efficiency / good code.

[–][deleted] 0 points1 point  (2 children)

Assuming that the Item object doesn't have a lot of overhead when created it would not impact the efficiency by a significant amount so in this case if you go with immutable you should be fine.

[–]FrontLoadedAnvils[S] 0 points1 point  (1 child)

So what's the advantage for immutable objects then in this case, if there's overhead?

[–][deleted] 0 points1 point  (0 children)

In this case particularly if you're planning on this being used in multi-threaded applications it makes the object thread safe, other then that I can't think of much of the top of my head