Archive for category Java Strings
I have found this very interesting and informative post for Java developers. Many Java developers may not know that
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
Here is an article on java good practices.Have a look!
String Literal Pool:
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”; // true.
String s2 = “hello”;
Object s2 is the same object with s1. But if you create using new operator:
String s1 = “hello”;//false
String s2 = new String(“hello”);
Here is a great article about Strings.
Here I am giving a code in java to make first character in upper case and rest in lower case
static String breakAtSpace(String s)
String arr=new String;
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(“”);
if(j==0) result.append(str.substring(j,j+1).toUpperCase()) ;
else result .append( str.substring(j,j+1).toLowerCase());
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.
result.append(" "); appends an space after every word.
The output is:
Here Is My String In Different Case Letters