• 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
gsmaster

Java----C/C++

48 posts in this topic

There is no pass-by-reference in Java, Java only knows pass-by-value.

This means that changes to the contents of the formal parameters do not affect the actual parameters. An example:

public class Test
{
public static void main(String[] args)
{
int a = 10;
LinkedList l = new LinkedList();
javaPassesByValue(a, l);
assert a == 10;
assert l.isEmpty();
}

public static void javaPassesByValue(int x, Collection c)
{
x = 5;
c = new ArrayList<String>(100);
c.add("test");
}
}

In other words, you cannot change the reference. As already pointed out, the reference is passed by value.
0

Share this post


Link to post
Share on other sites
http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html

Pass by Value

Primitive arguments, such as an int or an array, are passed by value, the rest are passed by reference. When invoked, a method or a constructor receives the value of the variable passed in and the method cannot change its value.

No pass by reference?


According to java doc there is and its used on ints or an array.

Simply google something if your not sure instead of posting half truths.

Ste
0

Share this post


Link to post
Share on other sites
Like Fred304 said, arguments in Java are pass-by-value.
Everything you want to know about arguments in Java can be read in this article.


Quote:
Original post by Anonymous Poster
Java and C++ both have the 'new' keyword, but in java, the only way to create an object, is with 'new'. 'new' dynamically allocates memory on the heap...

In Java, the "new" keyword doesn't necessarily allocates memory on the heap. It depends on the JVM how memory allocation is done. While (most?) current JVMs allocate memory on the heap, future JVMs will most likely also have stack allocation. For more info, please read Java theory and practice: Urban performance legends, revisited; Allocation is faster than you think, and getting faster.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
In other words, you cannot change the reference. As already pointed out, the reference is passed by value.


It's perfectly correct to say that in Java all objects are passed by reference. If you change some attribute of the passed class that you'll be changing it in the same instance passed. This is useful in real world.

Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method? The only people I found complaining about this were C++ developers trying to find a pretext for bashing Java.




0

Share this post


Link to post
Share on other sites
Quote:
Original post by RayNbow
Like Fred304 said, arguments in Java are pass-by-value.
Everything you want to know about arguments in Java can be read in this article.


Technicalities. In practical terms, you will get the reference to the same object.

So the claim that originated this flow of e-mails that this wasn't possible and you always needed to use "return" for getting any value is wrong, and in real world work is that what matters.

0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Fred304
In other words, you cannot change the reference. As already pointed out, the reference is passed by value.


It's perfectly correct to say that in Java all objects are passed by reference. If you change some attribute of the passed class that you'll be changing it in the same instance passed. This is useful in real world.

Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method? The only people I found complaining about this were C++ developers trying to find a pretext for bashing Java.


No, it's not. Objects are passed by value. The only reason your changes in the function affect the passed instance of the class is that both pointers, the one in the function and the one outside the function, point to the same instance of the class. The pointer is passed by value, but it's value is the class instance. If you, inside the function, made the passed pointer point to something else, the pointer on the outside of the function would still be pointing to the original instance. If you passed by reference, it would be pointing to whatever you stated in the function.

There's a few examples I can think of that are "real world", but references don't do anything more than pointers could do. And since everything in Java is a pointer, the lack of references is basically insignificant.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Your example does not meet "real world" work. Why a Java programmer would want to change the instance inside of a method?

Someone wanted to do this... But alas, it didn't work because things were passed by value.
0

Share this post


Link to post
Share on other sites
It would seem that the "its pass by value only" guys are correcct. I think one of the major problems with this type of argument / discussion is that the powers that be use the wrong terms. A reference in java isnt actually a reference at all, its a copy of a pointer to an object - which is passing by value, not by passing by reference. Until today I too fell for - It says pass by reference so it must be it passes a reference to the actual object. Not so!

Thanks for correcting me. You learn something new everyday.


Ste
0

Share this post


Link to post
Share on other sites
Quote:
Original post by _goat
No, it's not. Objects are passed by value. The only reason your changes in the function affect the passed instance of the class is that both pointers, the one in the function and the one outside the function, point to the same instance of the class. The pointer is passed by value, but it's value is the class instance. If you, inside the function, made the passed pointer point to something else, the pointer on the outside of the function would still be pointing to the original instance. If you passed by reference, it would be pointing to whatever you stated in the function.

There's a few examples I can think of that are "real world", but references don't do anything more than pointers could do. And since everything in Java is a pointer, the lack of references is basically insignificant.


Ok, did you actually read what I have written?

Who cares if you call it by value and I by reference if ALL I NEED is to work with the class inside of the function???????????????????????

Are we done with that? Do you agree that's possible and used in 100% of the projects you will ever work with in Java?

Tell me just one single case where "changing the instance" inside of a function was terribly needed. Just one...
0

Share this post


Link to post
Share on other sites
Quote:
Original post by RayNbow
Someone wanted to do this... But alas, it didn't work because things were passed by value.


I am sure that was part of a vital functionality in a 1 million dollars project. :)

The guy obviously was a beginner. Beginners commit mistakes but it doesn't mean this a sorely needed "feature" we will die if we don't have it. Seriously, in all my life as a programmer I have seen only C++ developers complaining about it. Can't you adapt to Java's paradigm?

0

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
There is no pass-by-reference in Java, Java only knows pass-by-value.

This means that changes to the contents of the formal parameters do not affect the actual parameters. An example:

public class Test
{
public static void main(String[] args)
{
int a = 10;
LinkedList l = new LinkedList();
javaPassesByValue(a, l);
assert a == 10;
assert l.isEmpty();
}

public static void javaPassesByValue(int x, Collection c)
{
x = 5;
c = new ArrayList<String>(100);
c.add("test");
}
}

In other words, you cannot change the reference. As already pointed out, the reference is passed by value.




I'm confused why l is empty.

When javaPassesByValue is called, what happens in memory? There is an instance of LinkedList l in memory while in the scope of main. That's a given. But when the method is called and l is passed to it, is the instance l cloned so there are two distinct (but identical) instances in memory, their difference being scope? So the stuff you're doing in the method only changes the copy and not the original?

OR...

Is something else happening in memory? If so, I can't imagine what. What I described is pure pass by value... (I think).
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I'm confused why l is empty.

Have you read the article mentioned in this reply?
Quote:
Original post by RayNbow
Everything you want to know about arguments in Java can be read in this article.

0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I'm confused why l is empty.

Let's start at the beginning ;)

What does this mean?

int a = 10;

This is a declaration of a variable named a of type int.
What is the value inside a? Easy one, the number 10.

Now let's move on to the next declaration:

LinkedList l = new LinkedList();

This is a declaration of a variable named l of type LinkedList.
What is the value inside l? Maybe a LinkedList Object?










Wrong! Non-primitve types never hold objects themselves, but rather references to objects. So when you examine the value of l, you will find a reference which points to an empty LinkedList.

Now what happens if you call a method with a and l as actual parameters? a and l are evaluated (their value is obtained), and this value gets passed to the method. in case of a, this value is 10. At this point, this 10 has NOTHING to do with a anymore! And in case of l, this value is a reference to an empty LinkedList which does not know anything about a variable called l.

Now when the method changes its formal parameters, these changes cannot be reflected to the calling method.

BTW imagine what would happen if this actually was a call by reference. After the method call, l (which is declared as LinkedList) would hold a reference to an ArrayList... all hell would break loose ;)
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I'm confused why l is empty.

When javaPassesByValue is called, what happens in memory? There is an instance of LinkedList l in memory while in the scope of main. That's a given. But when the method is called and l is passed to it, is the instance l cloned so there are two distinct (but identical) instances in memory, their difference being scope? So the stuff you're doing in the method only changes the copy and not the original?

OR...

Is something else happening in memory? If so, I can't imagine what. What I described is pure pass by value... (I think).


You've got it. Java, like C, has only pass-by-value. Since all references to first-class objects are actually pointers to instances, when you pass an object you're really passing a pointer to a class instance by value. It's not pass by reference (as in C++) or pass by name (as in ALGOL) or pass-by-descriptor (FORTRAN). It's pass-by-value.

Each of these argument passing mechanisms have a different name because they're different. When it comes to programming, semantics DOES matter.
0

Share this post


Link to post
Share on other sites
I still don't understand. In the example above, l and c both point to the same object, correct? I don't understand how it's possible to change the object through the reference called c and then NOT having this be reflected when I use the object through the reference called l. [depressed]
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Who cares if you call it by value and I by reference

I care. If someone states that Java passes objects by reference and I know it is wrong, why shouldn't I correct the person?

I work with lots of beginners, and they all have their troubles with this issue, so I think it's very important to be clear about this.

Quote:

if ALL I NEED is to work with the class inside of the function???????????????????????

How often do you work with classes? Most of the times, you'll be working with (references to) objects.

Quote:

Tell me just one single case where "changing the instance" inside of a function was terribly needed. Just one...

A swap function maybe?

BTW it's not about changing the instance (because that's totally possible by sending messages to the instance!), it's about changing the reference. Letting it point to ANOTHER instance.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I still don't understand. Both references reference the SAME OBJECT. I don't understand how it's possible to change the object with one reference and not have the object be changed for the other.




public void foo()
{
ArrayList a = new ArrayList()

bar( a );
}

public void bar( arrayList b )
{
b = new ArrayList();
// a and b are no longer aliases. a still has its old value. if we passed a by reference, a would be equal to b
}





Edit: oh, this has been discussed. Sorry [smile]
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
I still don't understand. Both references reference the SAME OBJECT.

Only up to the point where I wrote

l = new ArrayList(100);

From this point on, l points to another object! If you leave this statement out, l.add("test"); affects the original object.
0

Share this post


Link to post
Share on other sites
I just did a test


import java.util.*;

public class Test
{
public static void main(String[] args)
{
int a = 5;
ArrayList l = new ArrayList();
System.out.printf("a is %d and l is %s.%n", a, l.toString());
javaPassesByValue(a, l);
System.out.printf("a is %d and l is %s.%n", a, l.toString());
}

public static void javaPassesByValue(int x, Collection c)
{
x = 10;
c.add("hi");
}
}
~





And this is the output:


a is 5 and l is [].
a is 5 and l is [hi].


So it DOES really work like I remember it. For a second there you really scared the hell out of me..
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
For a second there you really scared the hell out of me..

This was not my intention. It might help remembering it though :)

The important line of code is this one:

c = new ArrayList(100);

I just want people to realise what happens here ;)

What about this piece of code, maybe it makes my point clearer:

public class Test
{
public static void main(String[] args)
{
int a = 10;
LinkedList l = new LinkedList();
Object before = l;
javaPassesByValue(a, l);
assert a == 10;
assert l == before;
}

public static void javaPassesByValue(int x, Collection c)
{
x = 5;
c = new ArrayList();
}
}

After the method call, l still points to the same object. javaPassesByValue was not able to change l. (The object l points to could have been changed by javaPassesByValue, as in your code example... but l == before still holds.)
0

Share this post


Link to post
Share on other sites
Ohhhh I think I finally see your point.. so you're saying if it was really pass by reference, then l would now point to the new ArrayList you declared inside your method, right?

(I think you mean c = new ArrayList(100);
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kevinator
Ohhhh I think I finally see your point.. so you're saying if it was really pass by reference, then l would now point to the new ArrayList you declared inside your method, right?

Right!

Quote:

(I think you mean c = new ArrayList(100);

Whoops... thanks!
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Fred304
I care. If someone states that Java passes objects by reference and I know it is wrong, why shouldn't I correct the person?

I work with lots of beginners, and they all have their troubles with this issue, so I think it's very important to be clear about this.


So explain it completely. It's not a bug, it's a feature.

If your car doesn't fly is that a limitation? Why would you want use a car when you need to fly in the first place?

Java was designed this way. We manage to get the reference inside methods and work with it. If academically this is called "pass by magic" it couldn't matter less for our daily work.

The lack of practical examples doesn't help beginners. What I see is a bunch of C++ developers trying to find pretexts for bashing Java as if this single "detail" was the very meaning of life and all developer's work gravitates around it.

Quote:
Original post by Fred304
Quote:

if ALL I NEED is to work with the class inside of the function???????????????????????

How often do you work with classes? Most of the times, you'll be working with (references to) objects.


And what does it have to do with what I said? Are you implying that not following the correct nomenclature will make people's work impossible?

That's something funny. Let's settle this, let's call it "pass by magic" from now on, and let's see if all systems stop working at the same moment.

Quote:
Original post by Fred304
A swap function maybe?

BTW it's not about changing the instance (because that's totally possible by sending messages to the instance!), it's about changing the reference. Letting it point to ANOTHER instance.


Whatever you want to call it, but the result will be the same.

Me: I'm going to travel to city X in a blue dodge viper.
You: I'm going there to with a red dodge viper and listening to Korn.
Me: Ok, so I see you there then.
You: No, I am going with a "RED" dodge viper and "LISTENING TO" Korn. I will see you there in my "RED" dodge viper and "LISTENING TO" Korn.
Me: Ok, what's the difference? Won't we be there anyway?
You: No, I am going with a "RED" dodge viper and ... (ad infinitum)

What's your point? Please elucidate how Java works and don't treat that as limitation.

"Oh, my computer is not a spaceship, that's a limitation, I will have to work around it!"
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Fred304
I think it's very important to be clear about this.

So explain it completely. It's not a bug, it's a feature.

I'm not saying "Java does not have pass-by-reference and thus sucks", I'm just saying "Java does not have pass-by-reference". I find these differences between languages very interesting. You do not care about every little detail, how it is called and what the semantics are. I'm fine with that!

Quote:

What I see is a bunch of C++ developers trying to find pretexts for bashing Java as if this single "detail" was the very meaning of life and all developer's work gravitates around it.

Will you be surprised to hear that I love Java and have a strong dislike against C++? :)
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