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

Java newbie question

Options
  • 31-10-2014 5:33pm
    #1
    Registered Users Posts: 1,192 ✭✭✭


    I started a course on Java about 6 weeks ago. I'm struggling to stay with the pace; some concepts are taking longer than they should to get established in my head.

    We've gone through Strings and I thought I understood them, 'til I saw an answer to a sample exam question. The question called for the number of vowels in a string to be counted, irrespective of case.

    The sample answer included code to convert the string to lower-case, i.e.
    s=s.toLowerCase();

    I understand what is being done here, but I thought that, in Java, strings are immutable ?


Comments

  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


    They are, however the String object that s points to can be changed.


  • Registered Users Posts: 1,192 ✭✭✭TarfHead


    OK thanks.

    I don't understand the answer, but that's to do with me, not you :(


  • Registered Users Posts: 159 ✭✭magooly


    Strings are immutable meaning any operation on an instance of String returns a new String object. Immutable String class is fundamental to the JVM for security reasons. Its critical to the operation of the String pool, which is an optimization on the JVM memory model around the storing of Strings. Only 1 (immutable) instance of a particular String object exists in the pool and then multiple references to the recurring strings exist throughout your program. This is a memory saving technique since the guys realised Strings were used everywhere. If Strings were mutable this could not happen.

    [HTML]s=s.toLowerCase();[/HTML]

    this reads assign s(the reference) to location of the new object returned from the method.

    the original object s was pointing to will go out of scope and be reclaimed by the gc(if no other reference to this String exists see above)

    Dont take my word for it tho, its right here in the API implementation.

    http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/lang/String.java#String.toLowerCase%28java.util.Locale%29

    [HTML]
    public String toLowerCase(Locale locale) {

    ...
    ...
    return new String(0, count+resultOffset, result);
    }
    [/HTML]


  • Registered Users Posts: 27,087 ✭✭✭✭GreeBo


    TarfHead wrote: »
    OK thanks.

    I don't understand the answer, but that's to do with me, not you :(

    tolowercase() is returning a new string object

    s = s.tolowercase() is assigning the reference "s" to this new object.

    The original string is now unreferenced and available for garbage collection.


    so you had
    s --> MyValue

    now you have
    s --> myvalue
    "null" --> MyValue

    (sorta!)


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    TarfHead wrote: »
    OK thanks.

    I don't understand the answer, but that's to do with me, not you :(

    Strings are immutable, but you are re-assigning the value of s, which is why it works.

    ie.
    s = s.toLowerCase();
    
    works because you are not actually trying to modify the string. the
    .toLowerCase()
    
    method that you are calling returns a new value, which is assigned to the reference variable s


  • Advertisement
  • Registered Users Posts: 223 ✭✭Fate Amenable To Change


    TarfHead wrote: »
    I started a course on Java about 6 weeks ago. I'm struggling to stay with the pace; some concepts are taking longer than they should to get established in my head.

    We've gone through Strings and I thought I understood them, 'til I saw an answer to a sample exam question. The question called for the number of vowels in a string to be counted, irrespective of case.

    The sample answer included code to convert the string to lower-case, i.e.
    s=s.toLowerCase();

    I understand what is being done here, but I thought that, in Java, strings are immutable ?

    s.tolowercase creates something new, you are then sayin from now on "s" is gonna call out that something new.

    its like saying someones called Ted, and everytime I call "Ted" Ted should shout "HELLO", I decide that it would be cool if the same thing could be done without shouting so I change this "HELLO" to "hello". Ted doesn't now this yet so everytime his name is called he still shouts "HELLO". To let him know you can use the equals sign and then you are telling Ted if his name is called say "hello". Now its obvious he can only do one thing if his name is called so saying "HELLO" is forgotten about.

    So what you;re doing is making a copy of the original String then changing the copy, then you;re saying that everytime I call s from now on I want it to bring up the new string.

    If the above confuses don;t worry I'm not 100% sober so my analogy might be a bit strange.


  • Registered Users Posts: 1,192 ✭✭✭TarfHead


    Thanks for those answers.

    It now makes sense in my head :D


Advertisement