jeudi 2 avril 2015

Avoid String charAt() method inside loops

Is there any effect on Code Optimization(Speed etc) if i replace ?


// Before



public void test(String str)
{
for(int i=0; i<str.length(); i++)
{
System.out.println(str.charAt(i)); // VIOLATION
}
}


The above Code Snippet with below code :-


// After



public void test(String str)
{
char[] ch = str.toCharArray(); // CORRECTION
for(int i=0; i < ch.length; i++)
{
System.out.println(ch[i]); // CORRECTION
}
}

Aucun commentaire:

Enregistrer un commentaire