Archive for category Java Strings

toLowerCase() is Locale Based

I have found this very interesting and informative post for Java developers. Many Java developers may not know that toLowerCase() and toUpperCase() methods of java.lang.String class are Locale based. Have a look here at javadocs for these methods.

toLowerCase() internally calls toLowerCase(Locale locale). Same is the case for toUpperCase().

Leave a comment

Java Good Practices

Here is an article on java good practices.Have a look!

1 Comment

Ways of Creating Strings in java: String Literal Pool

    String Literal Pool:

String str=”hello”;
willl go to String literal pool managed by String class and will reuse the “hello” string if it is already present, otherwise creates new “hello” in pool.
String str=new String(“hello”)
every time creates new object in heap.

Garbage collector never looks in String literal pool. String literal pool is also a part of heap memory.


String s1 = “hello”;
String s2 = “hello”;
// true.

Object s2 is the same object with s1. But if you create using new operator:

String s1 = “hello”;
String s2 = new String(“hello”);
Here is a great article about Strings.

Leave a comment

Making First Character of Every Word in Upper Case and Others in Lower Case in a Java String

Here I am giving a code in java to make first character in upper case and rest in lower case

class UpperFirst
static String[] breakAtSpace(String s)
int arrayIndex=0;
int first=0;
int last=0;

String arr[]=new String[10];

last=s.indexOf(” “,first);
if(last==-1) last=s.length();
return arr;

public static void main(String arg[])
String s= “here is MY STRING in DiffeRent cAse LetterS”;
String arr[]= UpperFirst.breakAtSpace(s);
StringBuffer result=new StringBuffer(“”);

for(int i=0;i<arr.length;i++)
String str=arr[i];
for(int j=0;j<str.length();j++)
if(j==0) result.append(str.substring(j,j+1).toUpperCase()) ;
else result .append( str.substring(j,j+1).toLowerCase());
result.append(” “);



The logic I have used is that I have broken the string into words by searching space(” “) character in string. Then stored every word in an array. This is implemented in breakAtSpace() method.
In main method, by checking (j==0), I have changed the first character of every word in upper case and rest in lower case.
the line result.append(" "); appends an space after every word.

The output is:

Here Is My String In Different Case Letters

Leave a comment