• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question - methods - copy of a reference

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to everyone!
I'm currently preparing for the OCPJP and I've got one question (based on the code form Khalid Mughal's book):

public class Kazuke{
private static void put0(StringBuilder s1){
s1.append("0");
}

public static void main (String... args){
StringBuilder s1 = new StringBuilder("W");
put0(s1);
s1.append("W!");
System.out.println(s1);
}
}

Question:
Why if
1) Java passes the argument by a reference
2) the method is void
=> as a result of the method we get a new value of s1???

Please, I know that it's a basic question, but I need help here....
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and what will be the difference if we change VOID to StringBuilder and add a return of a new s1?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the forum Anna

The append() method called on the StringBuilder reference mutates the object and changes its contents. If you return a new StringBuilder from the method, then the previous reference will be lost and a new Stringbuilder will be created
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

So the problem is just in StringBuilder? Becuse it's mutable. And if there was a String (immutable) => no change?

To sum up:
if we send a mutable object to a method like this, it's changed
if we send an IMmutable object to a method like this, it's NOT changed?

The fact that the method is static doesn't change anything?
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anna Rozanova wrote:Thanks!

So the problem is just in StringBuilder? Becuse it's mutable. And if there was a String (immutable) => no change?

To sum up:
if we send a mutable object to a method like this, it's changed
if we send an IMmutable object to a method like this, it's NOT changed?

The fact that the method is static doesn't change anything?



That pretty much sums it up
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot then!!!
 
Anna Gabillet
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
6. Confusion over passing by value, and passing by reference

This can be a frustrating problem to diagnose, because when you look at the code, you might be sure that its passing by reference, but find that its actually being passed by value. Java uses both, so you need to understand when you're passing by value, and when you're passing by reference.

When you pass a primitive data type, such as a char, int, float, or double, to a function then you are passing by value. That means that a copy of the data type is duplicated, and passed to the function. If the function chooses to modify that value, it will be modifying the copy only. Once the function finishes, and control is returned to the returning function, the "real" variable will be untouched, and no changes will have been saved. If you need to modify a primitive data type, make it a return value for a function, or wrap it inside an object.

When you pass a Java object, such as an array, a vector, or a string, to a function then you are passing by reference. Yes - a String is actually an object, not a primitive data type. So that means that if you pass an object to a function, you are passing a reference to it, not a duplicate. Any changes you make to the object's member variables will be permanent - which can be either good or bad, depending on whether this was what you intended.

On a side note, since String contains no methods to modify its contents, you might as well be passing by value.

from http://www.javacoffeebreak.com/articles/toptenerrors.html
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic