Re: problems with Java



Lew wrote:
ender <astrothayne@xxxxxxxxx> wrote:
The second thing is that you should have more control over how objects
are passed. Although pass by reference is preferable in most cases
there are many instances when pass by value is required, having to
constantly use clone() is not a very efficient, or safe way.

Huh? I never use clone(), and Java doesn't pass by reference, it passes by value.

Technically it passes references by value, but the effect is to pass a reference. I think it's splitting hairs, rather than showing anyone's misunderstanding of the language.

You probably don't use clone, but you do use constructors that copy.

I'm not sure what you are claiming to accomplish, but I am sure there are Java-idiomatic ways to whatever it is.

I believe what the original poster is referring to is the need to copy your arguments and return values. There's a bit of a problem, in that the type doesn't give you enough information. I want to return a Set<K>, should I make a copy? Well, no, not if the method is keySet(). Suppose I did make a copy, what implementation should I use? Do I always want to make a copy, even from private methods? It's a problem, but I think if you wanted to solve it in the language, there are better ways to go about it.

Tom Hawtin
.



Relevant Pages

  • Re: Instances effecting each other?
    ... public myObj Clone() ... Would the above return a clone not a reference? ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Create 1DataTable from a Select from DataTable
    ... But the using clone looks much better. ... Doug ... > DataTable is a reference type, ... > variable/property to another you are making a copy of the reference. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Creating a reference to an object
    ... Clone is a specific reference to the Clonemethod ... you are cloning ... such as if you were to make a copy constructor ... ICloneable you can make another copy of a reference or tracking ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating a reference to an object
    ... Clone is a specific reference to the Clonemethod ... Since a constructor doesn't "return" anything, it's always of the type being constructed. ... MemberwiseClone(), but that only ever makes a shallow copy. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Instances effecting each other?
    ... //Please note that if x and y are reference types, you will want to clone ... it will definitely return a reference to a new object. ... So in that case how can i take an instance in an array and create a new ... SceneChair chair2 = new SceneChair; ...
    (microsoft.public.dotnet.languages.csharp)