Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: Object-Parameter passing costs

  1. #1
    Join Date
    May 2012
    Rep Power

    Default Object-Parameter passing costs

    Hello everyone, first post on these forums. I've been using Java on and off for the past few years but still have much to learn.

    Right now I'm trying to write a simple 3d tile-based game in LWJGL, sort of like Minecraft in a few aspects, but not that many. Really, it's more like the old Tomb Raider 1-5 games where all the levels are built off of regular-sized grid platforms.

    Anyway, I'm trying to figure out some of the game logic in order to improve performance, and I need to know a little something about passing by parameter/referance or whatnot.

    Currently, I have a "World" object that is in turn made out of a grid of grids. It is created once upon entering the program and drawn every frame using openGL calls. I also have "Monster" objects that move around on in the World. Each frame refresh, I have the game check what tile the monster is above (in order to see if it should fall or not, or whether the next block is too high up for it to climb.) Currently, this is done in my main loop, ie:

    1) The main loop looks at the World object and gives the Monster object the tile it is on with its relevant height data.
    2) The Monster object determines the next location the monster will be if it can make a successful move, giving that new data to the main loop.
    3) The main loop looks at the World object again and gives the Monster the information on the next tile.
    4) The Monster checks if the next position is valid based on the new tile data. If it is valid, the monster moves. Otherwise, it stops and does nothing for that refresh.

    This is a bit messy, though. My monsters are stored in an ArrayList, so this means a lot of passing tiles back and forth between the main loop and each Monster object. What I'd prefer to do is just give each Monster a pointer to the world so it can do its current/next tile checks without needing to jump between classes.

    So, if I initialize each Monster like this:

    Monster monster = new Monster(World world)

    and I have, say, 175 monsters running around at a single time, how much of a performance hit is this? Does Java need to allocate room in memory for 175 new World objects in RAM (which would be far too costly)? Or would Java simply stick pointers to my World object and incur little to no performance penalty?

    Thanks for your help! I know this goes back to parameter passing, which is something I've never fully understood in Java.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Object-Parameter passing costs

    A reference is a sort of pointer to the real thing. No matter the size of the real thing, references all have the same size (typically 32 or 64 bits). All you do is pass references around; only the 'new' operator creates a new object (or 'new' in disguise, such as the clone() method). Don't worry too much about passing parameters; it is done quite efficient in Java.

    kind regards,

    quad64bit likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. passing date into parameter
    By tike in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 05:30 PM
  2. Switch Statement/Parameter Passing
    By in forum NetBeans
    Replies: 1
    Last Post: 01-06-2010, 01:50 PM
  3. passing parameter to a thread
    By adammyth in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-02-2010, 07:58 PM
  4. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  5. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 05:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts