• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
chosenkill6

java...pointers?

5 posts in this topic

So i have started learning java at my school. For most people this is their first language but I already know C so my knowledge in that is helping but I think sometimes it is limiting as well. In C there is so many times where pointers are used but how would I do the same in java? I did some research but all I could find is that referencing is only used in passing objects between methods. I would like to change the value of 3 int variables initialized in method A but instead of making them global (which I've been told is bad programming) I would like to just pass their locations in memory so I can change the variables in method B. What would be the correct way of doing something like this in java? Edited by chosenkill6
0

Share this post


Link to post
Share on other sites
Sorry. No pointers in Java. But you can use references, like in C++. For instance, you can also put your three variables into a custom-made class, kind of like a struct in C, and pass that to the function. You might be tempted to use the Integer class, which is a wrapper around the primitive int, but you must be careful - this class is immutable which can lead to rather unintuitive behavior when passing by reference.

In Java, primitive types are passed by value, and classes are passed by reference (which means a reference of them, is passed by value).

Suppose you pass an instance your class LinkedNode (or whatever) to some function:
[CODE]
void doStuff(LinkedNode node);

...

void main()
{
LinkedNode myNode = new LinkedNode(blah);
doStuff(myNode);
// what does myNode look like now?
}
[/CODE]

Then, you are passing the memory address of "node" (which is a reference) by value (with some syntactic sugar on top), which means that:

[CODE]
void doStuff(LinkedNode node)
{
node.change(...); // this will reflect on "node" outside the function
}
[/CODE]

But:
[CODE]
void doStuff(LinkedNode node)
{
node = createNewNode(); // will NOT reflect outside the function
}
[/CODE]

Because in the first case, you will have altered the reference, but in the second case, you will just have changed the value.

This is the same as in C. For C, you will have the equivalent (assuming LinkedNode is a struct):

[CODE]
void doStuff(LinkedNode *node);
...
void main()
{
LinkedNode *myNode = malloc(sizeof(LinkedNode)); // and initialize fields as needed
doStuff(myNode);
// what does myNode look like now?
}
[/CODE]

And:

[CODE]
void doStuff(LinkedNode *node)
{
node->someField = 3; // this will change someField of the node outside the function
}
[/CODE]

But:

[CODE]
void doStuff(LinkedNode *node)
{
// make a new node for some reason
*node = malloc(sizeof(LinkedNode)); // will not change the node at all since you're just changing the pointer (it's also a memory leak, btw)
}
[/CODE]

Now your problem is that int is a primitive type, so it is *always* passed by value. As I said above, you can use the wrapper class Integer, but unfortunately it is immutable, which means you cannot do anything useful with its reference. You can write your own mutable integer class, which will work, but is rather heavyweight. It is best to have a class that contains all your stuff (in your case, your three variables) so that you don't have to manage one class everytime you need to pass an integer around methods.

Another possibility to consider is that having a method modifying three integers may not be very Java-like, and you may be better served by splitting the work into multiple methods, each returning a single integer, so that you can combine them later on. But it depends on what you are trying to do. Edited by Bacterius
2

Share this post


Link to post
Share on other sites
In java all objects are basically pointers. You allocate the memory with new, and the memory is freed by the garbage collector.

Foo myFoo = new Foo();

myFoo is called an object, but is actually a pointer to an object.

Java passes objects as references passed by value.

You can modify the contents of the object and they will remain modified when you return. For example:

TestFunc ( Foo bar ) //1
{
bar.setName ("replaced name"); //2
bar = new Foo(); //3
bar.setName("new object"); //4
}

Foo myFoo = new Foo(); //5
myFoo.setName("original"); //6
TestFunc(myFoo); //7
myFoo.name.equals("replaced name") // 8


Starting with line 5, you create a new object. This allocates the object, setting the myFoo pointer. Let's say it creates an object at memory address 32. Line 6 sets the name string of the object at memory address 32. Line 7 calls the function; the pointer is passed by value --- that is, you can modify the thing being pointed to (the object at address 32), but you do not modify the pointer, it will still point to memory address 32. Line 1 makes a new pointer, called bar, that is initialized to point to address 32, which is the pass-by-value. Line 2 modifies the name of the object stored at memory address 32. Then line 3 creates a new object, say at memory address 70, and points the new bar pointer to the new memory address. Line 4 modifies the name of the object stored at memory address 70. When we return to line 8, since myFoo was passed by value, it still points to memory address 32, so the object was modified within the function, but it was not replaced by the function.

To modify your integers you cannot pass them by value and have those values change, but you can create an object that contains three integers and modify those members from within the function.
2

Share this post


Link to post
Share on other sites
Another possibility is making these 3 integers into 3-element integer array. Arrays are normal objects and are passed by reference (value of reference).

But it is hard to say, what is the "right" thing to do without having proper context.
0

Share this post


Link to post
Share on other sites
Thanks for the replies guys

What I am trying to do is get input and put that into the variables so I would like to have a separate method that gets input and then another that manipulates the variables.

I understand that I can put the integers into an object but it seems like overkill since I only need to pass the three variables to one function which will get input from the user and put the value into the variable. For now I have them as global so I'm not passing anything. Are global variables really that bad that I should go through all this trouble and change it?

I think I may just go with returning an array from the method but since the variables are so different in purpose within the program it will be hard to keep track of.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0