Java’s toLowerCase() has got a surprise for you!

Last modified on August 1st, 2014 by Joe.

Have you ever encountered a surprise while using toLowerCase()? This is a widely used method when it comes to strings and case conversion. There is a nice little thing you should be aware of.

toLowerCase() respects internationalization (i18n). It performs the case conversion with respect to your Locale. When you call toLowerCase(), internally toLowerCase(Locale.getDefault()) is getting called. It is locale sensitive and you should not write a logic around it interpreting locale independently.

import java.util.Locale;

public class ToLocaleTest {
    public static void main(String[] args) throws Exception {
        Locale.setDefault(new Locale("lt")); //setting Lithuanian as locale
        String str = "\u00cc";
		System.out.println("Before case conversion is "+str+" and length is "+str.length());// Ì
        String lowerCaseStr = str.toLowerCase();
		System.out.println("Lower case is "+lowerCaseStr+" and length is "+lowerCaseStr.length());// i?`

In the above program, look at the string length before and after conversion. It will be 1 and 3. Yes the length of the string before and after case conversion is different. Your logic will go for a toss when you depend on string length on this scenario. When your program gets executed in a different environment, it may fail. This will be a nice catch in code review.

To make it safer, you may use another method toLowerCase(Locale.English) and override the locale to English always. But then you are not internationalized.

So the crux is, toLowerCase() is locale specific.

Comments on "Java’s toLowerCase() has got a surprise for you!"

  1. Vincy says:

    Very new information I have never heard before. Because, it is common method that had been used by everybody. This post is very useful.

    Thank you.

  2. jason says:

    That’s great, i have never heard of it before.

  3. Pavan says:

    I agree with Vincy, useful and new information.
    Keep posting.

  4. Murali says:

    Is the behavior standard across all conditions?

    Instead of using \u00cc during string initialization, i just changed string to be cc and it gave me same length regardless of whether i set locale or not.

    Am i doing anything wrong?

  5. Vinayak says:

    Very good information.

  6. Bala Balaji Yalla says:

    Excellent article. Good to know such deep secrets of Java…Plz keep posting more such things…

  7. Sandeep says:

    Thanks for this new information.
    By reading the title I was guessing that the article will be about String immutability but I was wrong.

  8. Shampa says:

    New Concept. Thanks

  9. Santanu says:

    Very interesting.
    Keep posting.

  10. Kumaran says:


  11. Motalib says:

    This is really interesting and new to me. Keep it up. thanks

  12. Dhaval says:

    Really a good work. With your blogs I come to know about little internal things of JAVA.

  13. Anamika says:

    Intersting concept. Thanks for the information

  14. maheshbabujam says:

    I am learning java…in this location i found more useful articles ..this is best …thank you

  15. Rup says:


  16. sugyan says:


  17. Faiz says:

    Thanks for this hidden information…

  18. Java Learner says:

    Same is true with toUpperCase aswell

  19. jay says:

    Never heard of it before..very well

  20. Baskaran says:

    Yes it is nice catch

  21. Sathiya seelan says:

    Intresting. Good job Joe.

  22. Anonymous says:

    cool…this is informative

  23. Mahesh says:

    This blog is really cool buddy.

  24. Shiva says:

    Interesting never heard of it before..very well

  25. swati jain says:

    interesting fact..

  26. Anon says:

    I don’t know if somebody else has already said what I’m going to say, but: interesting…

  27. Sagaya says:

    Never heard before,, good one!!!

Comments are closed for "Java’s toLowerCase() has got a surprise for you!".