Java----C/C++

Started by
47 comments, last by Fred304 17 years, 11 months ago
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Advertisement
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.
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
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...
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?

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).
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.

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 ;)
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.

Stephen M. Webb
Professional Free Software Developer

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]

This topic is closed to new replies.

Advertisement