UNIT 4 Class Notes
UNIT 4 Class Notes
• A token is returned by taking a substring of the string that was used to create
the StringTokenizer object. It provides the first step in the parsing process
often called lexer or scanner.
separate tokens. These tokens are then stored in String sentence = "A blessing on disguise";
1. int countTokens(): This method counts and returns the number of tokens available
in the StringTokenizer object.
2. boolean hasMoreTokens(): This method checks if there are more tokens available
in the StringTokenizer object or not. If the next token is available, it will return true.
3. String nextToken(): This method returns the next token from the string tokenizer
object.
4. String nextToken(String delim): It returns the next token from the string
tokenizer object based on the delimiter.
5. boolean hasMoreElements(): It returns the same value as the hasMoreTokens
method.
6. Object nextElement(): It returns the same value as the nextToken method, but its
return type is Object.
Java StringTokenizer Example Programs
import java.util.StringTokenizer; // Now retrieve tokens from st and display them.
public class StringTokens { System.out.println("Tokens are: ");
public static void main(String[] args) while(st.hasMoreTokens()){
{ String tokens = st.nextToken();
// Take a string. System.out.println(tokens);
String str = "He is a gentle man"; }
}
// Take a string for delimiters. }
String delimiters = " ,"; // Here, delimiters are a space and
a comma. Output:
// Create an object of string tokenizer and break into tokens. Number of tokens: 5
Here, delimiters are a space and a comma.
Tokens are:
StringTokenizer st = new StringTokenizer(str, delimiters);
He
is
// Counts the number of tokens available in string tokenizer
object. a
int counts = st.countTokens(); gentle
System.out.println("Number of tokens: " +counts); man
.
Split() method:
• String[] s = str.split(delimiters);
Tokenizing and Formatting
String tokenization and formatting can be used to process and System.out.println(formattedToken);
reformat tokens extracted from a string. Example: Tokenizing
and Formatting a Sentence }
Input String: "hello geeks, how are you doing today?" }
Objective: Tokenize the string and format each token by private static String formatToken(String token) {
capitalizing the first letter of each word.
if (token.length() == 0) return token;
import java.util.StringTokenizer;
return token.substring(0, 1).toUpperCase() +
public class TokenizingAndFormatting { token.substring(1).toLowerCase();
public static void main(String[] args) { }
String str = "hello geeks, how are you doing today?"; }
StringTokenizer tokenizer = new StringTokenizer(str,
" ,?");
while (tokenizer.hasMoreTokens()) {
Hello
Geeks
How
Are
You
Doing
Today
Locating Data via Pattern Matching, Tokenizing
• Deserialization is the exact opposite process of serialization where the byte data type stream is
• All the fields of a class must be serializable; otherwise, use the transient keyword (more about it later)
• The child class doesn’t have to implement the Serializable interface, if the parent class does
• The serialization process only saves non-static data members, but not static or transient data members
By default, the String and all wrapper classes implement the Serializable interface
JDK 1.4 introduced the so-called New I/O (or NIO), in java.nio package and its
auxiliary packages, to support high performance and intensive I/O operations.
NIO is meant to complement the existing Standard I/O (in java.io package), not as
a replacement.
Java NIO (New Input/Output) is a high-performance networking and file handling
API introduced in JDK 4. It serves as an alternative to the standard Java I/O
system, offering advanced features and a different programming model.
• Physical I/O operation is thousands times slower than memory access.
Hence, a chunk of data is often cache or buffer to improve the
throughput. As illustrated from the above diagram, many layers of
cache exist between your Java application and physical disk.