Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:44:15+00:00 2026-05-27T12:44:15+00:00

Strings are immutable, does it mean that i always have to do something like

  • 0

Strings are immutable, does it mean that i always have to do something like that with a string passed to a method?

str= str.toLowerCase();

or is

str.toLowerCase();

fine? I tried the second one and it doesn’t give me any errors, why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:44:15+00:00Added an answer on May 27, 2026 at 12:44 pm

    String are immutable

    Yes, by your own admission. An immutable object is one that does not allow its state to be changed. This includes String objects.

    Then:

    str.toLowerCase();
    

    Creates a new string of lower-case characters and does not use the result. This is likely a “bug” in this case as str still evaluates to the original string object (which was not changed because it is immutable).

    There is no compiler error because Java has no way of knowing that the return value was “supposed to be used”. There are times when a method is called for side-effects, even if it also returns a value*. This could be judged to be an error in some pure languages (those without side-effects), but it is not possible in a language with side-effects in general. Some static analysis tools — not javac, which is just a compiler with a primitive set of warnings — are capable of detecting such bugs as the above by applying additional heuristic rules.

    Ditto, but assigns the new string to the same variable: (Variables are not values/objects.)

    str = str.toLowerCase();
    

    However, there is no requirement that the same variable is re-assigned to. Consider the following examples, which may be entirely valid in context:

    String normalizedTitle = title.toLowerCase();
    foo.setTitle(normalizedTitle);
    // or, skip normalizedTitle, etc.
    foo.setTitle(title.toLowerCase());
    

    Happy coding.


    *An example of relatively common method that causes a side-effect and returns a value that is normally ignored is List.remove(int).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If a string is immutable, does that mean that.... (let's assume JavaScript) var str
Can a class that does not have final modifier in it be fully immutable
What exactly does immutable mean - that is, what are the consequences of an
I have heard and read that a string can not be changed (immutable?). That
Strings are immutable, meaning, once they have been created they cannot be changed. So,
In languages like Java and C#, strings are immutable and it can be computationally
Why is it that they decided to make String immutable in Java and .NET
If I have an immutable string is the hashing algorithm run every time I
String's in C# are immutable and threadsafe. But what when you have a public
When I pass an immutable type object(String, Integer,.. ) as final to a method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.