Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - Why is s not changed?

  • 09-01-2014 2: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,689 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,336 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