Ankündigung

Einklappen
Keine Ankündigung bisher.

String per Referenz

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • String per Referenz

    Hi Leutz,

    wie kann ich in Java einen string per Referenz an eine Methode übergben, so das alle Änderungen direkt den string beeinflußen. In C++ geht da ja mit &. (da alles ohne das die funktion den string am Ende zurückgibt, weil meine Methode vom Typ void sein soll)

    Beispiel:

    Code:
    public static void method(String str) {
       str = str.replaceAll("ll","ss");
       System.out.println(str);
    }
    public static void main(String[] args) {
       String str = "Ball";
       method(str);
       System.out.println(str);
    }
    Kann mir einer dabei helfen?

    Danke

  • #2
    Java übergibt ALLE Werte by Value! Bei Objekten wird die Referenz by Value übergeben. Zusätzlich kommt bei String hinzu, dass er immutable ist. Der Java-Weg ist folgender:
    Code:
    public static String method(String str) {
       str = str.replaceAll("ll","ss");
       //System.out.println(str);
       return new String(str);
    }
    Musst du mehrere Werte zurückgeben, dann kapselst du sie in ein Objekt mit getter und setter Methoden, dann klappt folgendes:
    Code:
    public static void method(Integer i) {
       i.setValue(75);
    }
    Hier noch was zum Nachlesen:
    http://forum.java.sun.com/thread.jsp...4997&tstart=15

    How does pass-by-value work with references?

    Way too many people say "Java passes primitive by value and objects by reference". This is not the way it should be stated. Java passes everything by value. With primitives, you get a copy of the contents. With references you get a copy of the contents.

    But what is the contents of a reference?

    The remote control. The means to control / access the object.

    When you pass an object reference into a method, you are passing a COPY of the REFERENCE. A clone of the remote control. The object is still sitting out there, waiting for someone to use a remote.The object doesn't care how many remotes are "programmed" to control it. Only the garbage collector cares (and you, the programmer).

    So when you say:

    Cat A = new Cat();
    doStuff(A);

    void doStuff(Cat B) {

    // use B in some way
    }





    There is still just ONE Cat object. But now TWO remote controls (references) can access that same Cat object.

    So now, anything that B does to the Cat, will affect the Cat that A refers to, but it won't affect the A cup!

    You can change the Cat, using your new B reference (copied directly from A), but you can't change A.

    What the heck does that mean?

    You can change the object A refers to, but you can't take the A reference variable and do something to it -- like redirect it to reference a different object, or null.

    So if you change the B reference (not the Cat object B refers to, but the B reference itself) you don't change A. And the opposite is true.

    So...

    Cat A = new Cat();
    doStuff(A);

    void doStuff(Cat B) {

    B = new Cat(); //did NOT affect the A reference
    }

    Doing this simply "points" B to control a different object. A is still happy.



    So repeat after me:

    Java is pass-by-value.

    (OK, once again... with feeling.)

    Java is pass-by-value.

    For primitives, you pass a copy of the actual value.

    For references to objects, you pass a copy of the reference (the remote control).

    You never pass the object. All objects are stored on the heap. Always.

    Now go have an extra big cup of coffee and write some code.
    EDIT: Schreibfehler
    Zuletzt geändert von GPC; 20.12.2005, 11:17.

    Kommentar


    • #3
      Wow, das nenn ich mal eine super Antwort. Vielen Dank!

      MfG
      Alex

      Kommentar

      Lädt...
      X