You are on page 1of 80

OOPDP

Module V
Module V- Syllabus
• Working with the String and StringBuilder class, Character class,
Tokenizing strings, Regular Expressions, Files and Streams, Using NIO
classes, Sequential file handling, Object serialization, JFileChooser,
Introduction to threading, Introduction to Generics and lambda
expressions.
Java Regex(Regular Expression)

• The Java Regex or Regular Expression is an API to define a pattern for searching
or manipulating strings.
• It is widely used to define the constraint on strings such as password and email
validation.
• After learning Java regex tutorial, you will be able to test your regular expressions
by the Java Regex Tester Tool.
• Java Regex API provides 1 interface and 3 classes in java.util.regex package.
• The java.util.regex package provides following classes and interfaces for regular
expressions.

• MatchResult interface
• Matcher class
• Pattern class
• PatternSyntaxException class
Matcher class

It implements the MatchResult interface.


It is a regex engine which is used to perform match operations on a character sequence.

No. Method Description

1 boolean matches() test whether the regular expression matches the pattern.
2 boolean find() finds the next expression that matches the pattern.
3 boolean find(int start) finds the next expression that matches the pattern from the given
start number.
4 String group() returns the matched subsequence.
5 int start() returns the starting index of the matched subsequence.
6 int end() returns the ending index of the matched subsequence.
7 int groupCount() returns the total number of the matched subsequence.
Pattern class
It is the compiled version of a regular expression.
It is used to define a pattern for the regex engine.

No. Method Description

1 static Pattern compile(String regex) compiles the given regex and returns the instance of
the Pattern.
2 Matcher matcher(CharSequence input) creates a matcher that matches the given input with
the pattern.
3 static boolean matches(String regex, It works as the combination of compile and matcher
CharSequence input) methods. It compiles the regular expression and
matches the given input with the pattern.
4 String[] split(CharSequence input) splits the given input string around matches of given
pattern.
5 String pattern() returns the regex pattern.
Example
import java.util.regex.*;
public class Main{
public static void main(String args[]){
//1st way
Pattern p = Pattern.compile(".s");//. represents single character
Matcher m = p.matcher("as");
boolean b = m.matches();

//2nd way
boolean b2=Pattern.compile(".s").matcher("as").matches();

//3rd way
boolean b3 = Pattern.matches(".s", "as");

System.out.println(b+" "+b2+" "+b3);


}}
• Output
true true true
Regex Character Classes

No. Character Class Description

1 [abc] a, b, or c (simple class)


2 [^abc] Any character except a, b, or c (negation)
3 [a-zA-Z] a through z or A through Z, inclusive (range)
4 [a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
5 [a-z&&[def]] d, e, or f (intersection)
6 [a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
7 [a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
Example

• System.out.println(Pattern.matches("[amn]", "abcd"));//false (not a or m or n)

• System.out.println(Pattern.matches("[amn]", "a"));//true (among a or m or n)

• System.out.println(Pattern.matches("[amn]", "ammmna"));
//false (m and a comes more than once)
Regex Quantifiers

Regex Description

X? X occurs once or not at all


X+ X occurs once or more times
X* X occurs zero or more times
X{n} X occurs n times only
X{n,} X occurs n or more times
X{y,z} X occurs at least y times but less than z times
Example

• System.out.println(Pattern.matches("[amn]?", "a"));//true (a or m or n comes one


time)

• System.out.println(Pattern.matches("[amn]+", "aaa"));//true (a comes more than


one time)

• System.out.println(Pattern.matches("[amn]*", "ammmna"));//true (a or m or n m
ay come zero or more times)
Regex Metacharacters
The regular expression metacharacters work as shortcodes.

Regex Description

. Any character (may or may not match terminator)


\d Any digits, short of [0-9]
\D Any non-digit, short for [^0-9]
\s Any whitespace character, short for [\t\n\x0B\f\r]
\S Any non-whitespace character, short for [^\s]
\w Any word character, short for [a-zA-Z_0-9]
\W Any non-word character, short for [^\w]
\b A word boundary
\B A non word boundary
Example

• System.out.println(Pattern.matches("\\d", "abc"));//false (non-digit)

• System.out.println(Pattern.matches("\\d", "1"));//true (digit and comes once)

• System.out.println(Pattern.matches("\\D", "m"));//true (non-


digit and comes once)

• System.out.println(Pattern.matches("\\D*", "mak"));//true (non-


digit and may come 0 or more times)
Multithreading in Java

• Multithreading in Java is a process of executing multiple threads simultaneously.


• A thread is a lightweight sub-process, the smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.
• However, we use multithreading than multiprocessing because threads use a
shared memory area.
• They don't allocate separate memory area so saves memory, and context-
switching between the threads takes less time than process.
• Advantages of Java Multithreading

• It doesn't block the user because threads are independent and you can perform
multiple operations at the same time.

• You can perform many operations together, so it saves time.

• Threads are independent, so it doesn't affect other threads if an exception occurs


in a single thread.
Java Thread class

• Java provides Thread class to achieve thread programming.

• Thread class provides constructors and methods to create and perform


operations on a thread.

• Thread class extends Object class and implements Runnable interface.


Life cycle of a Thread (Thread States)

• A thread in Java at any point of time exists in any one of the following
states.
• A thread lies only in one of the shown states at any instant:
• New
• Runnable
• Blocked
• Waiting
• Timed Waiting
• Terminated
Life cycle of a Thread
How to create thread

• There are two ways to create a thread:

1. By extending Thread class


2. By implementing Runnable interface.
Thread class:

• Thread class provide constructors and methods to create and perform operations
on a thread.

• Thread class extends Object class and implements Runnable interface.

• Commonly used Constructors of Thread class:


• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
Runnable interface:

• The Runnable interface should be implemented by any class whose instances are
intended to be executed by a thread.

• Runnable interface have only one method named run().

• public void run(): is used to perform action for a thread.


• If you are not extending the Thread class,your class object would not be treated
as a thread object.

• So you need to explicitely create Thread class object.

• We are passing the object of your class that implements Runnable so that your
class run() method may execute.
Java Thread Example by extending Thread class

class Multi extends Thread{


public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
• Output:
thread is running...
Starting a Thread

• start() method of Thread class is used to start a newly created thread.

• It performs following tasks:


• A new thread starts(with new callstack).
• The thread moves from New state to the Runnable state.
• When the thread gets a chance to execute, its target run() method will run.
Java Thread Example by implementing Runnable interface

class Multi implements Runnable{


public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


Multi m1=new Multi();
Thread t1 =new Thread(m1);
t1.start();
}
}
• Output:
thread is running...
Commonly used Methods of Thread Class

• Sleep method in java


• The sleep() method of Thread class is used to sleep a thread for the specified
amount of time.

• Syntax of sleep() method in java


• The Thread class provides two methods for sleeping a thread:
• public static void sleep(long miliseconds)throws InterruptedException
• public static void sleep(long miliseconds, int nanos)throws InterruptedException
Example
class TestSleepMethod extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod t1=new TestSleepMethod();
TestSleepMethod t2=new TestSleepMethod();

t1.start();
t2.start();
}
}
• Output
1
1
2
2
3
3
4
4
• The join() method
• It causes the currently running threads to stop executing until the
thread it joins with completes its task.
Example

class TestJoinMethod1 extends Thread{


public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}

t2.start();
t3.start();
}
}
• Output:1
• 2
• 3
• 4
• 5
• 1
• 1
• 2
• 2
• 3
• 3
• 4
• 4
• 5
• 5
Example:getName() and setName()

class Main extends Thread{


public void run(){
System.out.println("running...");
}
public static void main(String args[]){
Main t1=new Main();
Main t2=new Main();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
t1.start();
t2.start();
t1.setName("New_Thread");
System.out.println("After changing name of t1:"+t1.getName());
}
}
• Output

Name of t1:Thread-0
Name of t2:Thread-1
running...
After changing name of t1:New_Thread
running...
Priority of a Thread (Thread Priority):

• Each thread have a priority.

• Priorities are represented by a number between 1 and 10.

• In most cases, thread schedular schedules the threads according to their priority
(known as preemptive scheduling).

• But it is not guaranteed because it depends on JVM specification that which


scheduling it chooses.
• 3 constants defined in Thread class:
• public static int MIN_PRIORITY
• public static int NORM_PRIORITY
• public static int MAX_PRIORITY

• Default priority of a thread is 5 (NORM_PRIORITY).


• The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.
Example
class Main extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
Main m1=new Main();
Main m2=new Main();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();

}
}
• Output
running thread name is:Thread-0
running thread priority is:1
running thread name is:Thread-1
running thread priority is:10
Points to Remember

• Can we start a thread twice?


• No. After starting a thread, it can never be started again. If you does so,
an IllegalThreadStateException is thrown.

• What if we call run() method directly instead start() method?


• Each thread starts in a separate call stack.
• Invoking the run() method from main thread, the run() method goes onto the
current call stack rather than at the beginning of a new call stack.
Example:Calling run method directly
class TestCallRun extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestCallRun t1=new TestCallRun();
TestCallRun t2=new TestCallRun();

t1.run();
t2.run();
}
}
• Output
1
2
3
4
1
2
3
4
Streams and Files in Java

• A stream is a sequence of data. In Java, a stream is composed of bytes.

• It's called a stream because it is like a stream of water that continues to flow.

• In Java, 3 streams are created for us automatically.

• All these streams are attached with the console.


• System.out: standard output stream
• System.in: standard input stream
• System.err: standard error stream
InputStream and OutputStream

• OutputStream

• Java application uses an output stream to write data to a destination; it may be a


file, an array, peripheral device or socket.

• InputStream

• Java application uses an input stream to read data from a source; it may be a file,
an array, peripheral device or socket.
• OutputStream class

• OutputStream class is an abstract class.

• It is the superclass of all classes representing an output stream of bytes.

• An output stream accepts output bytes and sends them to some sink.
Useful methods of OutputStream

Method Description

1) public void write(int)throws IOException is used to write a byte to the current output stream.

2) public void write(byte[])throws IOException is used to write an array of byte to the current output
stream.

3) public void flush()throws IOException flushes the current output stream.

4) public void close()throws IOException is used to close the current output stream.
OutputStream Hierarchy
• InputStream class

• InputStream class is an abstract class.

• It is the superclass of all classes representing an input stream of bytes.


Useful methods of InputStream

Method Description

1) public abstract int read()throws IOException reads the next byte of data from the input stream. It returns
-1 at the end of the file.

2) public int available()throws IOException returns an estimate of the number of bytes that can be read
from the current input stream.

3) public void close()throws IOException is used to close the current input stream.
InputStream Hierarchy
Java FileOutputStream Class

• Java FileOutputStream is an output stream used for writing data to a file.

• If you have to write primitive values into a file, use FileOutputStream class.

• You can write byte-oriented as well as character-oriented data through


FileOutputStream class.

• But, for character-oriented data, it is preferred to use FileWriter than


FileOutputStream.
Important Methods of FileOutputStream

Method Description

protected void finalize() It is used to clean up the connection with the file output stream.
void write(byte[] ary) It is used to write ary.length bytes from the byte array to the file
output stream.
void write(byte[] ary, int off, int len) It is used to write len bytes from the byte array starting at
offset off to the file output stream.
void write(int b) It is used to write the specified byte to the file output stream.
FileChannel getChannel() It is used to return the file channel object associated with the file
output stream.
FileDescriptor getFD() It is used to return the file descriptor associated with the stream.
void close() It is used to closes the file output stream.
Example

import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}
}
• Output
Success...

The content of a text file testout.txt is set with the data A.


• Writing a byte array to a file

• String s="Welcome to java.";


• byte b[]=s.getBytes();//converting string into byte array
• fout.write(b);
Java FileInputStream Class

• Java FileInputStream class obtains input bytes from a file.

• It is used for reading byte-oriented data (streams of raw bytes) such as image
data, audio, video etc.

• You can also read character-stream data.

• But, for reading streams of characters, it is recommended to use FileReader class.


Important Methods of FileInputStream Class

Method Description

int available() It is used to return the estimated number of bytes that can be read
from the input stream.
int read() It is used to read the byte of data from the input stream.
int read(byte[] b) It is used to read up to b.length bytes of data from the input stream.
int read(byte[] b, int off, int len) It is used to read up to len bytes of data from the input stream.
long skip(long x) It is used to skip over and discards x bytes of data from the input
stream.
FileChannel getChannel() It is used to return the unique FileChannel object associated with the
file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
protected void finalize() It is used to ensure that the close method is call when there is no
more reference to the file input stream.
Example

import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("testout.txt");
int i=fin.read();
System.out.print((char)i);

fin.close();
}catch(Exception e){System.out.println(e);}
}
}
• Output
A
• To read all the characters

• int i=0;
• while((i=fin.read())!=-1){
• System.out.print((char)i);
Java FileWriter Class

• Java FileWriter class is used to write character-oriented data to a file.

• It is character-oriented class which is used for file handling in java.

• Unlike FileOutputStream class, you don't need to convert string into


byte array because it provides method to write string directly.
Useful Methods

Method Description

void write(String text) It is used to write the string into FileWriter.


void write(char c) It is used to write the char into FileWriter.
void write(char[] c) It is used to write char array into FileWriter.
void flush() It is used to flushes the data of FileWriter.
void close() It is used to close the FileWriter.
Example

import java.io.FileWriter;
public class FileWriterExample {
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("testout.txt");
fw.write("Welcome to java.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
}
• Output
Success...
Java FileReader Class

• Java FileReader class is used to read data from the file.

• It returns data in byte format like FileInputStream class.

• It is character-oriented class which is used for file handling in java.


Methods of FileReader class

Method Description

int read() It is used to return a character in ASCII form. It returns -1 at the end of file.
void close() It is used to close the FileReader class.
Example

import java.io.FileReader;
public class FileReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}
• Output
Welcome to java
Serialization and Deserialization in Java

• Serialization is a mechanism of converting the state of an object into a byte


stream.

• Deserialization is the reverse process where the byte stream is used to recreate
the actual Java object in memory.

• This mechanism is used to persist the object.


• To make a Java object serializable we implement
the java.io.Serializable interface.

• The ObjectOutputStream class contains writeObject() method for serializing an


Object.

• The ObjectInputStream class contains readObject() method for deserializing an


object.
• Advantages of Serialization

1. To save/persist state of an object.

2. To travel an object across a network.


• Only the objects of those classes can be serialized which are
implementing java.io.Serializable interface.

• Serializable is a marker interface (has no data member and method).

• It is used to “mark” java classes so that objects of these classes may get certain
capability.
SerialVersionUID

• The Serialization runtime associates a version number with each Serializable class
called a SerialVersionUID, which is used during Deserialization to verify that
sender and reciever of a serialized object have loaded classes for that object
which are compatible with respect to serialization.
• If the reciever has loaded a class for the object that has different UID than that of
corresponding sender’s class, the Deserialization will result in
an InvalidClassException.
Example

import java.io.*;

class Demo implements java.io.Serializable


{
public int a;
public String b;

public Demo(int a, String b)


{
this.a = a;
this.b = b;
}

}
class Test
{
public static void main(String[] args)
{
Demo object = new Demo(1, "java");
String filename = "file.ser";
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(object);
out.close();
file.close();
System.out.println("Object has been serialized");
}

catch(IOException ex)
{
System.out.println("IOException is caught");
}
Demo object1 = null;
// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
object1 = (Demo)in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized ");
System.out.println("a = " + object1.a);
System.out.println("b = " + object1.b);
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}}}
• Output
Object has been serialized
Object has been deserialized
a = 1
b = java
• Points to remember

• If a parent class has implemented Serializable interface then child class doesn’t need to
implement it but vice-versa is not true.

• Only non-static data members are saved via Serialization process.

• Static data members and transient data members are not saved via Serialization
process.So, if you don’t want to save value of a non-static data member then make it
transient.

• Constructor of object is never called when an object is deserialized.

• Associated objects must be implementing Serializable interface.

You might also like