Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Objects only passed by copy? Date: Sat, 08 Sep 2012 21:30:45 -0400 Organization: A noiseless patient Spider Lines: 74 Message-ID: References: <6e06860b-4c72-4f4b-8892-69aa4242a4dc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 9 Sep 2012 01:30:59 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="ffb8f7085759b339c1002252b48331a4"; logging-data="739"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19JKv2uVYxOJHnmkJoXi7Th" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <6e06860b-4c72-4f4b-8892-69aa4242a4dc@googlegroups.com> Cancel-Lock: sha1:V/I/z+5Vq9cRnD2rrOZIOalfkiE= Xref: csiph.com comp.lang.java.programmer:18611 On 9/8/2012 8:22 PM, Luft wrote: > I'm learning JAVA and read that EVERYTHING in JAVA is passed by copy and that in the case of objects the internal variables share a reference but that objects are also passed by copy. Argument expressions provide values, those values initialize method or constructor parameters. In that sense "passed by copy" is correct ("passed by value" is the more usual phrase): The parameter gets a copy of the value of the argument expression, not the expression itself. That is why you can call a method like `something(x + 3)' or `otherthing(obj.getAnswer())', even though you cannot write `x + 3 = 9' or `obj.getAnswer() = "NO"'. Some people (and some writers of books) like to say that primitive values (int, double, and so on) are passed by value, while objects (String, JButton, et al.) are passed by reference. I think this is needlessly confusing -- and it still doesn't explain `otherthing(obj.getAnswer())'. To my mind, it is more useful to think of everything being passed by value, where a "value" may be - a primitive value like 5 or 98.6 or 'X' or false, or - a reference value that is a "handle" for some object instance (or is null). With this point of view many things that are complicated in the "pass by value or sometimes by reference" scheme become much simpler: You *always* pass by value, whether the value is a primitive or a reference. With either viewpoint objects are not "passed by copy," by which I mean that the object inside the method is not an independent copy of the object provided by the caller. There's just one object, and the method gets a copy of the *reference* to that object -- and that leads to the example below. > I'm trying to think of a piece of code that would demonstrate this but can't think of anything off the top of my head. Okay. Let's write a method that appends 'X' to a StringBuilder: static void appendX(StringBuilder builder) { builder.append('X'); } Simple enough, right? Fine, now let's call it: StringBuilder adage = new StringBuilder( "Better late than never."); System.out.println(adage); // prints the old saying appendX(adage); System.out.println(adage); // prints ... what? Question: After the call, does the StringBuilder instance that `adage' refers to contain an 'X' or not? If the StringBuilder *instance* is copied, appendX() has its own independent StringBuilder to play with. It can append its 'X', or append twenty 'X'es, or insert an 'X' in the middle of what's already there -- but the StringBuilder in the caller, the one `adage' refers to, will be unchanged because appendX() is using a copy, not the original. If the StringBuilder *reference* is copied, appendX() has a reference to the same StringBuilder instance that `adage' designates. When appendX() adds its 'X', it changes the content of that one single StringBuilder instance, and the caller will now find an 'X' on the end of `adage'. Which is it? Make your prediction before trying the code, then try the code and check your answer. -- Eric Sosman esosman@ieee-dot-org.invalid "The speed at which the system fails is usually not important."