Java Multiple catch blocks

If more than one exception can occur in one try block, then we can use multiple catch blocks to provide appropriate handlers to different exception objects. Note: in case of multiple catch blocks, blocks must be placed from specific handler to general handler.   Syntax of try block with multiple catch blocks: try{ //block of … Read more

try and catch blocks in java

try block try block is used to enclose the code that might throw an exception. It must be followed by either a catch or finally or both blocks.   Syntax of a try block with catch block try{ //block of statements }catch(Exception handler class){ }   Syntax of try block with finally block try{        … Read more

Exception handling in java

Exception handling is a mechanism to handle runtime errors so that the normal flow of the program can be maintained.   Exception Hierarchy Throwable is the superclass.    Advantages/Benefits of Exceptional Handling Using exceptional handling we can separate the error handling code from normal code. Using exceptional handling we can differentiate the error types. The … Read more

Java StringBuilder ensureCapacity() method

ensureCapacity(int minCapacity): ensures that the capacity is at least equal to the specified minimum.   Syntax: public void ensureCapacity(int minCapacity)   Note: If the current capacity is greater than the argument there will be no change in the current capacity. If the current capacity is less than the argument there will be a change in the current … Read more

Java StringBuilder capacity() method

capacity(): returns the current capacity of the string builder. Capacity refers to the amount of available storage.   Syntax: public int capacity()   StringBuilder capacity() Example class TestStringBuilder{ StringBuilder sb = new StringBuilder(); public void capacityTest(){ //default capacity. System.out.println(sb.capacity()); sb.append(“Hello “); //current capacity 16. System.out.println(sb.capacity()); sb.append(“www.hello.com”); //current capacity (16*2)+2=34 i.e (oldcapacity*2)+2. System.out.println(sb.capacity()); } } public … Read more

Java StringBuilder reverse() method

reverse(): replace the string builder’s character sequence with the reverse character sequence.   Syntax: public StringBuilder reverse()   StringBuilder reverse() Example class TestStringBuilder{ StringBuilder sb = new StringBuilder(“www.w3schools.blog”); public void reverseTest(){ //replace the string buffer’s character //sequence by reverse character sequence. System.out.println(sb.reverse()); } } public class StringBuilderReverseExample { public static void main(String args[]){ //creating TestStringBuilder … Read more

Java StringBuilder delete() method

delete(int startIndex, int endIndex): delete the substring of the string builder from startIndex to endIndex-1. Syntax: public StringBuilder delete(int startIndex, int endIndex)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will be thrown.   StringBuilder delete() Example class TestStringBuilder{ StringBuilder sb … Read more

Java StringBuilder replace() method

replace(int startIndex, int endIndex, String str): replace the substring of the string builder from startIndex to endIndex-1 with the specified string.   Syntax: public StringBuilder replace(int startIndex, int endIndex, String str)   Note: startIndex should be between 0 and to length of the string or less than endIndex, if it is not StringIndexOutOfBoundsException will be … Read more