1

So I've had a bet with an acquaintance. It's about what needs more space in developing with Java.

ArrayList<Player>

In coding with the Bukkit (Minecraft) API, a player includes a location, an inventory, a health bar, a foodlevel, an xplevel and much more. A string though, is only just a compilation of characters. That's why I think that an

ArrayList<String>

would be much more pleasant than an ArrayList with players, if you just need the player's name.

My acquaintance told me about object reference, and meant that it'd be exactly the same thing since I get the player's name by doing

player.getName();

and because of "object reference" stuff, no more space or processing power would be occupied.

Which thesis is true?

Baustein
  • 49
  • 4
  • Your friend is correct *if you already have the player objects.* If you don't, you are. – user207421 Jan 06 '15 at 10:05
  • possible duplicate of [What is the memory consumption of an object in Java?](http://stackoverflow.com/questions/258120/what-is-the-memory-consumption-of-an-object-in-java) – Joe Jan 06 '15 at 10:09

1 Answers1

1

Assuming you have the Player instances somewhere else in memory already and you're going to be adding those existing objects to this list, then it comes to the same thing: Both will be lists of object references (to either a String or to a Player). Object references are small (see this answer for details, but we're talking 32 or 64 bits). The key thing here is that if you already have Player instances, adding a second reference to them is cheap; it doesn't make a copy of the object, just a copy of the reference, which is small.

Of course, if you don't already have Player instances and would be creating them just for this list, and those instances would include the name and also other information, then of course that would take more memory.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @T.J - Correct. It is also worth noting that if you did have to create the `Player` instances specifically so that you could call `getPlayer()`, AND you didn't keep a reference to the `Player` objects somewhere, then the long-term memory usage would be the same as if you just had player names. (If the `Player` objects are no longer reachable, they will be garbage collected.) – Stephen C Jan 06 '15 at 10:32