• Create Account

## Java----C/C++

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

48 replies to this topic

### #41Fred304  Members

Posted 05 May 2006 - 07:01 AM

Quote:
 Original post by Anonymous PosterWho 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.

### #42rip-off  Moderators

Posted 05 May 2006 - 07:03 AM

Quote:
 Original post by KevinatorI 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]

### #43Fred304  Members

Posted 05 May 2006 - 07:03 AM

Quote:
 Original post by KevinatorI 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.

### #44Kevinator  Members

Posted 05 May 2006 - 07:14 AM

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

### #45Fred304  Members

Posted 05 May 2006 - 07:21 AM

Quote:
 Original post by KevinatorFor 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 ;)

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

### #46Kevinator  Members

Posted 05 May 2006 - 07:25 AM

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

### #47Fred304  Members

Posted 05 May 2006 - 07:30 AM

Quote:
 Original post by KevinatorOhhhh 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!

### #48Anonymous Poster_Anonymous Poster_*  Guests

Posted 05 May 2006 - 07:44 AM

Quote:
 Original post by Fred304I 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 Fred304A 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!"

### #49Fred304  Members

Posted 05 May 2006 - 08:01 AM

Quote:
Original post by Anonymous Poster
Quote:

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++? :)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.