java Unit-5 ppt
java Unit-5 ppt
Topics:
1. Introduction to Strings
2. Char Sequence, Class String
3. Methods for Extracting Characters from Strings
4. Class String Buffer
Strings
What are Strings in Java?
Strings are the type of objects that can store the character of values and in Java, every
character is stored in 16 bits i,e using UTF 16-bit encoding. A string acts the same as an array
of characters in Java.
Example:
String name = "Geeks";
1. String literal
To make Java more memory efficient (because no new objects are created if it exists already
in the string constant pool).
Example:
String demoString = “GeeksforGeeks”;
Strings
2. Using new keyword
String s = new String(“Welcome”);
The Java String is immutable which means it cannot be changed. Whenever we change any
string, a new instance is created. For mutable strings, you can use StringBuffer and
StringBuilder classes.
String class in Java:
The string is a sequence of characters. In Java, objects of String are immutable
which means a constant and cannot be changed once created.
O/P:
Geeks for Geeks String in Java
O/P: First String
Second String
Java Program to Sort Names in an Alphabetical Order:
Differences between String, String Buffer and String Builder
classes:
String Class: The string is a sequence of characters. In Java, objects of String are
immutable which means a constant and cannot be changed once created.
StringBuilder in Java represents a mutable sequence of characters. Since the String Class
in Java creates an immutable sequence of characters, the StringBuilder class provides an
alternative to String Class, as it creates a mutable sequence of characters.
• New State
• Runnable State
• Blocked State
• Waiting State
• Timed Waiting State
• Terminated State
Lifecycle and States of a Thread in Java Life Cycle of a Thread
There are multiple states of the thread in a lifecycle as mentioned below:
New Thread: When a new thread is created, it is in the new state. The
thread has not yet started to run when the thread is in this state. When a
thread lies in the new state, its code is yet to be run and hasn’t started to
execute.
Runnable State: A thread that is ready to run is moved to a runnable state.
In this state, a thread might actually be running or it might be ready to
run at any instant of time. It is the responsibility of the thread scheduler
to give the thread, time to run.A multi-threaded program allocates a fixed
amount of time to each individual thread. Each and every thread runs for
a short while and then pauses and relinquishes the CPU to another
thread so that other threads can get a chance to run. When this happens,
all such threads that are ready to run, waiting for the CPU and the
currently running thread lie in a runnable state.
Blocked: The thread will be in blocked state when it is trying to acquire a
lock but currently the lock is acquired by the other thread. The thread will
move from the blocked state to runnable state when it acquires the lock.
Waiting state: The thread will be in waiting state when it calls wait()
method or join() method. It will move to the runnable state when other
thread will notify or that thread will be terminated.
Lifecycle and States of a Thread in Java Life Cycle of a Thread
Because it exits normally. This happens when the code of the thread has
been entirely executed by the program.
Because there occurred some unusual erroneous event, like a
segmentation fault or an unhandled exception.
Creating Thread
A thread is created either by "creating or implementing" the Runnable Interface or by
extending the Thread class. These are the only two ways through which we can create a
thread.
You can create threads by implementing the runnable interface and overriding the run()
method. Then, you can create a thread object and call the start() method.
import java.io.*;
class GFG extends Thread {
public void run()
{
System.out.print("Welcome to GeeksforGeeks.");
}
public static void main(String[] args)
{
GFG g = new GFG(); // creating thread
g.start(); // starting thread
}
}
Output : Welcome to GeeksforGeeks.
Creating Thread
ii. Implementing the java.lang.Runnable interface.
Synchronization in Java
Synchronization in Java is the capability to control the access of multiple
threads to any shared resource.
• Java Synchronization is better option where we want to allow only
one thread to access the shared resource.
• Process Synchronization
• Thread Synchronization
Synchronization in Java
Thread Synchronization
There are two types of thread synchronization
1.mutual exclusive
2.Inter-thread communication.
i.Mutual Exclusive
• Synchronized method.
• Synchronized block.
• Static synchronization.
2.Cooperation (Inter-thread communication in java)
Synchronization in Java :
i.Understanding the problem without Synchronization
Java Synchronized Method
If you declare any method as synchronized, it is known as synchronized
method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires
the lock for that object and releases it when the thread completes its
task.
2.With Synchronized method
Ex: Java programme that prints numbers from 1 to 10, line by
line, after every 5 seconds?
isAlive() Method:
The isAlive() method returns true if the thread upon which it is called is still
running otherwise it returns false.