Java String

class Test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); StringBuffer stringBuffer = new StringBuffer(“Java”); for (int i = 0; i < 8000000; i++) { stringBuffer.append(“Programming”); } System.out.println(“Time taken by StringBuffer: ” + (System.currentTimeMillis() – startTime) + “ms”); startTime = System.currentTimeMillis(); StringBuilder stringBuilder = new StringBuilder(“Java”); for (int i = 0; i…

Read More Difference between StringBuilder and StringBuffer classes

// creates an empty string Builder with the initial capacity of 16. StringBuilder str = new StringBuilder(); // creates a string Builder with the specified string. StringBuilder str = new StringBuilder(“some string value”); // creates an empty string Builder with the specified capacity as length. StringBuilder str = new StringBuilder(10); class Test { public static…

Read More StringBuilder class in Java

class Test { public static void main(String[] args) { String str = “ALEGRUTECHBLOG”; String strInLowerCase = str.toLowerCase(); System.out.println(strInLowerCase); } } Output: alegrutechblog   toUpperCase() Converts all of the characters in this String to the upper case using the rules of the default locale.   class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 2

class Test { public static void main(String[] args) { String str = “alegru”; char c = str.charAt(2); System.out.println(c); } } Output: e class Test { public static void main(String[] args) { String str = “alegru”; int index = str.indexOf(‘l’); System.out.println(index); } } Output: 1 class Test { public static void main(String[] args) { String str…

Read More Working With Strings in Java – Part 1

String str = “Hello”; String str = new String(“Hello”); class Test { public static void main(String[] args) { String str1 = “Hello”; String str2 = “Hello”; String str3 = new String(“Hello”); System.out.println(str1 == str2); System.out.println(str1 == str3); } } Output: true false   Here: We create str1 by assigning it a String literal. Then, that…

Read More Java String Tutorial