StringBuilder class in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
StringBuilder class
StringBuilder is identical to StringBuffer except for one important difference it is not synchronized, which means it is not thread-safe. Its because StringBuilder methods are not synchronized.
StringBuilder Constructors
- StringBuilder ( ), creates an empty StringBuilder and reserves room for 16 characters.
- StringBuilder ( in size ), create an empty string and takes an integer argument to set capacity of the buffer.
- StringBuilder ( String str ), create a StringBuilder object and initialize it with string str.
Difference between StringBuffer and StringBuilder class
StringBuffer class | StringBuilder class |
---|---|
StringBuffer is synchronized. | StringBuilder is not synchronized. |
Because of synchronization, StringBuffer operation is slower than StringBuilder. | StringBuilder operates faster. |
Example of StringBuilder
class Test { public static void main(String args[]) { StringBuilder str = new StringBuilder("study"); str.append( "tonight" ); System.out.println(str); str.replace( 6, 13, "today"); System.out.println(str); str.reverse(); System.out.println(str); str.replace( 6, 13, "today"); } }
Output :
studytonight studyttoday yadottyduts
Exception Handling
Exception Handling is the mechanism to handle runtime malfunctions. We need to handle such exceptions to prevent abrupt termination of a program. The term exception means an exceptional condition, it is a problem that may arise during the execution of a program. A bunch of things can lead to exceptions, including programmer error, hardware failures, files that need to be opened cannot be found, resource exhaustion etc.
Exception
A Java Exception is an object that describes the exception that occurs in a program. When an exceptional event occurs in Java, an exception is said to be thrown. The code that's responsible for doing something about the exception is called an exception handler.
Exception class Hierarchy
All exception types are subclasses of class Throwable, which is at the top of exception class hierarchy.
- Exception class is for exceptional conditions that program should catch. This class is extended to create user-specific exception classes.
- RuntimeException is a subclass of Exception. Exceptions under this class are automatically defined for programs.
Exception is categorized into 3 categories.
- Checked Exception
- Unchecked Exception
- Error
The exception that can be predicted by the programmer.Example: File that needs to be opened is not found. These type of exceptions must be checked at compile time.
Unchecked exceptions are the class that extends RuntimeException. An unchecked exception are ignored at compile time. Example: ArithmeticException, NullPointerException, Array Index out of Bound exception. Unchecked exceptions are checked at runtime.
Errors are typically ignored in code because you can rarely do anything about an error. Example: if a stack overflow occurs, an error will arise. This type of error is not possible to handle in code.
Uncaught Exceptions
When we don't handle the exceptions, they lead to unexpected program termination. Let's take an example for better understanding.
class UncaughtException { public static void main(String args[]) { int a = 0; int b = 7/a; // Divide by zero, will lead to exception } }
This will lead to an exception at runtime, hence the Java run-time system will construct an exception and then throw it. As we don't have any mechanism for handling the exception in the above program, hence the default handler will handle the exception and will print the details of the exception on the terminal.
Comments
Post a Comment