0% found this document useful (0 votes)
7 views4 pages

Java String Handling and Threading Guide

Uploaded by

pavanbeemeneni
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)
7 views4 pages

Java String Handling and Threading Guide

Uploaded by

pavanbeemeneni
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

UNIT – 5 (DETAILED ANSWERS WITH DEFINITIONS + EXAMPLES)

=======================================================

------------------------------------------------------------
1) STRING HANDLING METHODS (DEFINITIONS + EXAMPLES)
------------------------------------------------------------

Definition:
String handling in Java is the process of creating, modifying, extracting and comparing strings using
built■in methods from the String, StringBuilder, and StringBuffer classes.

Extraction Methods:
• length() – returns string length
• charAt() – returns character at given index
• substring() – extracts part of a string
Example:
String s = "JavaProgramming";
[Link]([Link](0,4)); // Java

Modification Methods:
• toUpperCase(), toLowerCase()
• replace(), trim(), concat()
Example:
String s = "Hello";
[Link]([Link]("H","M")); // Mello

Comparison Methods:
• equals() – compares content
• equalsIgnoreCase() – ignores case
• compareTo() – lexicographical comparison
Example:
"Apple".compareTo("Ball"); // Negative value

------------------------------------------------------------
2) THREAD, MULTI■THREADING & LIFE CYCLE
------------------------------------------------------------

Definition:
Thread is the smallest unit of a program that runs independently.
Multithreading is executing multiple threads simultaneously to improve performance.

Thread Creation Methods:


1. Extending Thread class
2. Implementing Runnable interface

Example:
class Demo extends Thread {
public void run() {
[Link]("Thread running...");
}
}
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
}

Thread Life Cycle:


1. New
2. Runnable
3. Running
4. Blocked / Waiting
5. Terminated

------------------------------------------------------------
3) JDBC ARCHITECTURE (WITH DIAGRAM DESCRIPTION + PROGRAM)
------------------------------------------------------------

Definition:
JDBC (Java Database Connectivity) is an API used to connect Java applications with databases.

Architecture Layers:
1. Application Layer
2. JDBC Driver Manager
3. JDBC Drivers
4. Database

JDBC Steps:
1. Load driver
2. Establish connection
3. Create statement
4. Execute query
5. Close connection

Example Program:
import [Link].*;
class Demo {
public static void main(String args[]) throws Exception {
[Link]("[Link]");
Connection con = [Link](
"jdbc:mysql://localhost:3306/test","root","pass");
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
while([Link]()) {
[Link]([Link](1)+" "+[Link](2));
}
[Link]();
}
}

------------------------------------------------------------
4) CONCURRENCY, SYNCHRONIZATION & PRODUCER-CONSUMER PROBLEM
------------------------------------------------------------

Definition:
Concurrency means multiple tasks executing at the same time.
Synchronization ensures only one thread accesses shared resources at a time.

Synchronization Keyword:
synchronized keyword prevents thread conflicts.

Producer-Consumer Problem:
Producer generates data → Consumer uses data.
A buffer is shared; synchronized methods prevent race conditions.

Example:
class Q {
int num;
boolean valueSet = false;

synchronized void put(int n) {


while(valueSet) wait();
num = n;
valueSet = true;
notify();
}

synchronized int get() {


while(!valueSet) wait();
valueSet = false;
notify();
return num;
}
}

------------------------------------------------------------
END OF UNIT – 5 DETAILED ANSWERS
------------------------------------------------------------

You might also like