Object Oriented Programming - JAVA - Strings
Object Oriented Programming - JAVA - Strings
Strings
Agenda
● Exploring the String class
● String buffer class
● Command-line arguments
● Library:
○ StringTokenizer
○ Random class
○ Wrapper classes
● Encapsulation: Abstraction
● Creating User defined Data Structures:
○ Array of Objects
○ User defined Linked List
Exploring the String class
The String class is one of the most commonly used classes in Java and represents a sequence of characters.
1. Immutability: Strings in Java are immutable, meaning once a string object is created, its content cannot be changed.
This property ensures that strings are thread-safe and can be safely shared across multiple threads.
2. Creating Strings: You can create a string in Java using either a string literal or by using the new keyword with the String
Constructor.
1. String Concatenation: Strings in Java can be concatenated using the + operator or the concat() method.
1. String Length: You can get the length of a string using the length() method.
6. Substring: You can extract a substring from a string using the substring() method.
7. Equality and Comparison: You can compare strings for equality using the equals() method or for lexicographical order using
compareTo() method. boolean isEqual = str1.equals(str2);
8. String Manipulation: The String class provides various methods for manipulating strings, such as toUpperCase(), toLowerCase(), replace(
), trim(), startsWith(), endsWith(), contains(), etc.
9. Splitting: You can split a string into an array of substrings using the split() method.
command-line arguments can be passed to a program when it is executed from the command line. These arguments are
provided after the name of the Java class file and are passed as strings to the main method of the class.
Example:
Execute the compiled bytecode file: java MyClass arg1 arg2 arg3
Library
1. StringTokenizer:
● StringTokenizer class in Java is used to break a string into tokens based on a specified delimiter.
● It provides methods to retrieve individual tokens and to check whether there are more tokens left.
Example:
import java.util.StringTokenizer;
public class StringToken{
public static void main(String[] args) {
String str = "Hello,World,Java";
StringTokenizer tokenizer = new StringTokenizer(str, ",");
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
System.out.println(token);
}
}
}
Library Continue
2. Random class:
● Random class in Java is used to generate random numbers of different data types.
● It provides methods to generate random integers, doubles, floats, longs, and more.
Example:
import java.util.Random;
3. Wrapper classes:
● Wrapper classes in Java are used to represent primitive data types as objects.
● They provide methods to convert between primitive data types and their corresponding object representations.
Example:
public class Main {
public static void main(String[] args) {
Integer intValue = Integer.valueOf("123"); // Convert string to Integer
int intValuePrimitive = intValue.intValue(); // Convert Integer to int
Double doubleValue = Double.valueOf("3.14"); // Convert string to Double
double doubleValuePrimitive = doubleValue.doubleValue(); // Convert Double to
double
System.out.println("Integer value: " + intValue);
System.out.println("Double value: " + doubleValue);
}
}
Encapsulation: Abstraction
Creating User defined Data Structures:
Array of Objects: