Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
package
1.
2. Packages are used in Java in order to prevent
naming conflicts, to control access, to make
searching/locating and usage of classes, interfaces,
enumerations and annotations easier.
A Package can be defined as a grouping
of related types (classes, interfaces,
enumerations and annotations ) providing
access protection and namespace management.
3. The Exception Handling in Java is one
of the powerful mechanism to handle the
runtime errors so that normal flow of the
application can be maintained.
Types of Java Exceptions
Checked Exception
Unchecked Exception
Error
4.
5. Checked Exception
The classes which directly inherit Throwable class
except RuntimeException and Error are known as
checked exceptions e.g. IOException, SQLException
etc. Checked exceptions are checked at compile-time.
Unchecked Exception
The classes which inherit RuntimeException are
known as unchecked exceptions e.g.
ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time, but they
are checked at runtime.
6. Error
Error is irrecoverable e.g. OutOfMemoryError,
VirtualMachineError, AssertionError etc.
Example:
public class JavaExceptionExample{
public static void main(String args[]){
try{
int data=100/0;
}
catch(ArithmeticException e){System.out.println(e);}
System.out.println("rest of the code...");
}
}
7. Multithreading is a Java feature that allows concurrent
execution of two or more parts of a program for
maximum utilization of CPU. Each part of such
program is called a thread. So, threads are light-weight
processes within a process.
Threads can be created by using two mechanisms :
1. Extending the Thread class
2. Implementing the Runnable Interface
8. Thread creation by extending the Thread class:
We create a class that extends the java.lang.Thread
class. This class overrides the run() method available in
the Thread class. A thread begins its life inside run()
method. We create an object of our new class and call
start() method to start the execution of a thread. Start()
invokes the run() method on the Thread object.
Thread creation by implementing the Runnable
Interface
We create a new class which implements
java.lang.Runnable interface and override run() method.
Then we instantiate a Thread object and call start()
method on this object.
9. The String object can be created explicitly by using the new
keyword and a constructor in the same way as you have
created objects previously. For example: The statement
String strl =new String("Java");
creates a String object initialized to the value of the string
literal "Java" and assigns a reference to string reference
variable str. Here, String ("Java") is actually a constructor of
the form String (string literal). When this statement is
compiled , the Java compiler invokes this constructor and
initializes the String object with the string literal enclosed in
parentheses which is passed as an argument.
12. String Handling concept is storing the
string data in the main memory (RAM),
manipulating the data of the String,
retrieving the part of the String etc.
String Handling provides a lot of
concepts that can be performed on a
string such as concatenation of string,
comparison of string, find sub string etc.
13. StringBuffer is a peer class of String
that provides much of the functionality of
strings. String represents fixed-length,
immutable character sequences while
StringBuffer represents growable and
writable character sequences.
14. StringBuffer may have characters and
substrings inserted in the middle or
appended to the end. It will automatically
grow to make room for such additions
and often has more characters
preallocated than are actually needed, to
allow room for growth.
16. StringBuffer Constructors:
StringBuffer( ):
It reserves room for 16 characters
without reallocation.
StringBuffer s=new StringBuffer();
StringBuffer( int size):
It accepts an integer argument that
explicitly sets the size of the buffer.
StringBuffer s=new StringBuffer(20);
17. StringBuffer(String str):
It accepts a String argument that sets the
initial contents of the StringBuffer object and
reserves room for 16 more characters without
reallocation.
StringBuffer s =
newStringBuffer("GeeksforGeeks");
18. Methods:
Some of the most used methods are:
length( ) and capacity( ): The length of a
StringBuffer can be found by the length( )
method, while the total allocated capacity()
methord.
int length()
int capacity()
19. append( ):
It is used to add text at the end of the
existence text. Here are a few of its forms:
StringBuffer append(String str)
StringBuffer append(int num) .
insert( ):
It is used to insert text at the specified
index position. These are a few of its forms:
StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
20. delete( ) and deleteCharAt( ):
o It can delete characters within a StringBuffer
by using the methods delete( ) and
deleteCharAt( ).
o The delete( ) method deletes a sequence of
characters from the invoking object.
o The deleteCharAt( ) method deletes the
character at the index specified by loc.
StringBuffer delete(int startIndex,
int endIndex)
StringBuffer deleteCharAt(int loc)
21. replace( ):
It can replace one set of characters
with another set inside a StringBuffer
object by calling replace( ). The substring
being replaced is specified by the indexes
start Index and endIndex.
StringBuffer replace(int startIndex, int
endIndex, String str)
22. ensureCapacity( ):
It is used to increase the capacity of a
StringBuffer object. The new capacity will be
set to either the value we specify or twice the
current capacity plus two (i.e. capacity+2),
whichever is larger. Here, capacity specifies
the size of the buffer.
void ensureCapacity(int capacity)
23. getchars():
To copy of a substring of a stringbuffer
into a array, use the getchars()
method().Sourcestart specifies the index of the
beginning of the substring, and source end
specifies an index of the end substring.
Void getchars(int souceStart, int sourceEnd,
char target[], int targetstart)
24. reverse():
You can reverse the characters within string
buffer object using reverse().
String buffer reverse()
substring():
You can obtain a portion of a string buffer by
calling substring().
String substring(int starIndex)
String substring(int startIndex, int endIndex)
25. J2SE 5 adds a new string class to Java’s already
powerful string handling capabilities. This new
class is called String Builder.
It is identical to String Buffer except for one
important difference: it is not synchronized, which
means that it is not thread-safe.
The advantage of String Builder is faster
performance. However, in cases in which you are
using multithreading, you must use String Buffer
rather than String Builder.