Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java - Why is s not changed?

  • 09-01-2014 03:14PM
    #1
    Registered Users, Registered Users 2 Posts: 474 ✭✭


    Hi there,
    Currently doing some enthuware practice questions and came across this:
    public class Sample{
    public static void main(String[] args) {
    String s1 = new String("java");
    StringBuilder s2 = new StringBuilder("java");
    replaceString(s1);
    replaceStringBuilder(s2);
    System.out.println(s1 + s2);
    }
    static void replaceString(String s) {
    s = s.replace('j', 'l');
    }
    static void replaceStringBuilder(StringBuilder s) {
    s.append("c");
    }
    }
    ok, so I chose the answer lavajavac but the answer actually is javajavac.
    Why is this?
    Surely in replaceString, as it takes a String and assigns that String to the value of lava, then it would mean that the answer would be lavajavac?
    Is this down to there being no "this" keyword?

    Thanks


Comments

  • Moderators, Society & Culture Moderators Posts: 9,688 Mod ✭✭✭✭stevenmu


    I'm guessing this question is about the differences between passing parameters by value and by reference.

    When you pass by value, you are effectively making a copy of that value and sending that to the method, if you update that value in your method, it does not update the original value. When you pass by reference, you are passing a pointer to the original variable, if you update that value in your method, you are in fact updating the original variable.

    IIRC, in Java, by default 'simple' types are passed by value and 'complex' types are passed by reference. Simple types are the built in basic types like int, float etc. Complex types are 'objects'. String is, I believe a simple type, and so should be passed by value, and StringBuilder is a complex type and should be passed by reference.

    Following that logic though, the output should be "javajavac". I don't know where "javalavac" comes from.


  • Registered Users, Registered Users 2 Posts: 474 ✭✭Umekichi


    stevenmu wrote: »
    Following that logic though, the output should be "javajavac". I don't know where "javalavac" comes from.

    Sorry typo, my eyes are going square from all this code :D
    Also Thanks, I understand it a bit better


  • Closed Accounts Posts: 167 ✭✭dsrckiyisvddht


    Reason for this is that String is immutable.

    In the below you are not returning anything and assigning it back to s1 so s1 remains the same.
    static void replaceString(String s) {
     s = s.replace('j', 'l');
     }
    

    If you change that to
    static void replaceString(String s) {
     s = s.replace('j', 'l');
    System.out.println(s);
     }
    

    you will see it is only "lava" for the scope of that method


  • Moderators, Technology & Internet Moderators Posts: 1,337 Mod ✭✭✭✭croo


    stevenmu wrote: »
    Following that logic though, the output should be "javajavac". I don't know where "javalavac" comes from.
    and that IS the output!


  • Registered Users, Registered Users 2 Posts: 8,324 ✭✭✭chrislad


    If you wanted, you could also return a String

    static String replaceString(String s) {
    return s.replace('j', 'l');
    }

    So for String s1, you would have

    s1 = replaceString(s1);


  • Advertisement
Advertisement