{"id":16254,"date":"2016-09-30T09:54:03","date_gmt":"2016-09-30T09:54:03","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=16254"},"modified":"2020-08-31T12:54:32","modified_gmt":"2020-08-31T12:54:32","slug":"ocajp-stringbuilder","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/","title":{"rendered":"OCAJP &#8211; StringBuilder Class Examples"},"content":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;<strong>Manipulate data using the StringBuilder class and its methods<\/strong>&#8220;. You will be tested in the exam about various methods and syntax related to the StringBuilder class. Here we would explain about the StringBuilder class and important methods. If you have any questions, please write it in the comments section.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_76 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ea7e02;color:#ea7e02\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ea7e02;color:#ea7e02\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#StringBuilder_Class\" >StringBuilder Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#When_to_use_StringBuilder_Class\" >When to use StringBuilder Class?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#Real_World_Scenario\" >Real World Scenario<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#Creating_StringBuilder_Objects\" >Creating StringBuilder Objects<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#How_StringBuilder_capacity_increases\" >How StringBuilder capacity increases ?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#StringBuilder_Class_Methods\" >StringBuilder Class Methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.whizlabs.com\/blog\/ocajp-stringbuilder\/#OCAJP_Tips\" >OCAJP Tips<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"StringBuilder_Class\"><\/span>StringBuilder Class<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\">String Builder was added to Java in 1.5 version. We had StringBuffer class before StringBuilder class.<\/span><span lang=\"EN-US\"><br \/>\n<\/span><\/li>\n<\/ul>\n<ul>\n<li>The class StringBuilder is defined in the package <em>java.lang<\/em> and it has a mutable sequence of characters.<\/li>\n<li><span lang=\"EN-US\">StringBuilder is a final class , so it is not possible to create sub classes for it. You can use StringBuilder methods , but you can\u2019t override.<\/span><\/li>\n<li><span lang=\"EN-US\">StringBuilder implements two intefaces such as CharSequence, Serializable.<\/span><\/li>\n<li>Unlike StringBuilder, the String class has an immutable sequence of characters. Every time you modify a string that\u2019s represented by the String class, your code actually creates new String objects instead of modifying the existing one.<\/li>\n<li><span lang=\"EN-US\"> <\/span>You must use class StringBuilder when you\u2019re dealing with larger strings or modifying the contents of a string often. Doing so will improve the performance of your code.<span lang=\"EN-US\"><br \/>\n<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"When_to_use_StringBuilder_Class\"><\/span><b><span lang=\"EN-US\">When to use StringBuilder Class?<\/span><\/b><span lang=\"EN-US\"><br \/>\n<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The exact difference between StringBuilder and StringBuffer class is that StringBuilder is not thread safe where StringBuffer is thread safe. It is highly recommended to use the StringBuilder class instead of using the StringBuffer class.<\/p>\n<p>Here is some of the points about the StringBuilder class.<\/p>\n<ul>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">StringBuilder class methods are exactly same as StringBuffer class methods, except that StringBuilder is not thread safe.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">In other words its methods are not synchronized.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">Oracle recommends you that you use StringBuilder instead of StringBuffer whenever possible, because StringBuilder will run faster.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span>Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/StringBuffer.html\" target=\"_blank\" rel=\"noopener\">StringBuffer<\/a> be used.<span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<h2 align=\"left\"><span class=\"ez-toc-section\" id=\"Real_World_Scenario\"><\/span><span lang=\"EN-US\">Real World Scenario<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p align=\"left\"><span lang=\"EN-US\">We prefer String Builder when you are working with files that are large, ever changing streams of input are being handled by the program.In these cases large blocks of characters are handled as units, String Builder object are the great way to handle a block of data , pass it on and then reuse the same memory to handle next block of data.<\/span><\/p>\n<h2 align=\"left\"><span class=\"ez-toc-section\" id=\"Creating_StringBuilder_Objects\"><\/span><span lang=\"EN-US\">Creating StringBuilder O<\/span><span lang=\"EN-US\">bjects<\/span><b><span lang=\"EN-US\"> <\/span><\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p align=\"left\"><span lang=\"EN-US\">The StringBuilder class has four overloaded constructors.<br \/>\n<\/span><\/p>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"><\/span><span lang=\"EN-US\">StringBuilder( ) <\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"><\/span><span lang=\"EN-US\">StringBuilder( CharSequence seq)<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"><\/span><span lang=\"EN-US\">StringBuilder(int capacity)<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"><\/span><span lang=\"EN-US\">StringBuilder(String str)<\/span><\/li>\n<\/ul>\n<p align=\"left\"><span lang=\"EN-US\">\u00a0Let\u2019s use these constructors in a sample program.<\/span><\/p>\n<pre>class StringBuilderObjects {\n\n     public static void main(String args[]) {\n\n          StringBuilder sb1 = new StringBuilder();  \/\/ 1 \n          StringBuilder sb2 = new StringBuilder(sb1); \/\/ 2\n          StringBuilder sb3 = new StringBuilder(10); \/\/ 3\n          StringBuilder sb4 = new StringBuilder(\"Hi\"); \/\/ 4 \n\n     }\n}<\/pre>\n<p>Line 1 creates StringBuilder object with no characters in it and an initial capacity of 16 characters.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_StringBuilder_capacity_increases\"><\/span>How StringBuilder capacity increases ?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Line 1 creates StringBuilder object with no characters in it and an initial capacity of 16 characters. After you add 17th character to StringBuilder object , it\u2019s capacity will be increased to 34 . The formula used by the StringBuilder to increase its capacity.<\/p>\n<p>newCapacity = (intialcapacity * 2) + 2.<\/p>\n<ul>\n<li>Line 2 creates a StringBuilder object that contains the same set of characters as contained by the StringBuilder object passed to it.<\/li>\n<li>Line 3 creates a StringBuilder object with no characters and an initial capacity of 10 characters.<\/li>\n<li>Line 4 creates a StringBuilder object with an initial value as contained by the String object.<\/li>\n<\/ul>\n<h2><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"StringBuilder_Class_Methods\"><\/span><span lang=\"EN-US\">StringBuilder Class Methods<\/span><b><span lang=\"EN-US\"> <\/span><\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p align=\"left\"><span lang=\"EN-US\">String Builder contains methods. Most of the methods works as like String methods, for example methods such as charAt, indexOf, lastIndexOf, substring and length.<br \/>\n<\/span><\/p>\n<h4 align=\"left\"><b><span lang=\"EN-US\">append( )<br \/>\n<\/span><\/b><\/h4>\n<ul>\n<li>The append method adds the specified value at the end of the existing value of a StringBuilder object<span lang=\"EN-US\"> and returns reference to the the same <\/span>StringBuilder object .<\/li>\n<li><span lang=\"EN-US\"> <\/span>Because you may want to add data from multiple data types to a StringBuilder object, this method has been overloaded so that it can accept data of any type.<span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<p><span lang=\"EN-US\">This method accepts all primitives(char, int, long,float,double,boolean), String, char array, and Object as method\u00a0 parameters. <\/span><\/p>\n<pre>class StringBuilder<span lang=\"EN-US\">Demo<\/span> {\n\n      public static void main(String args[]) {\n\n           StringBuilder sb = new StringBuilder();\n           StringBuilder sb<span lang=\"EN-US\">1<\/span> = new StringBuilder();\n           StringBuilder sb<span lang=\"EN-US\">2<\/span> = new StringBuilder();\n           sb.append(<span lang=\"EN-US\">5<\/span>);<span lang=\"EN-US\"> \/\/ appends int value\n<\/span>           sb.append(<span lang=\"EN-US\">false<\/span>);<span lang=\"EN-US\"> \/\/ appends boolean value\n<\/span>           sb.append('a');<span lang=\"EN-US\"> \/\/ appends char value \n<\/span>           sb.append(<span lang=\"EN-US\">10.5f<\/span>);<span lang=\"EN-US\"> \/\/ appends float value \n<\/span>           sb.append(<span lang=\"EN-US\">10.5<\/span>);<span lang=\"EN-US\"> \/\/ appends double value \n<\/span>           sb.append(\"Hi\");<span lang=\"EN-US\"> \/\/ appends String object\n<\/span>           sb1.append(sb); \/\/ appends StringBuilder object\n           sb2.append(sb, 1, 4);\/\/ appends characters from index 1 to 3 ( because 4 is exclusive).\n           \n           System.out.println(sb);<span lang=\"EN-US\"> \/\/ prints <\/span>5falsea<span lang=\"EN-US\">10.5<\/span>10.5Hi\n           System.out.println(sb<span lang=\"EN-US\">1<\/span>);<span lang=\"EN-US\"> \/\/ prints <\/span>5falsea<span lang=\"EN-US\">10.5<\/span>10.5Hi\n           System.out.println(sb<span lang=\"EN-US\">2<\/span>);<span lang=\"EN-US\"> \/\/ prints fa1\n<\/span>      }\n}<\/pre>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Note : <\/span>sb2.append(sb, 1, 4)<span lang=\"EN-US\"> This method throws <\/span>IndexOutOfBoundsException<span lang=\"EN-US\"> if you give invalid values.<\/span><span lang=\"EN-US\"><\/span><\/p>\n<ul>\n<li>You can append a complete char array, or its subset as follows:<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">StringBuilder sb = <b>new<\/b> StringBuilder();\n<b>char<\/b>[] name1 = { 'O', 'c', 'a', 'j', 'p' };\n<b>char<\/b>[] name2 = { 'J', 'a', 'v', 'a', '8' };\nsb.append(name1);\nsb.append(name2, 1, 3);\n\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 System.<b><i>out<\/i><\/b>.println(sb);<span lang=\"EN-US\"> \/\/ prints <\/span>Ocajpava \nsb.append(name1);<\/pre>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">It appends all characters to the StringBuilder object.<\/span><\/p>\n<pre style=\"padding-left: 30px\">sb.append(name2, 1, 3);<\/pre>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Pay attention to this method. Here<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">1 &#8211; starting position in the array.<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">3 &#8211; How many characters to add\u00a0 to array after starting position.<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">It throws <\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\"> according to Java doc<\/span><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">if you give wrong index values. But In practice it <\/span><span lang=\"EN-US\">throws Array<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\">.<br \/>\n<\/span><\/p>\n<ul>\n<li><span lang=\"EN-US\"> <\/span>Because the method append also accepts a method parameter of type Object, you can pass it any object from the Java API or your own user-defined object:<span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Example :<\/span><\/p>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n\n<b>       public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n \n              StringBuilder sb1 = <b>new<\/b> StringBuilder();\n              sb1.append(\"Java\");\n              sb1.append(<b>new<\/b> Student(\"Oracle\"));\n              System.<b><i>out<\/i><\/b>.println(sb1);<span lang=\"EN-US\"> \/\/ prints <\/span>Javacom.mss.Student@58d25a40\n       }\n}\n<b>class<\/b> Student {\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 String name;Student(String str) { name = str; }\n}<\/pre>\n<h3 align=\"left\"><\/h3>\n<h4 align=\"left\"><b><span lang=\"EN-US\">Insert( )<\/span><\/b><\/h4>\n<ul>\n<li>The <span lang=\"EN-US\">insert <\/span>method adds the specified value at the <span lang=\"EN-US\">specified position <\/span>of the existing value of a StringBuilder object<span lang=\"EN-US\"> returns reference to the same <\/span>StringBuilder object .<span lang=\"EN-US\"> It throws <\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\">, if you pass invalid index value.<\/span><\/li>\n<li><span lang=\"EN-US\">It is also overloaded method.<\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span>Because you may want to add data from multiple data types to a StringBuilder object, this method has been overloaded so that it can accept data of any type.<span lang=\"EN-US\"><\/span><\/li>\n<li><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">This method accepts all primitives(char, int, long,float,double,boolean), String, char array, and Object as method <\/span><\/li>\n<li>The main difference between the append and insert methods is that the insert method enables you to insert the requested data at a particular position, but the append method only allows you to add the requested data at the end of the StringBuilder object<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public class StringBuilderDemo {\n\n       public static voi<b>d<\/b> main(String[] args) {\n \n              StringBuilder sb =<b> <\/b>new StringBuilder(\"IamJavaDevelopernow\");\n              StringBuilder sb1 = new StringBuilder();\n              StringBuilder sb2 = new StringBuilder();\n              sb.insert(2, 5); \/\/ inserts 5 literal at position 2\n              sb.insert(4,false); \/\/ inserts false literal at position 4\n              sb.insert(6,'a'); \/\/ inserts 'a' literal at position 6\n              sb.insert(7,10.5f); \/\/ inserts 10.5 literal at position 7\n              sb.insert(8,10.5); \/\/inserts 10.5 literal at position 8\n              sb.insert(9,\"Hi\"); \/\/ inserts String object at position 9\n              sb1.insert(0,sb); \/\/ inserts StringBuilder object\u00a0 at position 0\n              sb2.insert(0,sb, 1, 4);\/\/ inserts characters of <span style=\"text-decoration: underline\">sb<\/span> from index 1 to 3 (because 4 is exclusive) to sb2 at position 0.\n              System.<i>out<\/i>.println(sb);\/\/prints Ia5mfaa11Hi0.50.5lseJavaDevelopernow\n              System.<i>out<\/i>.println(sb1);\/\/prints Ia5mfaa11Hi0.50.5lseJavaDevelopernow\n              System.<i>ou<b>t<\/b><\/i>.println(sb2); \/\/ prints a5m\n       }\n}<\/pre>\n<ul>\n<li>You can <span lang=\"EN-US\">inserts <\/span>a complete char array, or its subset as follows:<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">StringBuilder sb = <b>new<\/b> StringBuilder(\"HiHello\");\n<b>char<\/b>[] name1 = { 'O', 'c', 'a', 'j', 'p' };\n<b>char<\/b>[] name2 = { 'J', 'a', 'v', 'a', '8' };\nsb.insert(2,name1);\nsb.insert(4,name2, 1, 3);\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<b><i>out<\/i><\/b>.println(sb);<span lang=\"EN-US\">\/\/ prints <\/span>HiOcavaajpHello\nsb.insert(2,name1);<\/pre>\n<p style=\"padding-left: 30px\">inserts all characters of names1 array to <span style=\"text-decoration: underline\">sb<\/span> starting from position 2<\/p>\n<pre style=\"padding-left: 30px\">sb.insert(4,name2, 1, 3);\n<span lang=\"EN-US\"><\/span><\/pre>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Pay attention to this method. Here<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">1 &#8211; starting position in the array.<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">3 &#8211; How many characters to add\u00a0 to sb after starting position.<\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">4- starting position in sb to insert array elements.<br \/>\n<\/span><\/p>\n<ul>\n<li><span lang=\"EN-US\"> <\/span>Because the method append also accepts a method parameter of type Object, you can pass it any object from the Java API or your own user-defined object:<span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Example :<\/span><\/p>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n\n<b>       public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n\n              StringBuilder sb1 = <b>new<\/b> StringBuilder(\"<span lang=\"EN-US\">HiHello<\/span>\");\n              sb1.insert(<span lang=\"EN-US\">2,<\/span>\"Java\");<span lang=\"EN-US\">\/\/ inserts String object at position 2 \n<\/span>              sb1.insert(<span lang=\"EN-US\">4,<\/span><b>new<\/b> Student(\"Oracle\"));<span lang=\"EN-US\">inserts <\/span>Student<span lang=\"EN-US\"> object at position 4\n<\/span>              System.<b><i>out<\/i><\/b>.println(sb1);<span lang=\"EN-US\">\/\/prints <\/span>HiJacom.mss.Student@58d25a40vaHello\n      }\n}\n\n<b>class<\/b> Student {\n      String name;\n      Student(String str) { name = str; }\n}\n<\/pre>\n<h3><\/h3>\n<h4><strong>charAt( ), setCharAt( )<\/strong><\/h4>\n<pre style=\"padding-left: 30px\">public char charAt(int index)<\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">charAt method takes index value as argument , it returns the the particular character at the specified index value.<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">If you pass invalid index value it throws <\/span>IndexOutOfBoundsException<span lang=\"EN-US\"> . But in practice it throws <\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\">.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public void setCharAt(int index, char ch)<span lang=\"EN-US\"><\/span><\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">setCharAr method replaces the character at the specified index in the object. It returns nothing.<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">If you pass invalid index value it throws <\/span>IndexOutOfBoundsException<span lang=\"EN-US\"> . But in practice it throws <\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\">.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Example :<\/span><\/p>\n<pre style=\"padding-left: 30px\">public class StringBuilderDemo {\n\n       public static void main(String[] args) {\n\n              StringBuilder sb = new StringBuilder(\"Hello\");\n              char c = sb.charAt(1);\n              sb.setCharAt(0, 'N');\n              System.<i>out<\/i>.println(c);<span lang=\"EN-US\"> \/\/ prints e\n<\/span>              System.<i>out<\/i>.println(sb);<span lang=\"EN-US\"> \/\/ prints Nello\n<\/span>      }\n}<\/pre>\n<h3><\/h3>\n<h4 align=\"left\"><b><span lang=\"EN-US\">reverse(), toString() , replace <\/span><\/b>public <span lang=\"EN-US\">StringBuilder<\/span> reverse()<\/h4>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\">Reverse method reverses the character sequence present in <\/span><span lang=\"EN-US\">StringBuilder object and returns reference to the same object.<\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public String toString()<\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">toString method converts <\/span><span lang=\"EN-US\">StringBuilder object\u00a0 into String object.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public StringBuilder replace(int start, int end, String str)<\/pre>\n<ul>\n<li align=\"left\">Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end &#8211; 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)<\/li>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\"><\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\"> if you give in valid index values.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public class StringBuilderDemo {\n\n       public static void main(String[] args) {\n\n              StringBuilder sb = new StringBuilder(\"Hello\");\n              sb.replace(1, 4, \"ABC\");\n              System.<i>out<\/i>.println(sb);<span lang=\"EN-US\"> \/\/ prints <\/span>HABCo\n              StringBuilder sb1 = sb.reverse();\n              String s = sb.toString(); \n              System.<i>out<\/i>.println(sb);<span lang=\"EN-US\"> \/\/ prints <\/span>oCBAH\n              System.<i>out<\/i>.println(sb == sb1);<span lang=\"EN-US\"> \/\/ prints true because both sb , sb1 referencing to the same object.\n<\/span>              System.<i>out<\/i>.println(s);<span lang=\"EN-US\">\/\/ prints <\/span>oCBAH\n       }\n }<\/pre>\n<h3><\/h3>\n<h4><b><span lang=\"EN-US\">Delete, DeleteCharAt <\/span><\/b><\/h4>\n<pre style=\"padding-left: 30px\">public StringBuilder delete(int start<span lang=\"EN-US\">, <\/span>int end)<\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">Delete methods removes characters from start position to end-1 position.<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\"><\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\"> if you give in valid index values.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/StringBuilder.html\" target=\"_blank\" rel=\"noopener\">StringBuilder<\/a> deleteCharAt(int index)<\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">Removes the character at the specified index.<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\"><\/span>StringIndexOutOfBoundsException<span lang=\"EN-US\"> if you give in valid index value.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public class StringBuilderDemo {\n\n       public static void main(String[] args) {\n\n              StringBuilder sb = new StringBuilder(\"HelloHi\");\n              sb.delete(2, 4); \n              sb.deleteCharAt(0);\n              System.<i>out<\/i>.println(sb);<span lang=\"EN-US\"> \/\/ prints <\/span>eoHi\n       }\n}<\/pre>\n<h3><\/h3>\n<h4 align=\"left\"><b><span lang=\"EN-US\">Capacity, ensureCapacity, length,setLength, trimToSize <\/span><\/b><\/h4>\n<pre style=\"padding-left: 30px\">public int capacity()<\/pre>\n<ul>\n<li align=\"left\">Returns the current capacity. The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public void ensureCapacity(int minimumCapacity)<\/pre>\n<ul>\n<li align=\"left\">Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:<\/li>\n<li align=\"left\">The minimumCapacity argument.<\/li>\n<li align=\"left\">Twice the old capacity, plus 2.<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public void trimToSize()<\/pre>\n<ul>\n<li align=\"left\">Attempts to reduce storage used for the character sequence. If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to become more space efficient.<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public int length()<\/pre>\n<ul>\n<li align=\"left\">Returns the <span lang=\"EN-US\">number of characters in the particular object <\/span>.<span lang=\"EN-US\"><br \/>\n<\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public void setLength(int newLength)<\/pre>\n<ul>\n<li align=\"left\">Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument.<\/li>\n<li align=\"left\"><span lang=\"EN-US\">It throws <\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\">, <\/span>if the newLength argument is negative.<span lang=\"EN-US\"><br \/>\n<\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public class StringBuilderDemo {\n\n       public static void main(String[] args) {\n\n              StringBuilder sb = <b>new<\/b> StringBuilder();\n              sb.append(\"OCAJP8\");\n              System.<b><i>out<\/i><\/b>.println(sb.capacity());\/\/ prints 16 because StringBuilder default capacity 16.\n              System.<b><i>out<\/i><\/b>.println(sb.length());\/\/ prints 6 because we appended 6 character sequence  \n              sb.ensureCapacity(20); \/\/ It increases the capacity, doesn't changes length of character sequence\n              System.<b><i>out<\/i><\/b>.println(sb.capacity()); \/\/ prints 34 because capacity increases\n              System.<b><i>out<\/i><\/b>.println(sb.length());\/\/ prints 6 \n              sb.setLength(8);\/\/ It increases the length of character sequence, doesn't changes\u00a0 capacity\n              System.<b><i>out<\/i><\/b>.println(sb.capacity()); \/\/ prints 34\n              System.<b><i>out<\/i><\/b>.println(sb.length()); \/\/ prints 8\n              sb.trimToSize(); \/\/ It decreases the capacity to length of character sequence, but doesn't changes length of character sequence  \n              System.<b><i>out<\/i><\/b>.println(sb.capacity()); \/\/ prints 8\n              System.<b><i>out<\/i><\/b>.println(sb.length()); \/\/ prints 8\n       }\n}<\/pre>\n<h3><\/h3>\n<h4 align=\"left\"><b><span lang=\"EN-US\">getChars, indexOf, lastIndexOf , subSequence, subString<\/span><\/b><\/h4>\n<pre style=\"padding-left: 30px\"><b><span lang=\"EN-US\">\u00a0<\/span><\/b>public void getChars(int srcBegin, int srcEnd<span lang=\"EN-US\">,<\/span>char[] dst, int dstBegin)<\/pre>\n<ul>\n<li align=\"left\">Characters are copied from srcBegin<span lang=\"EN-US\"> to <\/span>srcEnd<span lang=\"EN-US\">-1 from the StringBuilder object <\/span>into the destination character array dst<span lang=\"EN-US\">\u00a0 starting at\u00a0 <\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\">It throws <\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a> <span lang=\"EN-US\">according to Java Doc.<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\">But in practice , if <\/span>srcBegin<span lang=\"EN-US\"> or <\/span>srcEnd<span lang=\"EN-US\"> or both invalid values , It throws String<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\">. If <\/span>dstBegin<span lang=\"EN-US\"> is invalid value It throws Array<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\">.<\/span><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\"><span lang=\"EN-US\">Example<\/span><\/p>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n\n<b>       public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n\n              StringBuilder sb = <b>new<\/b> StringBuilder();\n              sb.append(\"OCAJP8\");\n<b>              char<\/b>[] ch = <b>new<\/b> <b>char<\/b>[5] ;\n              sb.getChars(0, 2, ch, 0);\n              System.<b><i>out<\/i><\/b>.println(Arrays.<i>toString<\/i>(ch));<span lang=\"EN-US\"> \/\/ prints [O,C,\u00a0 ,\u00a0 ]\n<\/span>       }\n}<\/pre>\n<h3 align=\"left\"><\/h3>\n<h4 align=\"left\"><strong>public int indexOf(<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/String.html\" target=\"_blank\" rel=\"noopener\">String<\/a> str)<\/strong><\/h4>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\">It looks for first match.I<\/span>f the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.<\/li>\n<li align=\"left\">public int indexOf(<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/String.html\" target=\"_blank\" rel=\"noopener\">String<\/a> str, int fromIndex)<\/li>\n<li align=\"left\"><span lang=\"EN-US\">It looks for first match.I<\/span>f the string argument occurs as a substring within this object<span lang=\"EN-US\"> <\/span>starting at the specified index , then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n<b> \n       public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n\n              StringBuilder sb = <b>new<\/b> StringBuilder();\n              sb.append(\"OCAJP<span lang=\"EN-US\">A<\/span>8J\");\n              System.<b><i>out<\/i><\/b>.println(sb.indexOf(\"A\"));<span lang=\"EN-US\"> \/\/ prints 2\n<\/span>              System.<b><i>out<\/i><\/b>.println(sb.indexOf(\"Z\"));<span lang=\"EN-US\">\u00a0 \/\/ prints -1\n<\/span>              System.<b><i>out<\/i><\/b>.println(sb.indexOf(\"J\",4));<span lang=\"EN-US\">\u00a0 \/\/ prints 7\n<\/span>      }\n}<\/pre>\n<h3 align=\"left\"><\/h3>\n<h4 align=\"left\"><strong>public int <span lang=\"EN-US\">lastI<\/span>ndexOf(<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/String.html\" target=\"_blank\" rel=\"noopener\">String<\/a> str)<\/strong><\/h4>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\">It looks for last match.I<\/span>f the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.<\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\"><span lang=\"EN-US\">Example : <\/span><\/pre>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n\n<b>        public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n\n               StringBuilder sb = <b>new<\/b> StringBuilder();\n               sb.append(\"OCAJP<span lang=\"EN-US\">A<\/span>8J\");\n               System.<b><i>out<\/i><\/b>.println(sb.indexOf(\"A\"));<span lang=\"EN-US\"> \/\/ prints 2\n<\/span>               System.<b><i>out<\/i><\/b>.println(sb.lastIndexOf(\"<span lang=\"EN-US\">A<\/span>\"));<span lang=\"EN-US\">\u00a0 \/\/ prints 5\n<\/span>               System.<b><i>out<\/i><\/b>.println(sb.lastIndexOf(\"<span lang=\"EN-US\">Z<\/span>\"));<span lang=\"EN-US\">\u00a0 \/\/ prints -1\n<\/span>       }\n}<\/pre>\n<h3 align=\"left\"><\/h3>\n<h4 align=\"left\"><strong><span lang=\"EN-US\">public <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/String.html\" target=\"_blank\" rel=\"noopener\">String<\/a> substring(int start) <\/span><\/strong><\/h4>\n<ul>\n<li align=\"left\">Returns a new String that contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.<\/li>\n<li align=\"left\"><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\">String<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\"> if start is invalid value.<\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/String.html\" target=\"_blank\" rel=\"noopener\">String<\/a> substring(int start, int end)<\/pre>\n<ul>\n<li align=\"left\">Returns a new String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end &#8211; 1.<\/li>\n<li align=\"left\"><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\">String<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\"> if start or end or both invalid <\/span><\/li>\n<\/ul>\n<pre style=\"padding-left: 30px\">public <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/CharSequence.html\" target=\"_blank\" rel=\"noopener\">CharSequence<\/a> subSequence(int start, int end)<\/pre>\n<ul>\n<li align=\"left\"><span lang=\"EN-US\"> <\/span>Returns a new character sequence that is a subsequence of this sequence<span lang=\"EN-US\"> starting from start to end-1.<\/span><span lang=\"EN-US\"><\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\">It throws <\/span><span lang=\"EN-US\">String<\/span><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/IndexOutOfBoundsException.html\" target=\"_blank\" rel=\"noopener\">IndexOutOfBoundsException<\/a><span lang=\"EN-US\"> if start or end or both invalid .<\/span><\/li>\n<li align=\"left\"><span lang=\"EN-US\">The difference between <\/span>substring<span lang=\"EN-US\"> , <\/span>subSequence<span lang=\"EN-US\"> is return types are different.<\/span><\/li>\n<\/ul>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Both the returns reference to String object.<\/span><span lang=\"EN-US\"><\/span><\/p>\n<p align=\"left\" style=\"padding-left: 30px\"><span lang=\"EN-US\">Example :<\/span><\/p>\n<pre style=\"padding-left: 30px\"><b>public<\/b> <b>class<\/b> StringBuilderDemo {\n\n<b>       public<\/b> <b>static<\/b> <b>void<\/b> main(String[] args) {\n\n              StringBuilder sb = <b>new<\/b> StringBuilder();\n              sb.append(\"OCAJPA8J\");\n              System.<b><i>out<\/i><\/b>.println(sb.substring(2)); \/\/ prints AJPA8J\n              System.<b><i>out<\/i><\/b>.println(sb.substring(2,5));\u00a0 \/\/ prints AJP\n              CharSequence c = \u00a0\u00a0\u00a0\u00a0\u00a0 sb.subSequence(2, 5);\n              System.<b><i>out<\/i><\/b>.println(c);\u00a0 \/\/ prints AJP\n              System.<b><i>out<\/i><\/b>.println(c <b>instanceof<\/b> String);\u00a0 \/\/ prints true\n              System.<b><i>out<\/i><\/b>.println(c <b>instanceof<\/b> StringBuilder); \/\/ prints false\n       }\n}<\/pre>\n<h3><\/h3>\n<h2><span class=\"ez-toc-section\" id=\"OCAJP_Tips\"><\/span>OCAJP Tips<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The following are the methods that are very important for the OCAJP exam preparation:<\/p>\n<ul>\n<li>public StringBuilder append(String s)<\/li>\n<li>public StringBuilder delete(int start, int end)<\/li>\n<li>public StringBuilder insert(int offset, String s)<\/li>\n<li>public StringBuilder reverse()<\/li>\n<li>public String toString()<\/li>\n<\/ul>\n<p>The exam may test your knowledge on difference between String and StringBuilder objects. You have to understand the clear difference between the String class and StringBuilder class.<\/p>\n<p>You can try our free test for <a href=\"https:\/\/www.whizlabs.com\/ocajp-scja\/ocajp8-free-test\/\">OCAJP Mock Exams<\/a>. If you have any questions, please write it in the comments section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is about the OCAJP exam objective &#8220;Manipulate data using the StringBuilder class and its methods&#8220;. You will be tested in the exam about various methods and syntax related to the StringBuilder class. Here we would explain about the StringBuilder class and important methods. If you have any questions, please write it in the comments section. StringBuilder Class String Builder was added to Java in 1.5 version. We had StringBuffer class before StringBuilder class. The class StringBuilder is defined in the package java.lang and it has a mutable sequence of characters. StringBuilder is a final class , so it [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[1139],"class_list":["post-16254","post","type-post","status-publish","format-standard","hentry","category-java","tag-ocajp-8"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":4,"uagb_excerpt":"This post is about the OCAJP exam objective &#8220;Manipulate data using the StringBuilder class and its methods&#8220;. You will be tested in the exam about various methods and syntax related to the StringBuilder class. Here we would explain about the StringBuilder class and important methods. If you have any questions, please write it in the&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=16254"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16254\/revisions"}],"predecessor-version":[{"id":71618,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/16254\/revisions\/71618"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=16254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=16254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=16254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}