Why doesnt this String change?

Discussion in 'Java' started by SlowLearner, Dec 16, 2006.

  1. SlowLearner

    SlowLearner Guest

    I'm trying to create a method which returns a flag and a string. So I
    passed the String as an argument but it doesn't change. I thought all
    objects were past as references and so I could change the value. What
    am I doing wrong?




    SomeEvent(....) {
    String FolderName="123";
    if (test(FolderName)) Do SomeThing;
    // FolderName still 123 at this line. Why isn't it asdfg?
    }





    public boolean test(String txt) {
    txt="asdfg";
    return true;
    }
     
    SlowLearner, Dec 16, 2006
    #1
    1. Advertising

  2. SlowLearner wrote:
    > I'm trying to create a method which returns a flag and a string. So I
    > passed the String as an argument but it doesn't change. I thought all
    > objects were past as references and so I could change the value. What
    > am I doing wrong?
    >
    >
    >
    >
    > SomeEvent(....) {
    > String FolderName="123";
    > if (test(FolderName)) Do SomeThing;
    > // FolderName still 123 at this line. Why isn't it asdfg?
    > }
    >
    > public boolean test(String txt) {
    > txt="asdfg";
    > return true;
    > }


    The line

    txt="asdfg";

    is assigning a String object "asdfg" to the reference "txt", but doesn't
    change the reference "FolderName". The references themselves aren't
    passed by reference. :)

    If you invoked a method on txt that altered its contents, it would
    affect FolderName. But Java strings are immutable, so this isn't
    possible. To do what you want you need StringBuffer:

    StringBuffer FolderName = new StringBuffer("123");
    ....
    public Boolean test (StringBuffer txt) {
    txt.delete(0, txt.length());
    txt.append("asdfg");
    return true;
    }

    FolderName now also points to "asdfg" after test(FolderName) is called.
    (On the other hand, "txt = new StringBuffer("asdfg");" wouldn't work. It
    would change "txt" to refer to a different StringBuffer, rather than
    change the StringBuffer itself.)
     
    John Ersatznom, Dec 16, 2006
    #2
    1. Advertising

  3. SlowLearner

    SlowLearner Guest

    SlowLearner wrote:
    > I'm trying to create a method which returns a flag and a string. So I
    > passed the String as an argument but it doesn't change. I thought all
    > objects were past as references and so I could change the value. What
    > am I doing wrong?
    >
    >
    >
    >
    > SomeEvent(....) {
    > String FolderName="123";
    > if (test(FolderName)) Do SomeThing;
    > // FolderName still 123 at this line. Why isn't it asdfg?
    > }
    >
    >
    >
    >
    >
    > public boolean test(String txt) {
    > txt="asdfg";
    > return true;
    > }


    Thanks for your help everybody
     
    SlowLearner, Dec 18, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Mayer [C# MVP]
    Replies:
    2
    Views:
    2,875
    Andrew Baker
    Apr 15, 2004
  2. =?Utf-8?B?U29saXR1cw==?=

    Asp:TextBox value doesnt change

    =?Utf-8?B?U29saXR1cw==?=, Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,654
    Grant Merwitz
    Oct 13, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    909
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,998
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    3
    Views:
    437
    alex23
    May 27, 2008
Loading...

Share This Page