08 String and String Buffer Class
08 String and String Buffer Class
examples.
ANS:
The String class in Java is part of the java.lang package and is widely used for
representing sequences of characters. It's important to note that String objects
in Java are immutable, meaning once a String object is created, its value cannot
be changed. Any operation that appears to modify a String actually creates a new
String object.
(1) length():
• Returns the length of the string.
• Example:
String s1 = "Hello";
String s2 = "HELLO";
boolean isEqual = s1.equalsIgnoreCase(s2);
// isEqual is true
(9) trim():
• Removes leading and trailing whitespaces.
• Example:
Example:
public class StringFunctionsExample {
// 1. length()
int length = originalStr.length();
System.out.println("Length of the string: " + length);
// 2. charAt(int index)
char charAtIndex = originalStr.charAt(7);
System.out.println("Character at index 7: " + charAtIndex);
// 3. substring(int beginIndex)
String substring = originalStr.substring(7);
System.out.println("Substring from index 7: " + substring);
// 5. equals(Object obj)
String anotherStr = "Hello, World!";
boolean isEqual = originalStr.equals(anotherStr);
System.out.println("Are the strings equal? " + isEqual);
// 6. equalsIgnoreCase(String anotherString)
String caseSensitive = "Hello";
String caseInsensitive = "HELLO";
boolean isEqualIgnoreCase =
caseSensitive.equalsIgnoreCase(caseInsensitive);
System.out.println("Are the strings equal (ignoring case)? " +
isEqualIgnoreCase);
// 7. compareTo(String anotherString)
String str1 = "apple";
String str2 = "banana";
int result = str1.compareTo(str2);
System.out.println("Comparison result: " + result);
// 9. trim()
String withSpaces = " Hello, World! ";
String trimmedStr = withSpaces.trim();
System.out.println("Trimmed string: " + trimmedStr);
(3) append():
(4) insert():
(6) reverse():
(7) replace():
(9) substring():
• Returns a new String that contains a subsequence of characters from the
StringBuffer.
• Example:
StringBuffer sb = new StringBuffer("Hello, World!");
String subString = sb.substring(7); // subString is "World!"
(10) indexOf() and lastIndexOf():
These are some of the commonly used methods provided by the StringBuffer
class in Java. It's important to note that StringBuffer is synchronized, making it
safe for use in multithreaded environments. However, if synchronization is not
needed, the non-synchronized StringBuilder class can be used, which is similar
to StringBuffer but more efficient in a single-threaded context.
Example:
public class StringBufferExample {
// 1. append()
stringBuffer.append(" World");
System.out.println("1. append(): " + stringBuffer);
// 2. insert()
stringBuffer.insert(5, " Beautiful");
System.out.println("2. insert(): " + stringBuffer);
// 4. reverse()
stringBuffer.reverse();
System.out.println("4. reverse(): " + stringBuffer);
// 5. replace()
stringBuffer.replace(0, 2, "Hi"); // Replaces "He" with "Hi"
System.out.println("5. replace(): " + stringBuffer);
// 7. substring()
String subString = stringBuffer.substring(2, 5); // Substring from index 2 to 4
System.out.println("7. substring(): " + subString);
// 10. toString()
String result = stringBuffer.toString();
System.out.println("10. toString(): " + result);
}
}
QUE: Explain the StringJoiner class in Java with all its methods with
proper examples.
The StringJoiner class in Java is part of the java.util package and was
introduced in Java 8 as a utility for constructing sequences of characters
separated by a delimiter, with optional prefix and suffix. It is particularly useful
when you need to concatenate multiple strings with a specified separator.
Here's an overview of the StringJoiner class and its methods with examples:
(1) Constructors:
• StringJoiner(CharSequence delimiter): Creates a StringJoiner with
the specified delimiter.
StringJoiner joiner = new StringJoiner(", ");
(4) length():
• Returns the length (number of characters) of the current sequence.
• Example:
// Adding elements
fruits.add("Apple").add("Banana").add("Orange");
fruits.merge(moreFruits);
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
import java.util.ArrayList;
class Autoboxing {
public static void main(String[] args)
{
char ch = 'a';
// Autoboxing- primitive to Character object
// conversion
Character a = ch;
ArrayList<Integer> arrayList
= new ArrayList<Integer>();
2. Unboxing
It is just the reverse process of autoboxing. Automatically converting an object
of a wrapper class to its corresponding primitive type is known as unboxing. For
example – conversion of Integer to int, Long to long, Double to double, etc.
// Java program to demonstrate Unboxing
import java.util.ArrayList;
class Unboxing {
public static void main(String[] args)
{
Character ch = 'a';
ArrayList<Integer> arrayList
= new ArrayList<Integer>();
arrayList.add(24);
// unboxing because get method returns an Integer
// object
int num = arrayList.get(0);
class GFG {
public static void main(String[] args)
{
// byte data type
byte a = 1;