0% found this document useful (0 votes)
26 views

Advanced Java Unit 4

Uploaded by

23102208
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Advanced Java Unit 4

Uploaded by

23102208
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

IV UNIT:

• String Tokenizer – Parsing - Tokenizing and Formatting - Locating Data via


PatternMatching, Tokenizing –
• Object Serialization - Serializable Interface - Writing and Reading
• Serializable Objects -Transient Keyword- SerialVersionUID –
• Advanced I/O - PipedStreams (PipedInputStream and PipedOutputStream) –
SequenceInputStream PushbackInputStream and PushbackReader.
String Tokenizer:
StringTokenizer class in Java is used to break a string into tokens.
One must go through StringTokenizer class where concepts and constructors are
discussed which help in better understanding methods that are been discussed here
below as follows:
Methods of StringTokenizer class are as follows:
•hasMoreToken
•nextToken
•countTokens
•nextElement
•hasMoreElements
Parsing
❑ Parsing involves analyzing a string and breaking it into meaningful components.
❑ It's commonly used to process input data and convert it into a suitable format for
further processing.
Tokenizing and formatting
Locating Data via Pattern Matching:
• The Pattern Class is used to hold a representation of a regex expression, so that it
can be used and reused by instances of the Matcher class.

• The Matcher class is used to invoke the regex engine with the intention of
performing match operations.
• Pattern Class
• Purpose: Represents a compiled regular expression. Once compiled, the pattern can
be reused multiple times for matching operations.
• Methods:compile(String regex): Compiles the given regular expression into a
pattern.matcher(CharSequence input): Creates a matcher that can be used to
perform matching operations on the given input sequence.
Matcher Class

Purpose: Used to perform matching operations on a character sequence by interpreting a Pattern.

Methods:find(): Attempts to find the next subsequence of the input sequence that matches the pattern.

matches(): Attempts to match the entire input sequence against the pattern.

group(): Returns the matched subsequence.

replaceAll(String replacement): Replaces every subsequence that matches the pattern with the given replacement

string.split(CharSequence input): Splits the input sequence around matches of the pattern.
SERIALIZATION
❑ Serialization is a process of converting an object into a sequence of bytes which can be
persisted to a disk or database or can be sent through streams. The reverse process of
creating object from sequence of bytes is called deserialization

❑ It is mainly used in Hibernate, RMI, JPA, EJB, and JMS technologies.

❑ The java object is serializable if its class or any of its subclasses implements
java.io.serializable or its sub interface java.io.externalizable interface.

❑ The entire process is JVM independent, meaning an object can be serialize on


platform and deserialize on an entirely different platform.

❑ Classes : ObjectInputStream and ObjectOutputStream or High-level Streams that contain


the methods for serializing and deserializing the object.
How to Serialize an Object?
Since you now know what serialization in Java is, and all the relevant points,
let’s delve deep into how to serialize an object. You must use the writeObject() method
of the ObjectOutputStream class for serialization and readObject() method of the
InputObjectStream class for deserialization purpose.

Advantages of Serialization
1.To save/persist the state of an object.
2.It is mainly used to travel the object’s state on the network (which is known as marshaling).
Serialization.java
Steps to Serialize an object:

Step1: java objects become Serializable only when the class of that object implement the java.io.Serializable marker
interface.

Step2:ObjectOutputStream oos = new ObjectOutputStream(OutputStream);


In serialization, the objects will not be written to a file but the data of that object will be written to a file. To perform this
serialization we can use a class called ObjectOutputStream. It is mainly used in Hibernate, RMI, JPA, EJB, and JMS
technologies.

Step4 :Understanding WriteObject() method:


writeObject() method of ObjectOutputStream class that is used to serialize the object into a byte stream

Step 3: Understanding readObject() method

The readObject() method of ObjectInputStream class is used to read an object from the objectinputstream. It
reads the class of the object, the signature of the class, static, non-static fields and supertype.

Creation of ObjectInputStream: ObjectInputstream ois = new ObjectInputStream(InputStream);


transient Keyword in Java
• We can transfer an object from one location to another location only when the
corresponding object is serialized.
• When we transfer an object from one location to another location then all the object
data will be transferred.
• But if you don’t want to transfer a few values of the object then the variables are
declared using a java keyword or modifier called “transient” which are called
transient variables.
Important Points:
• Transient variables are the variables that cannot participate in serialization.
• When we transfer the transient variables then it will hide the original value and
transfer the default values.
SerialVersionUID
▪ The serialization process at runtime associates an id with each Serializable class
which is known as SerialVersionUID.
▪ It is used to verify the sender and receiver of the serialized object. The sender and
receiver must be the same. To verify it, SerialVersionUID is used.
▪ The sender and receiver must have the same SerialVersionUID, otherwise,
InvalidClassException will be thrown when you deserialize the object.
▪ If the class structure changes (e.g., fields are added or removed),
serialVersionUID allows the Java runtime to detect these changes and handle
them appropriately.

Serialization.java
Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as
source or destination.
•PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream
and can be written using PipedInputStream.But, using both threads at the same time will create a deadlock
for the threads.
•A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is
no longer alive.
Methods:
Java.io.PipedOutputStream Class in Java

Piped output stream


Java SequenceInputStream

• The Java SequenceInputStream combines two or more other InputStream's into one.
• First the SequenceInputStream will read all bytes from the first InputStream, then all bytes from the second
InputStream.
• That is the reason it is called a SequenceInputStream, since the InputStream instances are read in sequence.
Sequence
PushbackInputStream

You might also like