0% found this document useful (0 votes)
12 views15 pages

Module 51

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

Module 51

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Module 5

ENUMERATIONS
ENUMERATION FUNDAMENTALS

• An enumeration can be defined simply by creating a list of enum


variable

• Ex

• Identifiers JAVA, CPP, C and DBMS are called enumeration constants.


These are public, static and final by default.
• Variables of Enumeration can be defined directly without any new
keyword.
• Ex: Subject sub;
ENUMERATIONS
StringBuffer
StringBuffer may have characters and substrings
inserted in the middle or appended to the end.
StringBuffer Constructors
– StringBuffer defines these four constructors:
– StringBuffer( ): The default constructor (the one with no
parameters) reserves room for 16 characters without
reallocation.
– StringBuffer(int size): accepts an integer argument that
explicitly sets the size of the buffer.
– StringBuffer(String str): accepts a String argument that
sets the initial contents of the StringBuffer object and
reserves room for 16 more characters without
reallocation
– StringBuffer(CharSequence chars): creates an object that
contains the character sequence contained in chars.
• length( )
– The current length of a StringBuffer can be found
via the length( ) method.
– They have the following general forms:
• int length( ) int capacity( )
class StringBufferDemo {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Krishna");
System.out.println("buffer = " + sb);
// displays Krishna
System.out.println("length = " + sb.length());
// displays 7

}
}
• setLength( )
– To set the length of the buffer within a
StringBuffer object, use setLength( ). Its general
form is shown here:
• void setLength(int len)
charAt( ) and setCharAt( )
– The value of a single character can be obtained from a
StringBuffer via the charAt( ) method.
• You can set the value of a character within a StringBuffer
using setCharAt().
• Their general forms are shown here: char charAt(int
where)
• void setCharAt(int where, char ch)

Example:
• StringBuffer sb = new StringBuffer("Krishna");
sb.charAt(2)// selects i
• sb.setCharAt(2, “u‘)// sets second index value to u means
krushna
insert( )
– The insert( ) method inserts one string into another. It is overloaded to accept
values of all the simple types, plus Strings, Objects, and CharSequences.
These are a few of its forms:
• StringBuffer insert(int index, String str)
Example:
class insertDemo {

StringBuffer insert(int index, char ch) StringBuffer insert(int index,


Object obj)

public static void main(String args[]) {


StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
The output of this example is shown here:
I like Java!
reverse( )
– You can reverse the characters within a StringBuffer object using
reverse( ), shown here:
• StringBuffer reverse( )
Example:
class ReverseDemo {
public static void main(String args[]) {
StringBuffer s = new StringBuffer("abcdef"); System.out.println(s);
s.reverse(); System.out.println(s);
}
}
Here is the output produced by the program:
abcdef fedcba
• replace( )
– You can replace one set of characters with another
set inside a StringBuffer object by calling
replace( ). Its signature is shown here:
• StringBuffer replace(int startIndex, int
endIndex, String str)
• delete( ) and deleteCharAt( )
– You can delete characters within a StringBuffer by
using the methods delete( ) and
• deleteCharAt( ). These methods are shown
here:

StringBuffer delete(int startIndex, int


endIndex) StringBuffer deleteCharAt(int loc)
• substring( )
– You can obtain a portion of a StringBuffer by
calling substring( ). It has the following two forms:
• String substring(int startIndex)
• String substring(int startIndex, int endIndex)
– The first form returns the substring that starts at
startIndex and runs to the end of the invoking
StringBuffer object.
– The second form returns the substring that starts
at startIndex and runs through
• endIndex–1.
• StringBuilder
– J2SE 5 adds a new string class to Java‘s already powerful
string handling capabilities. This new class is called
StringBuilder.
– It is identical to StringBuffer except for one
important difference: it is not synchronized, which
means that it is not thread-safe.
– The advantage of StringBuilder is faster performance.
– However, in cases in which you are using multithreading,
you must use StringBuffer
• rather than StringBuilder.

You might also like