[go: up one dir, main page]

0% found this document useful (0 votes)
20 views7 pages

Revision

hbv nm v

Uploaded by

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

Revision

hbv nm v

Uploaded by

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

Revision

StringBuffer Methods
is a mutable sequence of characters. Unlike String , which is
StringBuffer

immutable, StringBuffer allows modification of the string’s content without creating


new objects.

1. append(String str) :

Appends the specified string to the end of the current StringBuffer .

StringBuffer sb = new StringBuffer("Hello");


sb.append(" World");
// Output: "Hello World"

2. insert(int offset, String str) :

Inserts the specified string at the given position in the StringBuffer .

StringBuffer sb = new StringBuffer("Hello");


sb.insert(5, " World");
// Output: "Hello World"

3. delete(int start, int end) :

Deletes the characters from the specified start index to end index
(exclusive).

StringBuffer sb = new StringBuffer("Hello World");


sb.delete(5, 11);
// Output: "Hello"

4. deleteCharAt(int index) :

Deletes the character at the specified index.

Revision 1
StringBuffer sb = new StringBuffer("Hello");
sb.deleteCharAt(0);
// Output: "ello"

5. replace(int start, int end, String str) :

Replaces the substring from start to end (exclusive) with the specified
string.

StringBuffer sb = new StringBuffer("Hello World");


sb.replace(6, 11, "Java");
// Output: "Hello Java"

6. reverse() :

Reverses the characters in the StringBuffer .

StringBuffer sb = new StringBuffer("Hello");


sb.reverse();
// Output: "olleH"

7. capacity() :

Returns the current capacity of the StringBuffer . This is the amount of


space allocated for characters.

StringBuffer sb = new StringBuffer("Hello");


int cap = sb.capacity(); // Returns 16 (default capacity
+ length of string)

8. ensureCapacity(int minCapacity) :

Ensures that the StringBuffer has at least the specified capacity. If not, it
increases the capacity.

StringBuffer sb = new StringBuffer("Hello");

Revision 2
sb.ensureCapacity(30);

9. toString() :

Converts the StringBuffer to a String .

StringBuffer sb = new StringBuffer("Hello");


String str = sb.toString();
// Output: "Hello"

10. substring(int start, int end) :

Returns a new string that is a substring of the StringBuffer starting from


index start to end - 1 .

Summary
Here is a quick recap of the key methods:

StringBuffer Methods
append() , insert() , delete() , deleteCharAt() , replace()

reverse() , capacity() , ensureCapacity() , toString() , substring()

StringBuilder in Java
StringBuilder is a mutable sequence of characters in Java that allows you to create
and manipulate strings efficiently. Unlike the String class, StringBuilder objects
can be modified without creating new objects, which makes it faster for
operations like appending, inserting, or deleting characters.

Key Features
1. Mutability: Changes to a StringBuilder object occur in the same object, unlike
String .

2. Efficient Memory Usage: No new object is created for every modification,


reducing memory overhead.

Revision 3
3. Not Thread-Safe: Unlike StringBuffer , StringBuilder is not synchronized,
making it faster but unsuitable for multi-threaded environments.

Commonly Used Methods


Here’s a list of essential StringBuilder methods:

Method Description Example

Appends the given string to sb.append("Java"); → Adds


append(String str)
the end of the StringBuilder . "Java" to the existing content.

insert(int offset, Inserts the given string at the sb.insert(3, "Code"); →


String str) specified position. Inserts "Code" at index 3.

Replaces characters from sb.replace(0, 4, "Hi"); →


replace(int start,
start to end index with the Replaces first 4 characters with
int end, String str)
given string. "Hi" .

delete(int start, Removes characters from sb.delete(1, 3); → Deletes


int end) start to end index. characters at index 1 and 2.

Reverses the characters in the sb.reverse(); → Reverses the


reverse()
StringBuilder . entire sequence.

Returns the character at the sb.charAt(2); → Gets the


charAt(int index)
specified index. character at index 2.

Replaces the character at the sb.setCharAt(1, 'X'); →


setCharAt(int index,
char ch) specified index with the given Replaces character at index 1
character. with 'X' .

Returns the number of


sb.length(); → Returns the
length() characters in the
length of the sequence.
StringBuilder .

Returns the current capacity


sb.capacity(); → Shows how
capacity() (storage size) of the
much space is allocated.
StringBuilder .

Returns a substring of the sb.substring(2, 5); →


substring(int start,
int end)
character sequence between Extracts characters between
start and end . index 2 and 5.

Revision 4
Garbage Collection in Java
Garbage Collection (GC) is a process by which Java automatically manages
memory by reclaiming unused memory and cleaning up objects that are no longer
in use, preventing memory leaks.

How Garbage Collection Works


1. Heap Memory: Java objects are stored in the heap memory.

2. Reachability: GC determines which objects are still in use by checking if they


are reachable through active references.

3. Unreachable Objects: Objects that are no longer referenced are considered


garbage and are eligible for collection.

4. Finalization: Before an object is garbage collected, its finalize() method (if


overridden) is called for cleanup.

5. GC Phases:

Mark: The GC identifies which objects are still reachable.

Sweep: Unreachable objects are cleared and memory is freed.

Compact: Memory is compacted to remove fragmentation (may not


always happen).

Key Functions for Garbage Collection


Java provides several functions related to garbage collection, primarily through
the System and Runtime classes.

1. System.gc() :

Suggests the JVM to run the garbage collector. However, it is not


guaranteed that GC will run immediately.

System.gc(); // Requesting GC to run

2. Runtime.gc() :

Revision 5
Similar to System.gc() , but it is invoked from the Runtime class. It suggests
that the JVM run the garbage collector.

Runtime.getRuntime().gc(); // Requesting GC from Runtime

3. finalize() :

A method in the Object class that is called before an object is garbage


collected. It can be overridden to perform cleanup actions (though it’s not
commonly used due to unpredictability).

@Override
protected void finalize() throws Throwable {
// Cleanup code (not recommended to rely on it)
super.finalize();
}

4. WeakReference , SoftReference , PhantomReference :

These classes are used to create references that allow the garbage
collector to reclaim objects even if they are still referenced.

: GC will reclaim the object as soon as it becomes


WeakReference

unreachable.

SoftReference : GC will reclaim the object when memory is low.

PhantomReference : Used for more advanced use cases like cleanup after the
object is finalized.

Types of Garbage Collectors in Java


1. Serial GC: Uses a single thread for garbage collection.

2. Parallel GC: Uses multiple threads for minor garbage collections.

3. Concurrent Mark-Sweep (CMS) GC: Aims to minimize pause times.

4. G1 Garbage Collector: A server-style collector aiming for low latency and high
throughput.

Revision 6
When Does Garbage Collection Happen?
Low Memory: The JVM may trigger garbage collection when it runs low on
memory.

Explicit Request: Through methods like System.gc() , although it's only a


suggestion.

Important Notes:
No Direct Control: Java does not give direct control over garbage collection.
You can only suggest it with gc() , but the JVM decides when to run it.

Memory Leaks: If objects are still referenced (even if not in use), they won’t
be garbage collected, causing memory leaks.

Summary:
Garbage collection is automatic memory management in Java that frees
memory by reclaiming unreachable objects.

Use System.gc() and Runtime.gc() to suggest garbage collection.

Finalization is done using finalize() , but it is generally discouraged.

Revision 7

You might also like