Copy Constructor versus copy() methods

Hi All

Am interested in some views on copy constructors versus copy methods.

I work with a group of programmers many of whom diligently use copy constructors instead of copy methods. I think this is due to reading Bloch's views on the clone method and advocation of using a copy constructor instead of the clone method. I agree the clone method is broken but feel in adopting a copy constructor approach we might be throwing the baby out with the bath water.

THe problem with copy constructor I find is that it makes an assumption about the type of the object at the point of copy. That might be fine if you're trying to make sure your object is of a particular type but I find that in 90% of the times this is not the intention.

Does anybody have any contrary views or things to add before I ask those in my team to begin favouring copy methods over copy constructors?

Thanks in advance

Tucky

[939 byte] By [Tuckya] at [2007-10-2 13:24:13]
# 1

> THe problem with copy constructor I find is that it

> makes an assumption about the type of the object at

> the point of copy.

In what way?

I tend use copy ctors, mainly out of habit I suppose, and to be consistent with convention. Can you provide a bit more detail about your above point, maybe some sample code?

jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
Are you talking about a factory method? Foo foo2 = factory.copy(foo1);where the class of the new object can be determined at runtime?
jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
verdy:tucky doesnt know what the shi and t he is talking about, and you seem to be a sucker...
jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> I tend use copy ctors,

did you really every produced a copy contructor in java?

> mainly out of habit I suppose,

that must be a great habit.

> and to be consistent with convention.

and what convention you are talking about?

> Can you provide

> a bit more detail about your above point,

can you?

>maybe some

> sample code?

dont dream.

jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

Some example code that should give a feel for what I mean

METHOD 1 - Copy method, allows client code to be free of type assumptions

========================================================

public new ClassA(int a)

...

ClassA copy(){

return new ClassB(a, b);

}

public new ClassB(int a, Date b) extends ClassA

...

ClassB copy(){

return new ClassB(a, b);

}

public new ClassC(int a, String c) extends ClassA

...

ClassC copy(){

return new ClassC(a, c);

}

public void someClientMethod(ClassA input){

ClassA classACopy = input.copy();

...

METHOD 2 - Copy constructor, client code needs to make type assumption

========================================================

public new ClassA(int a)

...

public ClassA (ClassA classA){

...

}

public new ClassB(int a, Date b) extends ClassA

...

public ClassB (ClassB classB){

...

}

public new ClassC(int a, String c) extends ClassA

...

public ClassC (ClassC){

return new ClassC(a, c);

}

public void someClientMethod(ClassA input){

ClassA classACopy = new ClassA(input);

...

Tuckya at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
big time, verdy.
Tuckya at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
daFei... I hope you're not posting to this site without your parent's permission.
Tuckya at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 8
> daFei... I hope you're not posting to this site> without your parent's permission.Please ignore dafei, he escaped out of his cage: http://forum.java.sun.com/thread.jspa?threadID=701432&tstart=0
watercolour...yesa at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

> > I tend use copy ctors,

>

> did you really every produced a copy contructor in

> java?

Yes. Though I admit I don't know if what people commonly call "copy constructors" truly are, or if there's a more rigid standard definition.

> > and to be consistent with convention.

>

> and what convention you are talking about?

The convention to copy an object's state through a constructor, rather than a copy or clone method.

>

> > Can you provide

> > a bit more detail about your above point,

>

> can you?

I can if somebody who will understand it asks.

jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> Some example code that should give a feel for what I

> mean

>

> METHOD 1 - Copy method, allows client code to be

> free of type assumptions

> ======================================================

> ==

>

> public new ClassA(int a)

> ...

> ClassA copy(){

> return new ClassB(a, b);

> }

>

> public new ClassB(int a, Date b) extends ClassA

> ...

> ClassB copy(){

> return new ClassB(a, b);

> }

>

> public new ClassC(int a, String c) extends ClassA

> ...

> ClassC copy(){

> return new ClassC(a, c);

> }

>

> public void someClientMethod(ClassA input){

> ClassA classACopy = input.copy();

> ...

Not really sure what you're showing here, as that code won't compile.

>

>

> METHOD 2 - Copy constructor, client code needs to

> make type assumption

> ======================================================

> ==

>

> public new ClassA(int a)

> ...

> public ClassA (ClassA classA){

> ...

> }

>

> public new ClassB(int a, Date b) extends ClassA

> ...

> public ClassB (ClassB classB){

> ...

> }

>

> public new ClassC(int a, String c) extends ClassA

> ...

> public ClassC (ClassC){

> return new ClassC(a, c);

> }

>

> public void someClientMethod(ClassA input){

> ClassA classACopy = new ClassA(input);

> ...

Okay, now I see what you mean. I can't say I've ever been in a situation where I thought, "I want a copy of this, but I want flexibility in what the actual type is."

jverda at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 11

>Okay, now I see what you mean. I can't say I've ever been in a situation where I thought, "I want a copy of this, but I want flexibility in what the actual type is."

You may be right here in that the problem is deeper than just the copy method/constructor issue. I need to go back to the points at which this has been done to see why they were doing it. There's a lot of examples of poor encapsulation in the code base and this issue may be more a result of that.

For example the case which caused me to finally raise this post has been removed by some refactoring of responsibilities. I'll see if I can dig up a concrete example based on a valid need over the next day or two and re-post.

Tuckya at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

> daFei... I hope you're not posting to this site

> without your parent's permission.

if your parents had given you permission to post here, they certainly made a mistake.

a copy constructor is a special constructor, it works in conjuction with the assignment operator, as a regular constructor does not return anything, it is merely a function that is first called to create an instance.

java does not allow any overloading on the assignment operator, or any others. so unless you are implementing a new version of java, you just have to use the clone method or overriding it with yours or using yours anyway to do the 'copy'.

hope this helps.

Tuckya at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 13

> You may be right here in that the problem is deeper

> than just the copy method/constructor issue. I need

> to go back to the points at which this has been done

> to see why they were doing it. There's a lot of

> examples of poor encapsulation in the code base and

> this issue may be more a result of that.

>

> For example the case which caused me to finally raise

> this post has been removed by some refactoring of

> responsibilities. I'll see if I can dig up a concrete

> example based on a valid need over the next day or

> two and re-post.

This sounds like the template pattern. The only reason I would use a "copy method" like this is if the object that needs a copy has no knowledge of the concrete type. In this case, however, a copy ctor is not an option as it would be impossible for the object to call it anyway. In all other cases I think a copy ctor is appropriate and follows convention. Any circumstance where this would not work is one that goes beyond the purpose of a copy ctor and probably merits a special method other than "copy".

kablaira at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 14

> daFei... I hope you're not posting to this site

> without your parent's permission.

I'm glad you've already figured out he's a troll. It is very sad when they manage to mislead new developers, especially those seeking knowledge rather than a free ride. Look out for BobGateuax or whatever his name is too, he's really talented at sounding coherent while spewing complete and utter nonsense.

> > daFei... I hope you're not posting to this site

> > without your parent's permission.

>

> if your parents had given you permission to post

> here, they certainly made a mistake.

>

> a copy constructor is a special constructor, it works

> in conjuction with the assignment operator, as a

> regular constructor does not return anything, it is

> merely a function that is first called to create an

> instance.

>

> java does not allow any overloading on the assignment

> operator, or any others. so unless you are

> implementing a new version of java, you just have to

> use the clone method or overriding it with yours or

> using yours anyway to do the 'copy'.

>

> hope this helps.

You obviously have no idea what a copy ctor is. It's used extensively in Collections, for example, to provide the ability to copy one Collection over to another Collection by passing it to the new Collection's ctor.Of course, we're not surprised by your usual incoherent, babbling nonsense because you don't even understand what a reference in Java is.

kablaira at 2007-7-13 11:02:44 > top of Java-index,Other Topics,Patterns & OO Design...
# 15

> You obviously have no idea what a copy ctor is. It's

> used extensively in Collections, for example, to

> provide the ability to copy one Collection over to

> another Collection by passing it to the new

> Collection's ctor.

wrong!

java collections uses the clone and newInstance methods. there is no copy constructor in java, meaning it does not exist in java. let me know if i could make it clearer.

you have to have a copy constructor to pass an object by value.

at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 16

> a copy constructor is a special constructor, it works

> in conjuction with the assignment operator, as a

> regular constructor does not return anything, it is

> merely a function that is first called to create an

> instance.

You're talking about C++, not Java.

> java does not allow any overloading on the assignment

> operator, or any others. so unless you are

> implementing a new version of java, you just have to

> use the clone method or overriding it with yours or

> using yours anyway to do the 'copy'.

Or maybe he's just doing what everybody but you already understood: creating a ctor that takes an instance of that same type as an argument and contructs a new object based on the old one.

Since you have nothing useful to offer, please just go away.

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 17

> > You obviously have no idea what a copy ctor is.

> It's

> > used extensively in Collections, for example, to

> > provide the ability to copy one Collection over to

> > another Collection by passing it to the new

> > Collection's ctor.

>

> wrong!

>

> java collections uses the clone and newInstance

> methods.

Wrong.

Here's the copy constructor for Arraylist: public ArrayList(Collection<? extends E> c) {

size = c.size();

// Allow 10% room for growth

elementData = (E[])new Object[

(int)Math.min((size*110)/100,Integer.MAX_VALUE)];

c.toArray(elementData);

}

> there is no copy constructor in java,

The above is what people are talking about when referring to copy constuctor. It's not the same as C++'s copy constructor, but then Java is not the same as C++ (which you have yet to grasp).

It may be the the term "copy constructor" has a pretty standard meaning (like pass by reference does) and that's being misused here (the way you misuse pass by reference). If so, then fine, Java does not have a copy constructor in the strict sense. But we all knew what the OP was talking about. Only you are confused (which is the normal state of things).

> meaning it does not exist in java. let me know if i

> could make it clearer.

You need to understand things yourself before you try to clarify them for others

> you have to have a copy constructor to pass an object

> by value.

Wrong.

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 18

> You're talking about C++, not Java.

>

there is no copy constructor in java.

> Or maybe he's just doing what everybody but you

> already understood: creating a ctor that takes an

> instance of that same type as an argument and

> contructs a new object based on the old one.

>

how can you return it with a constructor?

> Since you have nothing useful to offer, please just

> go away.

although i never meant to flash you out for a show but you keep doing it: last time you did it was last year, and you did it again...

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 19

> > You're talking about C++, not Java.

> >

> there is no copy constructor in java.

>

> > Or maybe he's just doing what everybody but you

> > already understood: creating a ctor that takes an

> > instance of that same type as an argument and

> > contructs a new object based on the old one.

> >

> how can you return it with a constructor?

Come on, are even you that stupid? Here is what people are talking about. I won't debate whether this is truly a "copy constructor." I don't know and don't care. But it's very very obvious that this is what everybody means:

public class Foo {

private final String bar;

private final int baz;

public Foo(String bar, int baz) {

this.bar = bar;

this.baz = baz;

}

// "copy constructor"

public Foo(Foo that) {

this.bar = that.bar;

this.baz = that.baz;

}

}

...

Foo foo1 = new Foo("abc", 123);

Foo foo2 = new Foo(foo1); // create another Foo using the "copy constructor"

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 20

> Here's the copy constructor for Arraylist: >public ArrayList(Collection<? extends E> c) {

> size = c.size();

> // Allow 10% room for growth

> elementData = (E[])new Object[

> (int)Math.min((size*110)/100,Integer.MAX_VALUE)];

> )];

> c.toArray(elementData);

>}

>

>

i understand your village has its own unique culture; however, may i humbally ask what part of the code makes you think, if ever you think, that it is a copy constructor at work? if you shed the faintest light on a single trace that it has anything to do with copy constructor? what makes those regular constructors to be copy constructor?

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 21

> > > You obviously have no idea what a copy ctor is.

> > It's

> > > used extensively in Collections, for example, to

> > > provide the ability to copy one Collection over

> to

> > > another Collection by passing it to the new

> > > Collection's ctor.

> >

> > wrong!

> >

> > java collections uses the clone and newInstance

> > methods.

>

> Wrong.

>

> Here's the copy constructor for Arraylist:

And just to nip captain doofus in the bud

public Object[] toArray() {

Object[] result = new Object[size];

System.arraycopy(elementData, 0, result, 0, size);

return result;

}

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 22

> > Here's the copy constructor for Arraylist: > >public ArrayList(Collection<? extends E> c) {

> > size = c.size();

> > // Allow 10% room for growth

> > elementData = (E[])new Object[

> > (int)Math.min((size*110)/100,Integer.MAX_VALUE)];

> > )];

> > c.toArray(elementData);

> >}

> >

> >

>

> i understand your village has its own unique culture;

> however, may i humbally ask what part of the code

> makes you think, if ever you think, that it is a copy

> constructor at work? if you shed the faintest light

> on a single trace that it has anything to do with

> copy constructor? what makes those regular

> constructors to be copy constructor?

Ah-ha!

Got you unckle fscker!

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 23

> > Here's the copy constructor for Arraylist:

>

> And just to nip captain doofus in the bud

>

> public Object[] toArray() {

> Object[] result = new Object[size];

> System.arraycopy(elementData, 0, result, 0, size);

> return result;

>}

and why it is a copy constructor? what is your sense of a copy constructor? even just a constructor? all i see is methods and regular constructors?

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 24

> > Here's the copy constructor for Arraylist: > >public ArrayList(Collection<? extends E> c) {

> > size = c.size();

> > // Allow 10% room for growth

> > elementData = (E[])new Object[

> > (int)Math.min((size*110)/100,Integer.MAX_VALUE)];

> > )];

> > c.toArray(elementData);

> >}

> >

> >

>

> i understand your village has its own unique culture;

> however, may i humbally ask what part of the code

> makes you think, if ever you think, that it is a copy

> constructor at work? if you shed the faintest light

> on a single trace that it has anything to do with

> copy constructor? what makes those regular

> constructors to be copy constructor?

As I've stated at least two or three times already in this thread: I realize that there may be a more rigid definition of "copy constructor" that isn't actually applicable in Java. However, formal definitions aside, everybody here except you has known from the first post that this is what the OP was talking about when he said "copy constructor." That is what we all mean in this context, and that is what is commonly meant in Java.

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 25

> > > Here's the copy constructor for Arraylist:

> >

> > And just to nip captain doofus in the bud

> >

> > public Object[] toArray() {

> > Object[] result = new Object[size];

> > System.arraycopy(elementData, 0, result, 0,

> size);

> > return result;

> >}

>

> and why it is a copy constructor? what is your sense

> of a copy constructor? even just a constructor? all i

> see is methods and regular constructors?

***********,

You said there were no constructors that used copying. You are wrong. Anyone with a half a brain can follow through from the constructor code posted to the method called.

Go take your crack-addled brain back to your own thread you insane twit. There is no absolutley NO need for you to try to foist your crazy bullshit on anyone else. None.

**** off!

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 26

> As I've stated at least two or three times already in

> this thread: I realize that there may be a more rigid

> definition of "copy constructor" that isn't actually

> applicable in Java. However, formal definitions

> aside, everybody here except you has known from the

> first post that this is what the OP was talking about

> when he said "copy constructor." That is what we all

> mean in this context, and that is what is commonly

> meant in Java.

i understand your point: wheather to use a constructor or a method to do the copying. but please understand a constructor does not return anything, so only a method can do the work.

the copy constructor in c++ works with the assignmen operator. do not you know what the assignment operator is? it is this: =, with which you can use a copy constructor to do the copy. without that, how can you use a constructor to do the copy? you must be drunk! whats happening in your village? some chickens died or born again?

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 27

> i understand your point: wheather to use a

> constructor or a method to do the copying. but please

> understand a constructor does not return anything

I know that. You're cofused again. Nobody here ever said or implied that it does.

>, so

> only a method can do the work.

Or the new operator, in conjunction with an appropriate constructor, which I demonstrated a few posts ago.

>

> the copy constructor in c++ works with the assignmen

> operator. do not you know what the assignment

> operator is? it is this: =,

Irrelevant.

> can you use a constructor to do the copy?

I already showed you how. See post 19.

Yes, I know, the constructor isn't actually what's creating the object. But everybody here understands that, and that point is irrelevant to this discussion.

Are you ever able to follow any discussion?

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 28

> wrong!

>

> java collections uses the clone and newInstance

> methods. there is no copy constructor in java,

> meaning it does not exist in java. let me know if i

> could make it clearer.

>

> you have to have a copy constructor to pass an object

> by value.

No, it doesn't use clone. No, it doesn't use any method called newInstance. Yes, it does use a constructor that accepts another Collection, copying that Collection into the new instance the constructor is creating. This is a copy constructor, at least in the sense we're talking about. Would you care to define what you mean by "copy constructor" so there's no miscommunication? My definition:

"A constructor that takes a parameter of similar type and copies the contents/state to the new instance being created."

Not great wording, but close enough.

kablaira at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 29

> the sense we're talking about. Would you care to

> define what you mean by "copy constructor" so there's

> no miscommunication? My definition:

>

> "A constructor that takes a parameter of similar type

> and copies the contents/state to the new instance

> being created."

>

> Not great wording, but close enough.

Simple, to the point, fits what I was thinking--and surely everybody else but daf.

It will be interesting to see how he twists and misinterprets that, and what irrelevant point he latches onto as if it's the central thesis.

jverda at 2007-7-20 21:53:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 30
> how can you return it with a constructor?It doesn't have to. It's copying the parameter's contents/state to itself.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 31
> > how can you return it with a constructor?> > It doesn't have to. It's copying the parameter's> contents/state to itself.Oh, there's plenty for him to misunderstand, misinterpret, and take out of context there!Yeehaw.
jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 32
I know. I'm waiting for him to tell me ctor is not an object or some such.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 33
> I know. I'm waiting for him to tell me ctor is not> an object or some such.Maybe he'll say that ctors are passed by reference.
jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 34
> Maybe he'll say that ctors are passed by reference.I'm sure he'll come up with some irrelevent digression or explain to me that what I never even said isn't true.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 35

> > Maybe he'll say that ctors are passed by

> reference.

>

> I'm sure he'll come up with some irrelevent

> digression or explain to me that what I never even

> said isn't true.

I think Trey and Matt got their idea for the Chewbacca defense from daFfy.

jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 36
I don't get it. Who are they and what Chewbacca defense?
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 37

> > daFei... I hope you're not posting to this site

> > without your parent's permission.

>

> if your parents had given you permission to post

> here, they certainly made a mistake.

>

> a copy constructor is a special constructor, it works

> in conjuction with the assignment operator, as a

> regular constructor does not return anything, it is

> merely a function that is first called to create an

> instance.

Which would be true if any of the following were true

1. This forum was discussing C++.

2. Assignement in C++, versus initialization, did what you claimed that it does.

3. C++ only passed by reference. (Initialization is not the only place that the copy constructor is called in C++.)

4. That a C++ constructor or even java constructor created an object. (Initialization expresssions create objects, while constructors initialize the object.)

Since none of the above are true, it is rather hard to determine what you are talking about.

jschella at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 38

> I don't get it. Who are they and what Chewbacca

> defense?

Creators of South Park.

The Cewbacca defense is what Johnny Cohran used in one episode to distract and confuse the jurors.

http://www.planearium2.de/scripts-214.htm

Reporter:And so, on this fifteenth day of what is considered to be the most important trial of the …day, Johnny Cochran has appeared to defend Capitalist Records. The question now is, will Cochran use his famous "Chewbacca" defense?

Cartman:What's a Chewbacca defense?

Kyle:I don't know.

Stan:That's what Cochran used in the O.J. Simpson trial.

Cartman:[tosses away the empty Cheesy Poofs box] God-damned, I hate that Cochran guy. If he was here in front of me, I'd be like, "Ay! You stupid son of a *****, you d-. I b-. I'ma I'm gonna kick you in the nuts!"

Kyle:I'm sure that would scare the hell out of him, Cartman.

Gerald:[the trial is being aired live] And so, in summation, ladies and gentlemen of the jury, you've heard the version of my client's song recorded over twenty years ago. You've heard the EXACT SAME song produced by these cheats in the past month [shot of producer and Cochran]. I'd say it's pretty much an open-and-shut case. Make the right decision. Thank you. [one person claps. Gerald goes back to his table and tells Chef] I've got 'em. [Chef grins and gives his thumbs-up approval]

Judge Moses:[gavels] Mr. Johnny Cochran, your closing arguments.

Cochran:[rises and approaches the jury] Ladies and gentlemen of this supposed jury, Chef's attorney would certainly want you to believe that his client wrote "Stinky Britches" ten years ago. And they make a good case. Hell, I almost felt pity myself. But ladies and gentlemen of this supposed jury, I have one, final, thing I want you to consider. [walks to a display stand and pulls down a screen] Ladies and gentleman, this is Chew-bacca. [true] Chewbacca is a Wookie from the planet Kashyyyk, but Chewbacca… LIVES …on the planet Endor. Now think about that. That does NOT MAKE SENSE.

Gerald:[softly, pounds on the table] **** it!

Chef:[softly] What?

Gerald:[softly] He's using the Chewbacca defense!

Cochran:Why would a Wookie, an eight-foot tall Wookie, want to live on Endor, with a bunch of two-foot tall Ewoks? That does NOT MAKE SENSE! [the jury listens] But more important, you have to ask yourself: What does this have to do with this case? Nothing. [Gerald buries his head in his hand, shaking his haed at the disaster his case is being turned into] Ladies and gentlemen, it has nothing to do with this case! It does NOT MAKE SENSE! Look at me. I'm a lawyer defending a major record company, and I'm talkin' about Chewbacca! Does that make sense? Ladies and gentlemen, I am not making any sense! None of this makes sense! And so you have to remember, when you're in that jury room deliberatin' and conjugatin' the Emancipation Proclamation, [apporaches and softens] does it make sense? No! Ladies and gentlemen of this supposed jury, it does NOT MAKE SENSE! If Chewbacca lives on Endor, you must acquit! The defense rests. [walks back to his table]

Judge Moses:Oh-kay then.

Cartman:[working on a second box of Cheesy Poofs] Wow, he's good.

Reporter:In a teary-eyed courtroom, Johnny Cochran has just finished his closing arguments, and, as was anticipated, he did use the Chewbacca defense [drawings of the trial are shown] Whether or not it worked, is up to the jury to decide.

Judge Moses:How find you, the jury?

Jury Foreman:We find the defendant, Jerome "Chef" McElroy, guilty as charged.

Some observers:Ooo.

Gerald:Woops.

Chef:Woops?!

Judge Moses:Mr. Chef, you've been found guilty of harassing a major record label. The full fee of $2 million will be handed over within twenty-four hours.

Chef:Do I look like I have $2 million?

Judge Moses:Well, you have twenty-four hours to find it, or else you'll have to go to jail. For eight million years! [Chef is agape]

Bailiff:[whispering into Judge Moses' ear] Uh, sir, it's for four years. [leaves]

Judge Moses:Oh. Sorry. You'll go to jail for four years.

jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 39

>

> i understand your point: wheather to use a

> constructor or a method to do the copying. but please

> understand a constructor does not return anything, so

> only a method can do the work.

>

> the copy constructor in c++ works with the assignmen

> operator. do not you know what the assignment

> operator is? it is this: =, with which you can use a

> copy constructor to do the copy. without that, how

> can you use a constructor to do the copy? you must be

> drunk! whats happening in your village? some chickens

> died or born again?

Wrong. Wrong. Wrong.

The following is an initialization expression in C++ which has nothing to do with assignment. However I am sure that you will be incapable of understanding that.

MyClass myClass2 = myClass1;

The following is an assignment expression.

myClass2 = myClass1;

And the difference is that one will call the assignment operator while the other will call the copy constructor.

However that isn't the only place where a copy constructor might be implicitly used.

jschella at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 40
**** not another cartoon. I'm still stuck on the 80's and 90's anime my lady makes me watch. It'll be decades before we make it to modern day South Park episodes.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 41
**** is not a badword. Although rap should be.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 42

> **** not another cartoon. I'm still stuck on the

> 80's and 90's anime my lady makes me watch. It'll be

> decades before we make it to modern day South Park

> episodes.

<Cartman>

If my lady tried to do that to me I'd be all like, "Hey! We're gonna watch what I want! Now get me some Cheezy Poofs or I'll kick you square in the nuts!"

</Cartman>

jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 43
> **** not another cartoon. I'm still stuck on the> 80's and 90's anime my lady makes me watch.Which anime?
jverda at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 44
...but then she'd kill Kenny.
kablaira at 2007-7-20 21:53:07 > top of Java-index,Other Topics,Patterns & OO Design...
# 45
Ranma, Inuyasha, Escaflone, Samurai Shamploo, Wolf's Rain and God knows what else. Those are just the ones I can remember the name of and I probably misspelled all of them.
kablaira at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 46

> Ranma, Inuyasha, Escaflone, Samurai Shamploo, Wolf's

> Rain and God knows what else. Those are just the ones

> I can remember the name of and I probably misspelled

> all of them.

None of those sound familiar (except maybe Ranma). I'm not into anime, but my kids watch a few of them. I think the ones they watch are more current, although they could be from the 80s and 90s for all I know.

jverda at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 47
> ...but then she'd kill Kenny.That *******!(kidding, of course--just couldn't leave it stand)
jverda at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 48
There are things she watches that my 7 year old nephew won't even watch anymore.
kablaira at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 49
> > how can you return it with a constructor?> > It doesn't have to. It's copying the parameter's> contents/state to itself.if it doesnt matter, you might want to use the finalizer to do the copying too.
kablaira at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 50

> Which would be true if any of the following were

> true

> 1. This forum was discussing C++.

true. as there is no copy constrctor in java, it could only be on c++.

> 2. Assignement in C++, versus initialization, did

> what you claimed that it does.

i have been explctly clear that it is assignment, the assignment operator, not initilization which works using the address/pointer.

> 3. C++ only passed by reference. (Initialization is

> not the only place that the copy constructor is

> called in C++.)

this is totally wrong. c++ lets you optionally pass by reference or by value. initilization is the only place where a copy constructor is NOT called. it is called only in assignment, NOT in initilization.

> 4. That a C++ constructor or even java constructor

> created an object. (Initialization expresssions

> create objects, while constructors initialize the

> object.)

>

did you just discover this?

> Since none of the above are true, it is rather hard

> to determine what you are talking about.

except where you baaadly errored, as noted, all otheres are true. if you still dont have a clue, the problems lie somewhere else.

kablaira at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 51

> if it doesnt matter, you might want to use the

> finalizer to do the copying too.

Could you be more incoherent? Who said it doesn't matter where it's done. What doesn't matter is that a ctor can't return anything. Guess what? We don't want to return anything. We want to copy whatever is in the other object to our new object during intialization and we can do this by passing the old object to it as a parameter and letting it copy what it needs. This is not a "copy constructor" as defined in C++, but it is what everyone but you means when they say "copy constructor" in this thread. Whether or not there is a broader definition of "copy constructor" in CS in general (i.e. not specific to C++) is another issue. If there is, we should probably find another term, if there isn't then we don't need to. Either way, everyone but you understands what we mean by the term in this thread.

kablaira at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 52

> > > how can you return it with a constructor?

> >

> > It doesn't have to. It's copying the parameter's

> > contents/state to itself.

>

> if it doesnt matter, you might want to use the

> finalizer to do the copying too.

Did I call it or what?

daFei, you make no sense. You understand nothing. Everything you say is either wrong or irrelevant or both.

To get back to the the fundamentals of this thread (which you have conveniently ignored): One of the ways to make a copy of an object in Java is with a "copy constructor." (I put that term in quotes because I am admitting that I might not be using it correctly.) What we mean by "copy constructor" here is, as I showed (back in reply 19 or so I think), a constructor that takes an object of the same type to use for intializing the state of the new object.

It works. It is used all over the place, including in the core API. Say it's not proper to call it a "copy constructor" if you want--I don't care. But that is what people here mean, and we all understand that.

jverda at 2007-7-20 21:53:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 53

> true. as there is no copy constrctor in java, it

> could only be on c++.

Unless "copy constructor" is not a well-defined term in CS. It being well-defined in C++ is irrelevent. This is not C++, it's terms do not apply. What matters is the definition in the context of Java, and that may be up for debate. What is not up for debate is that everyone in this thread but you is using a definition very different from what the definition is in C++ and hence the definition in C++ is irrelevent.

kablaira at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 54

> > Which would be true if any of the following were

> > true

> > 1. This forum was discussing C++.

> true. as there is no copy constrctor in java, it

> could only be on c++.

Assuming you meant "constructor", and further assuming you somehow come into some sort of reading for comprehension skills, and assuming you won't just ignore this (since jverd has already addressed this several times and you continue to ignore his posts instead electing to continue to be purposely obstuse) let me demonstrate what people usually mean when they talk about a copy constructor in Java:

http://www.artima.com/intv/bloch13.html

http://www.javapractices.com/Topic12.cjp

http://www.codeguru.com/java/tij/tij0128.shtml (search for "copy-constructor)

http://www.agiledeveloper.com/articles/cloning072002.htm

http://en.wikipedia.org/wiki/Copy_constructor

Hope this helped (but I know it didn't)

tsitha at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 55

http://www.javapractices.com/Topic12.cjp

There's at least one source backing up our definition of "copy constructor" in the context of Java. Admittedly I have no idea how "authorative" it is. It's still more than the authorative sources daFei has provided on the definition of copy constructor in the context of Computer Science rather than just C/C++.

kablaira at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 56

> http://www.javapractices.com/Topic12.cjp

>

> There's at least one source backing up our definition

> of "copy constructor" in the context of Java.

> Admittedly I have no idea how "authorative" it is.

> . It's still more than the authorative sources daFei

> has provided on the definition of copy constructor in

> the context of Computer Science rather than just

> C/C++.

daf has ever provided sources for anything?

jverda at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 57

> daf has ever provided sources for anything?

Yes. There's been one or two cases of him providing a source, such as a C++ article, where the author said something inaccurate about Java. Note that both cases were articles written by C++ developers and one was in MSDN and neither actually had anything to do with Java, so it's no surprise they got something wrong.

He's also posted many other sources that contradicted what he said entirely. Remember Lafore and the way he took him out of context to try and twist his words around to mean the exact opposite of what his book actually said?

kablaira at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 58

Wikipedia says:

"A copy constructor is a constructor which takes a (single) parameter of an existing object of the same type as the constructor's class, and returns a deep copy of the object sent as a parameter."

Of course, this isn't actually true in PHP3 or C++ as far as I'm aware. Last I checked, by default they actually perform a shallow copy. So that leads one to the conclusion that either Wikipedia's definition is wrong or C++/PHP3 have default implementations of a copy constructor that don't actually meet the definition of copy constructor.

kablaira at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 59
Let's also not forget that every definition I can find really boils down to "Copying the state of one object to a new object." which is exactly what the ctors we're talking about do.
kablaira at 2007-7-20 21:53:13 > top of Java-index,Other Topics,Patterns & OO Design...
# 60

> http://www.artima.com/intv/bloch13.html

> http://www.javapractices.com/Topic12.cjp

> http://www.codeguru.com/java/tij/tij0128.shtml

> (search for "copy-constructor)

> http://www.agiledeveloper.com/articles/cloning072002.h

> tm

> http://en.wikipedia.org/wiki/Copy_constructor

>

> Hope this helped (but I know it didn't)

those are merely tricks they play in the village, not at all a copy constructor.

at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 61
OHMYGOD!!!!!!Is there any way in Sun's forum software to ignore a given user?
dannyyatesa at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 62
> OH> MY> GOD!!!!!!> is god yours?
dannyyatesa at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 63

> > http://www.artima.com/intv/bloch13.html

> > http://www.javapractices.com/Topic12.cjp

> > http://www.codeguru.com/java/tij/tij0128.shtml

> > (search for "copy-constructor)

> >

> http://www.agiledeveloper.com/articles/cloning072002.h

>

> > tm

> > http://en.wikipedia.org/wiki/Copy_constructor

> >

> > Hope this helped (but I know it didn't)

>

> those are merely tricks they play in the village, not

> at all a copy constructor.

daFei, why don't you try to say something that actually means something for a change?

I'll try to keep it simple for you:

* In Java, it is possible, and is common practice, to provide a constructor that can initialize a new object's state from the state of an existing object of the same type.

* That kind of constructor is commonly called a "copy constructor."

* The term "copy constructor" may or may not have more standard definition in CS that doesn't fit this Java scenario. Everybody here recognizes that. Nobody here cares.

* Nothing you have said here is relevant. Much of it is also incorrect.

jverda at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 64
> is god yours?Given that you know both the C++ and Java languages in such intimate and inscrutable detail, why is that you don't apply this same level of fastidiousness to your use of the English language?
dannyyatesa at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 65

> > http://www.artima.com/intv/bloch13.html

> > http://www.javapractices.com/Topic12.cjp

> > http://www.codeguru.com/java/tij/tij0128.shtml

> > (search for "copy-constructor)

> >

> http://www.agiledeveloper.com/articles/cloning072002.h

>

> > tm

> > http://en.wikipedia.org/wiki/Copy_constructor

> >

> > Hope this helped (but I know it didn't)

>

> those are merely tricks they play in the village, not

> at all a copy constructor.

You know, I didn't think you'd understand that material - but I really thought you'd come up with something more interesting than that reply. Did you notice in the PBR thread that you were given references (get it - references) from James Gosling - the guy who's responsible for the language spec - and you disagreed, and here the first reference showing the use of the phrase "copy constructor" is from Josh Bloch, whose name you'll see under the "@author" tag should you crack open src.zip and look at the source for java.util.ArrayList, and you disagree with him as well. Do you not see that maybe you're just frikkin' wrong?

No, I know you don't.

tsitha at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 66

> Did you

> notice in the PBR thread that you were given

> references (get it - references) from James Gosling -

> the guy who's responsible for the language spec - and

> you disagreed, and here the first reference showing

> the use of the phrase "copy constructor" is from Josh

> Bloch, whose name you'll see under the "@author" tag

> should you crack open src.zip and look at the source

> for java.util.ArrayList, and you disagree with him as

> well. Do you not see that maybe you're just frikkin'

> wrong?

>

becuase they are big guys in the industry does not mean they are correct on everything. gosling invented the java language, but he did not invent pass by reference/value, programming styles or approaches can be looked at and debated about. differing with them does not mean in any way 'frikkin' wrong. as a matter of fact, there are just too many people who gracefully explain why java passes objects by reference, not by value. they are mostly outside your village. you probablly dont see them as you dont see much outside your village. bloch's approach on 'copy constructor' is nothing but stupid tricks, it dos not mean anything meaningful, and you can crash his code easily using inheritance. the clone method in java is the most equalvlent to the copy constructor in c++, and i think it is a good idea not to provide the default copy constructor in java for more flecibility.

read freud's words as i posted before so that you can understand why you keep asking for authority, and that is becuase you do not understand it.

tsitha at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 67

> > Did you

> > notice in the PBR thread that you were given

> > references (get it - references) from James Gosling

> -

> > the guy who's responsible for the language spec -

> and

> > you disagreed, and here the first reference

> showing

> > the use of the phrase "copy constructor" is from

> Josh

> > Bloch, whose name you'll see under the "@author"

> tag

> > should you crack open src.zip and look at the

> source

> > for java.util.ArrayList, and you disagree with him

> as

> > well. Do you not see that maybe you're just

> frikkin'

> > wrong?

> >

>

> becuase they are big guys in the industry does not

> mean they are correct on everything.

But that's not the point. The point is that, as you go on to say, Gosling invented the language. When discussing what the language does his opinion is heavily weighted. And if he says "it does it this way" then, unless you can come up with some seriously rock solid evidence to the contrary, that's how it works by definition.

> gosling invented

> the java language, but he did not invent pass by

> reference/value, programming styles or approaches can

> be looked at and debated about. differing with them

> does not mean in any way 'frikkin' wrong. as a matter

> of fact, there are just too many people who

> gracefully explain why java passes objects by

> reference, not by value. they are mostly outside your

> village.

They must be outside everyone's village, as you seem unable to point us to all but a two (incorrect and/or irrelevant) sources.

> you probablly dont see them as you dont see

> much outside your village. bloch's approach on 'copy

> constructor' is nothing but stupid tricks,

But you'll need to make something approximating a coherent, cogent argument to back up that assertion, since Bloch wrote the Collections framework, whilst you are busy arguing that Gosling doesn't know what he's talking about wrt his own language and that the "finally" keyword is superflous.

> it dos not

> mean anything meaningful, and you can crash his code

> easily using inheritance.

Care to expound on this, or is this yet another instance of you talking out your butt?

> the clone method in java is

> the most equalvlent to the copy constructor in c++,

> and i think it is a good idea not to provide the

> default copy constructor in java for more

> flecibility.

>

What are you talking about - not to provide the default copy constructor in java (which, if I understand you correctly, you don't think exists) for more flexibility? What does that mean?

> read freud's words as i posted before so that you can

> understand why you keep asking for authority, and

> that is becuase you do not understand it.

I am not just asking for authority - I am asking that you back up your assertions with some sort of reference or even a logical argument. You might consider taking a course on critical thinking.

tsitha at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 68
> those are merely tricks they play in the village, not> at all a copy constructor.This is exactly what it is in the context of JAVA. This is not C++, this is not PHP3, their definitions of copy constructors are irrelevent.
kablaira at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 69

> becuase they are big guys in the industry does not

> mean they are correct on everything. gosling invented

> the java language, but he did not invent pass by

> reference/value, programming styles or approaches can

> be looked at and debated about. differing with them

> does not mean in any way 'frikkin' wrong. as a matter

> of fact, there are just too many people who

> gracefully explain why java passes objects by

> reference, not by value. they are mostly outside your

> village. you probablly dont see them as you dont see

> much outside your village. bloch's approach on 'copy

> constructor' is nothing but stupid tricks, it dos not

> mean anything meaningful, and you can crash his code

> easily using inheritance. the clone method in java is

> the most equalvlent to the copy constructor in c++,

> and i think it is a good idea not to provide the

> default copy constructor in java for more

> flecibility.

>

> read freud's words as i posted before so that you can

> understand why you keep asking for authority, and

> that is becuase you do not understand it.

No, but he did state that Java does not have pass-by-reference and only has pass-by-value and he was using the definitions accepted by the entire CS community. The only person who doesn't use those definitions is you. The problem here is you don't see outside of your village.The entire world sees things differently and your little village is completely backwards.Your little village is irrational, illogical and inaccurate. The only person in your village is you, daFei, the rest of the world thinks differently.

kablaira at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 70

> becuase they are big guys in the industry does not

> mean they are correct on everything. gosling invented

> the java language, but he did not invent pass by

> reference/value, programming styles or approaches can

> be looked at and debated about. differing with them

> does not mean in any way 'frikkin' wrong. as a matter

> of fact, there are just too many people who

> gracefully explain why java passes objects by

> reference, not by value. they are mostly outside your

> village. you probablly dont see them as you dont see

> much outside your village. bloch's approach on 'copy

> constructor' is nothing but stupid tricks, it dos not

> mean anything meaningful, and you can crash his code

> easily using inheritance. the clone method in java is

> the most equalvlent to the copy constructor in c++,

> and i think it is a good idea not to provide the

> default copy constructor in java for more

> flecibility.

>

> read freud's words as i posted before so that you can

> understand why you keep asking for authority, and

> that is becuase you do not understand it.

Daf, you are a nutter.

The definitions of PBV and PBR are not up for debate. They are clear and simple and standard, and Gosling and everybody here but you uses them consistently. For anyone who understands those definitions and understands the Java language, there is no question--Java passes primitves by value and references by value and does not pass objects or anything else$ by reference.

Your comments on the copy constructor are even stupider. Everybody here except you knows exactly what we're talking about when we say "copy constructor" in a Java context. It's a common practice, it works well, and it's no more likely to "crash your code" than cloning.

You are a nutter.

jverda at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 71

> > Which would be true if any of the following were

> > true

> > 1. This forum was discussing C++.

> true. as there is no copy constrctor in java, it

> could only be on c++.

There is nothing in java that prevents one from creating a constructor that has the same code as an equivalent "copy constructor" in a C++ class.

The difference only shows up in how the language implicitly applies a copy constructor.

>

> > 2. Assignement in C++, versus initialization, did

> > what you claimed that it does.

>

> i have been explctly clear that it is assignment, the

> assignment operator, not initilization which works

> using the address/pointer.

>

And have have been explicitly clear that the copy constructor has nothing to do with assignment.

> > 3. C++ only passed by reference. (Initialization is

> > not the only place that the copy constructor is

> > called in C++.)

>

> this is totally wrong. c++ lets you optionally pass

> by reference or by value. initilization is the only

> place where a copy constructor is NOT called. it is

> called only in assignment, NOT in initilization.

>

1. I said if that were true. I didn't say it was true.

2. The copy constructor is called in the initialization expression. It is not called in the assignment expression.

MyClass myClass2 = myClass1; // this is initialization - copy ctor called.

myClass2 = myClass1; // This is assignment - the copy ctor is not called.

>

> > 4. That a C++ constructor or even java constructor

> > created an object. (Initialization expresssions

> > create objects, while constructors initialize the

> > object.)

> >

> did you just discover this?

>

Apparently you just figured it out since you said the following....

--

a copy constructor is a special constructor, it works

in conjuction with the assignment operator, as a

regular constructor does not return anything, it is

merely a function that is first called to create an

instance.

> > Since none of the above are true, it is rather hard

> > to determine what you are talking about.

>

> except where you baaadly errored, as noted, all

> otheres are true. if you still dont have a clue, the

> problems lie somewhere else.

The problem lies entirely with you.

jschella at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 72

i am tired to argue about the same thing again and agin, but the following is so baaaadly wrong i have to...

> 2. The copy constructor is called in the

> initialization expression. It is not called in the

> assignment expression.

>

so funny. you got it exactly reversed.

> MyClass myClass2 = myClass1; // this is

> initialization - copy ctor called.

if this is initiklization, then

in c++:

the copy constructor is NOT called. what happens here is merely an exchange of address in the format of pointer from myclass1 to myclass2.

in java:

exactly the same as that in c++. the copy constructor is NOT called, as such a thing does not exist in java.

> myClass2 = myClass1; // This is assignment - the copy

> ctor is not called.

>

if this is an assignement,

in c++:

the copy constructor is called and it does a memberwise copy, meaning myclass2 is replaced by myclass1.

in java: no copy constructor is called, as such a thing does not exist in java, merely an exchange of the address in the format of pointer.

> The problem lies entirely with you.

yes, it is with you, no doubt about it.

jschella at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 73

> > those are merely tricks they play in the village, not

> > at all a copy constructor.

>

> This is exactly what it is in the context of

> JAVA. This is not C++, this is not PHP3,

> their definitions of copy constructors are irrelevent.

if so, why dont you just stuff your constructor with the clone method to make a copy constructor' that you so badly wnat?

jschella at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 74

> Daf, you are a nutter.

>

no, but you are.

> The definitions of PBV and PBR are not up for debate.

correct.

> They are clear and simple and...

not to you, obviously.

> there is no

> question--Java passes primitves by value and

> references by value

and where did you get the impression that java pssses references by value? gosling said they are passed 'by value'. pay attentin to the quotes around it, because gosling knew its too much for your brain, he wanted to make thing simple for you vilaage chickens.

>

> Your comments on the copy constructor are even

> stupider. Everybody here except you knows exactly

> what we're talking about when we say "copy

> constructor" in a Java context. It's a common

> practice, it works well, and it's no more likely to

> "crash your code" than cloning.

>

if so, whay dont you just stuff your constructor with the clone() method? isnt this easier? for your brain....

> You are a nutter.

you are chicken.

jschella at 2007-7-20 21:53:18 > top of Java-index,Other Topics,Patterns & OO Design...
# 75

[snip]

> > 2. The copy constructor is called in the

> > initialization expression. It is not called in

> the

> > assignment expression.

> >

> so funny. you got it exactly reversed.

What's funny is that, though I've not worked with C++ in 9 years now even I know he got it right.

>

> > MyClass myClass2 = myClass1; // this is

> > initialization - copy ctor called.

>

> if this is initiklization,

If? You don't know?

[snip]

How can you be so wrong so consistently across multiple topics?

tsitha at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 76

> I'll try to keep it simple for you:

>

> * In Java, it is possible, and is common practice, to

> provide a constructor that can initialize a new

> object's state from the state of an existing object

> of the same type.

>

you just dicovered this?

> * That kind of constructor is commonly called a "copy

> constructor."

>

by whom? gosling? or verdyling?

> * The term "copy constructor" may or may not have

> more standard definition in CS

may or may not? why are you so uncertain this time?

> that doesn't fit this

> Java scenario. Everybody here recognizes that. Nobody

> here cares.

>

why it does not fit in java?

tsitha at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 77

> There is nothing in java that prevents one from

> creating a constructor that has the same code as an

> equivalent "copy constructor" in a C++ class.

>

you simply can NOT create such a copy constructor in java, as it requires you overload the assignment operator.

tsitha at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 78

> > > those are merely tricks they play in the village,

> not

> > > at all a copy constructor.

> >

> > This is exactly what it is in the context of

> > JAVA. This is not C++, this is not PHP3,

> > their definitions of copy constructors are

> irrelevent.

>

> if so, why dont you just stuff your constructor with

> the clone method to make a copy constructor' that you

> so badly wnat?

In other words, you still don't understand what's going on.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 79

> and where did you get the impression that java pssses

> references by value?

As I already told you: From several reputable sources, including the JLS and the creator of the language, as well as from observed behavior.

> gosling said they are passed 'by

> value'. pay attentin to the quotes around it,

No, I won't, and you shouldn't either. They are used (incorrectly) for emphasis. Contrary to what your sad, confused little mind thinks, they are not used to mean "but actually I mean the opposite."

> > Your comments on the copy constructor are even

> > stupider. Everybody here except you knows exactly

> > what we're talking about when we say "copy

> > constructor" in a Java context. It's a common

> > practice, it works well, and it's no more likely

> to

> > "crash your code" than cloning.

> >

> if so, whay dont you just stuff your constructor with

> the clone() method? isnt this easier? for your

> brain....

So you either still don't understand anything, or you've realzed that you're wrong. Either way, you're frustrated, so you decide to lash out with a nonsensical insult, as opposed to your usual nonsensical comments on the topic at hand.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 80

> > I'll try to keep it simple for you:

> >

> > * In Java, it is possible, and is common practice,

> to

> > provide a constructor that can initialize a new

> > object's state from the state of an existing

> object

> > of the same type.

> >

> you just dicovered this?

Obviously not. I've been saying it for the whole thread. You don't seem to be able to grasp the concept, however.

>

> > * That kind of constructor is commonly called a

> "copy

> > constructor."

> >

> by whom? gosling? or verdyling?

I don't know about Gosling, but I use that term, as does just about everybody else in this thread, all of whom have proven themselves to be much more knowledgeable than you, and so do many other people I've read or heard.

I'm not saying that term is correct. I don't know or care if it is. I'm only saying that it's in common use and everybody in this thread except you has known from the beginning what was meant.

>

> > * The term "copy constructor" may or may not have

> > more standard definition in CS

> may or may not? why are you so uncertain this time?

>

> > that doesn't fit this

> > Java scenario. Everybody here recognizes that.

> Nobody

> > here cares.

> >

> why it does not fit in java?

I didn't say it doesn't. I said it MIGHT not. Learn to read. I also said that for this thread, it doesn't matter whether it does or not because everybody here except you knows what is meant by it.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 81

> > There is nothing in java that prevents one from

> > creating a constructor that has the same code as

> an

> > equivalent "copy constructor" in a C++ class.

> >

> you simply can NOT create such a copy constructor in

> java, as it requires you overload the assignment

> operator.

Wrong.

I demonstrated such a constructor very early in this thread.

Note that jschell didn't say that that ctor would be invoked in the same situations as a C++ copy ctor, only that its body could contain the equivalent code. Learn to read.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 82

> by whom? gosling? or verdyling?

The Java community at large. Your C++ definitions are irrelevent here little boy.

> may or may not? why are you so uncertain this time?

We've always been uncertain if there is a well-defined term in CS that our Java definition doesn't adhere to. The only definitions we can find in CS (meaning not specific to C++) are definitions that essentially define it as copying the state from one object to another, which our definition fits in perfectly with. You still have not provided a single definition in the context of CS that contradicts this. Your definitions are specific to C++, and a definition specific to C++ is irrelevent in a conversation about Java.

> why it does not fit in java?

Learn to read the entire sentence retard.

kablaira at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 83

> > * The term "copy constructor" may or may not have

> > more standard definition in CS

> may or may not? why are you so uncertain this time?

Because I haven't bothered to look into it, and I don't care enough about it right now to do so. Whether such a standard definition exists is not relevant to me right now.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 84

>

> > 2. The copy constructor is called in the

> > initialization expression. It is not called in the

> > assignment expression.

> >

> so funny. you got it exactly reversed.

>

> > MyClass myClass2 = myClass1; // this is

> > initialization - copy ctor called.

>

> if this is initiklization, then

>

> in c++:

> the copy constructor is NOT called. what happens here

> is merely an exchange of address in the format of

> pointer from myclass1 to myclass2.

>

Quite obviously you have never coded in C++.

The above C++ code does not have pointers at all. The code makes that obvious. The expression implicitly invokes the copy constructor of MyClass. That constructor might exist either explicitly or implicitly.

> in java:

> exactly the same as that in c++. the copy constructor

> is NOT called, as such a thing does not exist in

> java.

>

Obviously you can not differentiate between the method called "copy constructor" and how that method is used in an expression.

The following code can be written in either C++ or java. Both are equivalent. Both represent the same thing. Both can be called a copy constructor. As I pointed out, and as you are incapable of understanding, the difference in C++ is in how C++ implicitly uses the copy constructor in expressions. Java does not use it implicitly. That is the difference. That is the only difference.

public class MyClass

{

private int i;

public MyClass()

{

i = 0;

}

public MyClass(MyClass c)

{

this.i = c.i;

}

}

> > myClass2 = myClass1; // This is assignment - the copy

> > ctor is not called.

> >

> if this is an assignement,

> in c++:

> the copy constructor is called and it does a

> memberwise copy, meaning myclass2 is replaced by

> myclass1.

>

Wrong.

I doubt you understand how C++ works.

There is a very small possibility that you have confused yourself into reversing when the asssigment operator versus when the copy constructor is used.

When an initialization expression is invoked in C++ it uses a copy constructor. The copy constructor is called. The default copy constructor does a memberwise copy. That does not alter the fact that the copy constructor is called. The C++ compiler creates the default copy constructor just like a java compiler creates a default constructor.

For an assignment expression the same thing is true.

The following C++ code demonstrates exactly what I have said.

#include "stdafx.h"

class MyClass

{

int i;

public:

MyClass()

{

i = 0;

}

MyClass(MyClass& c)

{

printf("Copy constructor\n");

i = c.i;

}

MyClass& operator=(MyClass& c)

{

printf("Assignment operator\n");

i = c.i;

return *this;

}

};

int main(int argc, char* argv[])

{

printf("Start\n");

MyClass c1;

printf("Initialization expression\n");

MyClass c2 = c1;

printf("Assignment expression\n");

c2 = c1;

printf("End\n");

return 0;

}

Output -

Start

Initialization expression

Copy constructor

Assignment expression

Assignment operator

End

> in java: no copy constructor is called, as such a

> thing does not exist in java, merely an exchange of

> the address in the format of pointer.

>

Java doesn't use it in expressions. The code above however is a copy constructor.

jschella at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 85

> > There is nothing in java that prevents one from

> > creating a constructor that has the same code as an

> > equivalent "copy constructor" in a C++ class.

> >

> you simply can NOT create such a copy constructor in

> java, as it requires you overload the assignment

> operator.

And now it is quite obvious why you are so confused.

Seriously have you ever programmed in any language at all? Have you even written a hello world program?

If yes then have you ever worked with even one other programmer that you actually talked to?

jschella at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 86
> If yes then have you ever worked with even one other> programmer that you actually talked to?More to the point: listened to.
jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 87
> > If yes then have you ever worked with even one> other> > programmer that you actually talked to?> > More to the point: listened to.yeah, to the point: listen up now - you are very confused.
jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 88

> The above C++ code does not have pointers at all.

> The code makes that obvious.

the assumption was since we are making comparisons among java and c++, the c++ code should be references too, but never mind, i did put a condition that 'if it is initilization'. see below for more.

> The expression

> n implicitly invokes the copy constructor of

> MyClass. That constructor might exist either

> r explicitly or implicitly.

>

true.

> > in java:

> > exactly the same as that in c++. the copy constructor

> > is NOT called, as such a thing does not exist in

> > java.

> >

>

> Obviously you can not differentiate between the

> method called "copy constructor" and how that method

> is used in an expression.

>

hows it so?

> The following code can be written in either C++ or

> java. Both are equivalent. Both represent the same

> thing. Both can be called a copy constructor. As I

> pointed out, and as you are incapable of

> understanding, the difference in C++ is in how C++

> implicitly uses the copy constructor in expressions.

> Java does not use it implicitly. That is the

> e difference. That is the only difference.

>

> >public class MyClass

>{

> private int i;

>

> public MyClass()

> {

> i = 0;

> }

>

> public MyClass(MyClass c)

> {

> this.i = c.i;

> }

>}

>

>

> > > myClass2 = myClass1; // This is assignment - the copy

> > > ctor is not called.

> > >

> > if this is an assignement,

> > in c++:

> > the copy constructor is called and it does a

> > memberwise copy, meaning myclass2 is replaced by

> > myclass1.

> >

>

> Wrong.

>

you are wrong.

> I doubt you understand how C++ works.

>

likewise.

> There is a very small possibility that you have

> confused yourself into reversing when the asssigment

> operator versus when the copy constructor is used.

>

the possibility that you are confused is BIG and OBVIOUS.

> When an initialization expression is invoked in C++

> it uses a copy constructor.

that depends on what the 'type' is. if it a value variable, this is true; otherwise it is false.

> The copy constructor

> is called.

only on value variables, not on references.

>The default copy constructor does

> a memberwise copy. That does not alter the fact that

> the copy constructor is called. The C++ compiler

> creates the default copy constructor just like a java

> compiler creates a default constructor.

>

aqgain only on value variables, not on references.

> For an assignment expression the same thing is true.

>

only on value variables.

> The following C++ code demonstrates exactly what I

> have said.

>

> >#include "stdafx.h"

>

>class MyClass

>{

>int i;

>

>public:

>MyClass()

>{

>i = 0;

>}

>

>MyClass(MyClass& c)

>{

>printf("Copy constructor\n");

>i = c.i;

>}

>

>MyClass& operator=(MyClass& c)

>{

>printf("Assignment operator\n");

>i = c.i;

>

>return *this;

>}

>};

>

>int main(int argc, char* argv[])

>{

>printf("Start\n");

>MyClass c1;

>

>printf("Initialization expression\n");

>MyClass c2 = c1;

>

WRONG here!. this is not an initilization as c2 is a value variable, not a reference. what this line does is the same as the following line. they BOTH are assignment, not initilization.

>printf("Assignment expression\n");

>c2 = c1;

>printf("End\n");

>

>return 0;

>}

>

>Output -

>Start

>Initialization expression

this is not initilization, this is an assignment!

>Copy constructor

>Assignment expression

>Assignment operator

>End

>

>

> > in java: no copy constructor is called, as such a

> > thing does not exist in java, merely an exchange of

> > the address in the format of pointer.

> >

>

> Java doesn't use it in expressions. The code above

> however is a copy constructor.

garbage.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 89

> Seriously have you ever programmed in any language at

> all? Have you even written a hello world program?

>

i have programmed in c/c++ and java, not much else.

> If yes then have you ever worked with even one other

> programmer that you actually talked to?

yes. I have worked with a few different groups. i admired and learnt tremendously from one group of c/c++ plus one java group, the rest are unfortunately garbage developers who can not comprehend what an object is, they prefer the 'dao pattern', seperating data from functionality. it oftentimes hurts me when i see dum basses abuse oop but i have to work with them this way, as they dont seem to be able to pick up oo concepts and dont even appear to be willing to, and there is no critical business requests that require proper oo design and implementation, everything is team work and a job.

jverda at 2007-7-20 21:53:23 > top of Java-index,Other Topics,Patterns & OO Design...
# 90

> >#include "stdafx.h"

>

>class MyClass

>{

>int main(int argc, char* argv[])

>{

>printf("Start\n");

>MyClass c1;

>

>printf("Initialization expression\n");

>MyClass c2 = c1;

i need to make it easier to read so that jschell's trick wont get missed:)

t this is NOT an initilization: MyClass c2 = c1; because c1 and c2 are value variables, not references. value variables on object do NOT need to be initilized, there is no such a thing at all!. when do myclass c1 or c2, the objects are already created, there is no such a thing as to initilize them. had them been rereferences, then yes.

jschell, do you have any better tricks? this is so dumb and boring, only verdys and the lokos would be excited:)

>printf("Assignment expression\n");

>c2 = c1;

>printf("End\n");

>

>return 0;

>}

>

>Output -

>Start

>Initialization expression

again, this is NOT initilization, it is the same as the folloing line.

>Copy constructor

>Assignment expression

>Assignment operator

>End

at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 91
> If? You don't know?now you should know why if.> [snip]snip
at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 92

daFei > value variables on object do NOT need to be initilized, there is

daFei > no such a thing at all!.

Stroustrup > initialization - giving an object an initial value.

Speaking about MyClass c2 = c1;

daFei > this is not initilization, this is an assignment!

Stroustrup > Initialization differs from assignment in that there is no

Stroustrup > previous value involved.

So, daFei, the "previous value" of c2 is what exactly?

Stroustrup > Initialization is done by constructors.

In this case, the copy constructor as shown in jschell's example.

Which, daFei, only leaves the question of how you'll pidgeonhole

(roost?) poor old Bjarne. Is he another "dum bass abusing oop"? Or

one of those "authorities" Sigmund warned you about?

pbrockway2a at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 93
http://public.research.att.com/~bs/glossary.html
pbrockway2a at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 94

> > The above C++ code does not have pointers at all.

> > The code makes that obvious.

>

> the assumption was since we are making comparisons

> among java and c++, the c++ code should be references

> too,

Nope.

C++ references are nothing like Java references.

> garbage.

In other words, you're either incapable of understanding it, or you realize that it shows how wrong you are.

jverda at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 95
> yes. I have worked with a few different groups. i> admired and learnt tremendously from one group of> c/c++ plus one java groupNo, you haven't learned anything.
jverda at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 96

> > > >#include "stdafx.h"

> >

> >class MyClass

> >{

> >int main(int argc, char* argv[])

> >{

> >printf("Start\n");

> >MyClass c1;

> >

> >printf("Initialization expression\n");

> >MyClass c2 = c1;

>

>

> i need to make it easier to read so that jschell's

> trick wont get missed:)

There's no trick. You just don't understand anything.

> t this is NOT an initilization:

Wrong.

> MyClass c2 =

> c1; because c1 and c2 are value variables, not

> references.

Doesn't matter. c2 is a variable, and it's being initialized.

> value variables on object do NOT need to

> be initilized,

True. But they can be.

> there is no such a thing at all!.

Wrong.

jverda at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 97

> Stroustrup > initialization - giving an object an

> initial value.

>

thats true; but the ini values are not done using constructors, not copy constrcutors - they copy values from alrady initilized objects onto already initilized objects only, not initilize anything.

> Speaking about MyClass c2 = c1;

> daFei > this is not initilization, this is an

> assignment!

>

correct, because:

> Stroustrup > Initialization differs from

> assignment in that there is no

> Stroustrup > previous value involved.

>

> So, daFei, the "previous value" of c2 is what

> exactly?

>

the previouse values are done using c2's constructors, in this case it is the default constructor: MyClass(){...}.

> Stroustrup > Initialization is done by

> constructors.

>

yes.

> In this case, the copy constructor as shown in

> jschell's example.

>

nope. initilization is NEVER done by copy constructors.

> Which, daFei, only leaves the question of how you'll

> pidgeonhole

> (roost?) poor old Bjarne. Is he another "dum bass

> abusing oop"? Or

> one of those "authorities" Sigmund warned you about?

he is not another dum bass, you unfortunately are one, as you have way too much trouble reading technical books.

jverda at 2007-7-20 21:53:28 > top of Java-index,Other Topics,Patterns & OO Design...
# 98

> thats true; but the ini values are not done using

> constructors, not copy constrcutors - they copy

> values from alrady initilized objects onto already

> initilized objects only, not initilize anything.

>

the above line contains a typo, althoug it is obvious, it may cause confusions, and here is the clarification:

the ini values are done using constructors, NOT copy constructors.

java developers who have no experience in c++ might get confused here easily, so here is an additional note on this, wgoch is jschell's trick which he uses to cover his misunderstandings on cs:

MyClass c1;

in c++:

c1 is a value variable, it is an object, constructed by the constructor MyClass(){...}.

in java:

c2 is an uninitiliz