Java provides ready classes for text and for flexible collections, and ways to treat primitive values as objects.

Strings

In Java, a string is an object of the class String. String literals are written in double quotes: String s = "Java";. Strings are immutable: once created, their content cannot change; methods that seem to change a string return a new one. Useful methods are length(), charAt(i), substring(a, b), indexOf(str), toUpperCase(), toLowerCase(), trim(), replace(a, b), concat(s), split(regex), equals(s), equalsIgnoreCase(s) and compareTo(s), which compares alphabetically, returning a negative, zero or positive number. Strings are joined with +. To compare contents, use equals(), not ==.

Definition: Wrapper class. A class that wraps a value of a primitive type in an object, such as Integer for int and Double for double, and provides methods to convert and work with it.

StringBuffer and StringBuilder

When a string must be changed many times, the StringBuffer (thread-safe) or StringBuilder (faster) class is used. They are mutable: append(), insert(), delete(), reverse() and setCharAt() change the same object. reverse() makes palindrome checks easy.

Vectors

A Vector (in java.util) is a growable array of objects. Its size increases as elements are added: v.addElement(x) or v.add(x), v.elementAt(i) or v.get(i), v.removeElement(x), v.size() and v.insertElementAt(x, i). Vectors hold objects only, not primitives. ArrayList is a similar, faster, non-synchronised class, preferred in new code.

Wrapper Classes

Each primitive type has a wrapper class: Byte, Short, Integer, Long, Float, Double, Character and Boolean. Wrappers let primitives be stored in collections such as Vector, and provide conversion methods such as Integer.parseInt("25"), Integer.toString(25) and Integer.valueOf("25"), and constants such as Integer.MAX_VALUE. Java converts automatically between a primitive and its wrapper: autoboxing (int to Integer) and unboxing (Integer to int).

Enumerated Types

An enum defines a type with a fixed set of named constants: enum Day { MON, TUE, WED }. Enums are type-safe, can be used in switch, and provide values() to list all constants and ordinal() to give a constant's position.

Table 3.4: String and StringBuffer

Feature String StringBuffer
Mutable No Yes
Change content Creates a new object Changes the same object
Thread-safe Yes (immutable) Yes
Typical use Fixed text Text built step by step

Example 3.4: Palindrome, sorting names and a vector

Problem: (a) Check whether "MADAM" is a palindrome. (b) Sort the names Ravi, Asha, Meera alphabetically. (c) Store 10, 20, 30 in a Vector and print its size.

Solution:

  1. Reverse the string with StringBuffer and compare with equals(): MADAM reversed is MADAM, so it is a palindrome.
  2. Compare names with compareTo() (or use Arrays.sort): Asha, Meera, Ravi.
  3. Add the three numbers (autoboxed to Integer) and print size() = 3.

Result: MADAM is a palindrome; the sorted names are Asha, Meera, Ravi; the vector size is 3.

import java.util.Arrays;
import java.util.Vector;
public class StringDemo {
    enum Day { MON, TUE, WED }
    public static void main(String[] args) {
        String s = "MADAM";
        String r = new StringBuffer(s).reverse().toString();
        System.out.println(s + (s.equals(r) ? " is" : " is not") +
                " a palindrome");
        String[] names = {"Ravi", "Asha", "Meera"};
        Arrays.sort(names);
        System.out.println(Arrays.toString(names));
        Vector<Integer> v = new Vector<>();
        v.add(10); v.add(20); v.add(30);
        System.out.println("vector " + v + ", size " + v.size());
        int n = Integer.parseInt("25") + v.get(0);
        System.out.println("25 + 10 = " + n + ", " + Day.TUE +
                " is at " + Day.TUE.ordinal());
    }
}

Output

MADAM is a palindrome
[Asha, Meera, Ravi]
vector [10, 20, 30], size 3
25 + 10 = 35, TUE is at 1

Common mistakes

  • Comparing strings with ==, which compares references.
  • Expecting a String method such as toUpperCase() to change the original string.
  • Storing primitives directly in a Vector without wrappers (autoboxing does this for you).

Key takeaways

  • String is immutable; StringBuffer and StringBuilder are mutable.
  • Compare strings with equals() and compareTo().
  • A Vector is a growable array of objects.
  • Wrapper classes box primitives; enums define fixed sets of constants.

Practice questions

  1. What does it mean that strings are immutable?
  2. What is autoboxing?
  3. How is a vector different from an array?

Answers to the practice questions

  1. Their content cannot be changed after creation; changes produce new strings.
  2. Automatic conversion of a primitive value to an object of its wrapper class.
  3. A vector grows automatically as elements are added, while an array has a fixed size.

Sources

  • Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit III