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

09/05/2010

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.

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.

Vincy on May 9th, 2010 11:22 am

Java doc has this mentioned, have a look at http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#toLowerCase()

Varun on May 9th, 2010 11:31 am

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

jason on May 27th, 2010 2:18 pm

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

Pavan on June 3rd, 2010 12:43 pm

[...] by Chirag Jain on July 6, 2010 I have found this very interesting and informative post for Java developers. Many Java developers may not know that [...]

toLowerCase() is Locale B&hellip on July 6th, 2010 12:51 pm

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?

Murali on October 1st, 2010 1:31 pm

Very good information.

Vinayak on October 28th, 2010 11:47 am

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

Bala Balaji Yalla on December 6th, 2010 11:39 am

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

Sandeep on January 5th, 2011 12:11 pm

New Concept. Thanks

Shampa on March 7th, 2011 6:24 pm

Very interesting.
Keep posting.

Santanu on March 7th, 2011 7:14 pm

interesting…

Kumaran on April 19th, 2011 7:16 am

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

Motalib on June 16th, 2011 2:46 pm

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

Dhaval on June 27th, 2011 6:48 am

Intersting concept. Thanks for the information

Anamika on June 27th, 2011 11:25 am

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

maheshbabujam on October 12th, 2011 12:11 pm

Thanks………

Rup on November 20th, 2011 8:21 pm

Thanks

sugyan on February 6th, 2012 1:49 pm

Thanks for this hidden information…

Faiz on February 6th, 2012 4:18 pm

Same is true with toUpperCase aswell

Java Learner on February 9th, 2012 4:05 pm

Never heard of it before..very well

jay on March 14th, 2012 12:04 am

Yes it is nice catch

Baskaran on May 9th, 2012 6:03 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home