aggregation Vs composition

I got to know that:In aggregation we use the object by reference.In composition we use the object by value.can some one provide me what does this mean to the Java developer. It will be great if you can explain it with simple sample codes for each scenario.
[284 byte] By [nalakayoyoa] at [2007-10-2 9:05:09]
# 1

If you pass on object by reference, you just pass a pointer to the object (in Java this is just passing the variable name for this object).

If you pass an object by value, you make a copy of this object and pass a pointer of this new object (in Java you can call myObject.clone() but be aware that clone() does not guarantee to make a full copy!).

So the main idea about these 2 ways of passing objects is: if I allow the receiver of the object to change the object, I give him the pointer this object so that he can directly change the values (= passing by reference). If I don't want my original object to be changed by the receiver, I just give him a copy and he can do whatever he wants to do with it.

Of course, passing by reference is faster and doesn't waste memory for an extra object for each receiver. It's a more centralized, "public" way. The by-value approach is a paranoic approach for security reasons. This saves you time to check the object after the receiver is finished (and you also need to know that he is finished ...).

MartinHilperta at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Forgot the sample code:

Pass by reference:

Object o = new Object();

receiver.doSomething(o);

//here I hope, that receiver didn't do something dangerous with my object. Or: I hope receiver changed o the way I expected. Because I need the changed values in o.

Pass by value:

Object o = new Object();

Object o2 = o.clone(); //better implement your own getCopy() method!

receiver.doSomething(o2);

//here I can forget about the doSomething() call because I'm not interested in what the receiver has done. I can be sure that my object o is left untouched.

MartinHilperta at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> I got to know that:

> In aggregation we use the object by reference.

> In composition we use the object by value.

Java is always using references. It it always pass by value, because the thing that's being passed is not the object itself, which is out on the heap, but a reference to that object.

And that's not the difference between aggregation and composition. Both describe a parent-child relationship between objects, but in aggregation the children have a life outside the parent. Composition means that when you delete the parent the children are deleted, too.

No code - you're on your own.

%

duffymoa at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

I have a ScalableImageRenderer that's an example of aggregation. It has an ImageRenderer, ImageScale and Image. It uses all three in conjunction to perform it's job but all three have lives outside of the ScalableImageRenderer itself as they're passed to it in it's constructor. If perhaps it created them itself and they died with it then it might be considered composition.

kablaira at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
> If you pass on object by reference, you just pass a> pointer to the object (in Java this is just passing> the variable name for this object).Java does not do pass by reference.
jschella at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

> Java does not do pass by reference.

yes it does. object values are passed by reference, not by value. it is the passing mechnism that is pass by value.

assuming the famous sayings of jg is true, what he was saying is 'java does not pass objects, java passes its references, and the references are passed by value'.

as you claimed in the past that you have thirty years of experience programming in c++, you should be able to comprehend this very well. and by this, i mean entry level stuff.

jschella at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> > Java does not do pass by reference.

>

> yes it does. object values are passed by reference,

> not by value. it is the passing mechnism that is pass

> by value.

>

> assuming the famous sayings of jg is true, what he

> was saying is 'java does not pass objects, java

> passes its references, and the references are passed

> by value'.

>

> as you claimed in the past that you have thirty years

> of experience programming in c++, you should be able

> to comprehend this very well. and by this, i mean

> entry level stuff.

Java passes by value. The fact that the value might be a reference itself is irrelevent. Passing a reference by value is not the same as passing by reference.

I probably didn't actually need to say that since anyone passing by should immediately notice you contradicted yourself to begin with. "object values are passed by reference" and "java does not pass objects". It's amazing how you can sit there and say with a straight face that Java passes something it does not pass through a particular methodology. Yes, I know that's not what you really meant, I understand quite clearly exactly what you meant, the problem it's the same fuckin thing everyone else meant and you're just reiterating the obvious and pretending it's somehow different.

All you've done is backup jschell. Java doesn't pass objects, it passes their reference by value. Nothing is passed by reference, period.

kablaira at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

Martin Hilpert, what have you done?

Why, oh why must this sorry-@ss topic be resurrected again? WHY?

Java's always pass by value. Objects aren't passed; references to objects are passed. Objects live on the heap, and there they stay.

There is exactly one parameter passing mode in Java -- pass by value -- and that helps keep things simple.

-- James Gosling, "The Java Programming Language, Second Edition"

(James Gosling being the father of Java)

Still care to argue? Address all comments to James Gosling, please.

Been debated a thousand times. Here's another example:

http://forum.java.sun.com/thread.jspa?threadID=656399&messageID=3857999

%

duffymoa at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 9
Who's tired of this topic?Who's sick of "through the looking glass" arguments with the ultimate Mad Hatter, daFei? It's bad enough having to answer the tsunami of questions from the clueless, but to be debated without end by a know-nothing like daFei gets old fast.%
duffymoa at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> > Java does not do pass by reference.

>

> yes it does. object values are passed by reference,

> not by value. it is the passing mechnism that is pass

> by value.

No.

> assuming the famous sayings of jg is true, what he

> was saying is 'java does not pass objects, java

> passes its references, and the references are passed

> by value'.

You don't have to assume that JG is right. He is.

> as you claimed in the past that you have thirty years

> of experience programming in c++, you should be able

> to comprehend this very well. and by this, i mean

> entry level stuff.

What has this got to do with pass-by-value or pass-by-reference?

And, what has all this got to do with the original question?

hermionea at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
> > Java does not do pass by reference.> > yes it does. No it doesn't. As dozens of people have repeatedly pointed out to you.(And lest anyone doubt that dafei knows anything then try to get him to explain how a stack works.)
jschella at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

> > > Java does not do pass by reference.

> >

> > yes it does.

>

> No it doesn't. As dozens of people have repeatedly

> pointed out to you.

>

and now you are the only one left, still confused.

> (And lest anyone doubt that dafei knows anything then

> try to get him to explain how a stack works.)

this seems to be intersting, come here:

http://forum.java.sun.com/thread.jspa?threadID=689316&start=225

jschella at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 13

> and now you are the only one left, still confused.

He wasn't confused to begin with. He was correct and accurate in every sense of the word. You are the one who posted a convoluted and meaningless reply that implied he was wrong even as it said the same thing he did. If anyone is confused, it's you.

kablaira at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 14

> >

> > No it doesn't. As dozens of people have

> repeatedly

> > pointed out to you.

> >

> and now you are the only one left, still confused.

>

You are the only one confused.

> > (And lest anyone doubt that dafei knows anything then

> > try to get him to explain how a stack works.)

>

> this seems to be intersting, come here:

> http://forum.java.sun.com/thread.jspa?threadID=689316&

> start=225

Which means that you still have not explained how a stack works.

jschella at 2007-7-16 23:12:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 15

> and now you are the only one left, still confused.

Confusion, thy name is daFei.

> > (And lest anyone doubt that dafei knows anything

> then

> > try to get him to explain how a stack works.)

>

> this seems to be intersting, come here:

> http://forum.java.sun.com/thread.jspa?threadID=689316&

> start=225

Pray tell, daFei, how is this link germane to anything other than further proof of your insanity?

%

duffymoa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 16

Aggregation and composition don't have anything to do with values or references. In UML, both terms are used to refer to whole-part relationships. I wasn't exactly clear on this myself, but apparently the difference has to do with the lifetime of the parts relative to the whole. See here for a more detailed explanation:

http://ootips.org/uml-hasa.html

RadcliffePikea at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 17

Guys , am not here to fight with anyone , but this appeared confusing for me , So I was testing with this sample piece of code(below).

Can some help me understand this

first println prints null , eventhough the method assigns a different value

last println displays the value that was assigned inside the method.

public class Test2 {

public static void main(String args[]){

Object x = null;

giveMeAString (x);

//First println

System.out.println (x);

me m1 = new me();

m1.f1="Outside method";

testme(m1);

//Last println

System.out.println(m1.f1);

}

public static void giveMeAString(Object y){

y="Iam inside method";

}

public static void testme(me y){

y.f1="modified inside method";

//inside println

System.out.println("Inside "+ y.f1);

}

}

class me{

public String f1=null;

public String f2=null;

}

am_newa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 18
In the first you modified the local variable, which was a copy of the variable passed. In the second you modified the actual instance variable of m1.
kablaira at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 19

> Guys , am not here to fight with anyone , but this

> appeared confusing for me , So I was testing with

> this sample piece of code(below).

It is unclear to me what this has to do with this thread.

>

> Can some help me understand this

> first println prints null , eventhough the method

> assigns a different value

Java uses pass by value not reference.

Or in other words the assignment in the method remains in the method. So when you print with it it is still null.

jschella at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 20
Thanks for the reply guysYou mean when I do y="Iam inside method";a new object is created and assigned to the local variable y Eventhough I do the same in the 2nd case , but since iam modfiying the instance variable the value got changed?
am_newa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 21

> Thanks for the reply guys

>

> You mean when I do

> y="Iam inside method";

>

> a new object is created and assigned to the local

> variable y

It is using a local variable and not a reference to the variable that it was called with.

>

> Eventhough I do the same in the 2nd case , but since

> iam modfiying the instance variable the value got

> changed?

In the second case the local variable points to an object. The same object that the variable you used during the call does.

Then you modify the object (the data) rather than the local variable.

jschella at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 22
Thanks JschellGot it
am_newa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 23

when the method testme(m1) is called

m1.f1=" Modified Inside Method "

here it changes the data member f1 of class m1 into "modified Inside method " and then u just printed the value of m1.f1 merging with Inside b4 it.. so it prints Inside Modifed Inside method.

and then when println statement next to the testme() method is called then it prints m1.f1 value which is modifed inside method .

y_pavanka at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 24

I know that this is an old post, but I could not resist replying. If nothing else, to clarify for any newcomers that may read this thread.

First off:

> Java uses pass by value not reference.

No, actually it does not. Java is "pass by reference" not "pass by value".

To the crux of the question: why is the first println printing null?

The reason it is printing null is because the variable x in the main method was assigned to null. Then, a reference to this variable was given to the method giveMeAString.

So it works like this: giveMeAString has an Object pointer (y) which you told to point to the same thing that x is pointing to. Inside of giveMeAString you code it as y = "...", which in turn says: "Ignore what you were pointing to, and re-assign y to point to a new String. So, y is no longer referencing the same instance that x is referencing. Therefore, what happened to y inside the method giveMeAString has no bearing on x in the main method.

Because String is immutable (can't be mutated/changed once instantiated), there is no way to achieve what you had hoped to by using a void method. The only way for giveMeAString method to change x in the main method is to have giveMeAString return an object and re-assign x in the main method to point to that returned value.

The way it is currently written, giveMeAString method is useless. It would never achieve anything for anybody. It would simply take y, reassign it to a new String instance, and then destroy that instance as soon as the method was done since it would now be out of scope.

I hope this makes sense.

Message was edited by:

luccaB

luccaBa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 25

> I know that this is an old post, but I could not

> resist replying. If nothing else, to clarify for any

> newcomers that may read this thread.

>

> First off:

> > Java uses pass by value not reference.

>

> No, actually it does not. Java is "pass by reference"

> not "pass by value".

Wrong.

Please go actually learn some computer science and how compilers and interpreters work before posting on this particular subject in the future.

And if someone 'taught' you this then once you learn how it actually works then educate that person as well.

jschella at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 26

>

> Wrong.

>

> Please go actually learn some computer science and

> how compilers and interpreters work before posting on

> this particular subject in the future.

OK, perhaps I fumbled this one slightly. Let me phrase it this way: Objects are not passed by reference, but rather Object references are passed by value. These object references (pointers) are a reference to the original memory location rather than a copy of the value (like primitives). To many, this is pass-by-reference. Perhaps this term has been bastardized by an industry full of non-compiler folks. I apologize for perputating this mis-understanding. If you read through the rest of my example/description it is quite clear and accurate.

>

> And if someone 'taught' you this then once you learn

> how it actually works then educate that person as

> well.

Get a life. If you'd like to call me out as ignorant, that is fine, but at least back up your argument. It's simple to be critical when you yourself post nothing to be criticized. I see no explanation from you as to what is correct and/or appropriate. Is this because you don't know? Or are you that insecure? I love a good debate because people do learn that way, but if your just going to post snide remarks and put downs, please go somewhere else.

luccaBa at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...
# 27

> >

> > Wrong.

> >

> > Please go actually learn some computer science and

> > how compilers and interpreters work before posting

> on

> > this particular subject in the future.

>

> OK, perhaps I fumbled this one slightly. Let me

> phrase it this way: Objects are not passed by

> reference, but rather Object references are passed by

> value. These object references (pointers) are a

> reference to the original memory location rather than

> a copy of the value (like primitives). To many, this

> is pass-by-reference.

And again if you learn some computer science, the formal kind, you will find that the last statement above is false.

Note that accepting a less general term does nothing for those that completely understand it. And for those that are less than sure then a less than precise usage does nothing but confuse.

> Perhaps this term has been

> bastardized by an industry full of non-compiler

> folks.

Which your usage above would be.

> I apologize for perputating this

> mis-understanding. If you read through the rest of

> my example/description it is quite clear and

> accurate.

>

> >

> > And if someone 'taught' you this then once you

> learn

> > how it actually works then educate that person as

> > well.

>

> Get a life. If you'd like to call me out as ignorant,

> that is fine, but at least back up your argument.

> It's simple to be critical when you yourself post

> nothing to be criticized. I see no explanation from

> you as to what is correct and/or appropriate. Is this

> because you don't know? Or are you that insecure? I

> love a good debate because people do learn that way,

> but if your just going to post snide remarks and put

> downs, please go somewhere else.

Sorry but life is limited and the thousands of posts

by me and probably at least 100 others on this

subject on this very site should be sufficient

to allow you to educate yourself.

As should any formal general text on computer science.

People who post otherwise fall into one of the following

1. They do not understand the term at all.

2. They are 'generalizing' the term to a less formal meaning.

3. They understand the consequences of the term but not definition.

4. They are completely ignorant in every possible way (there is only one person that falls into this category and presumably not you.)

All of the above is based on ignorance. (The second in the above is based on the ignorance that such statements 'help' someone to understand what it means.)

Finally these days I am less accepting of those that do not understand the difference and yet make absolute statements about it which are in fact completely wrong.

jschella at 2007-7-20 20:01:43 > top of Java-index,Other Topics,Patterns & OO Design...