Learning Mutable Strings in Java with StringBuffer & StringBuilder

🚀 Day 23 of Learning Java @ Tap Academy 📘 Mutable Strings in Java Today, I learned about mutable strings—strings whose values can be changed after creation. 🔹 Classes used: ✔️ StringBuffer ✔️ StringBuilder These classes are used to create modifiable (mutable) strings in Java. 🔹 StringBuffer Basics: ✔️ Default capacity is 16 ✔️ Used to create mutable strings Example: StringBuffer sb = new StringBuffer(); sb.append("Hello"); System.out.println(sb); 🔹 Important Methods: ✔️ append(String s) → Adds text to the end ✔️ length() → Returns length of string ✔️ capacity() → Returns current capacity ✔️ ensureCapacity(n) → Increases capacity 📌 Capacity formula: New Capacity = (old capacity × 2) + 2 🔹 StringTokenizer: Used to split a string into tokens. Example: String s = "Java Python SQL AI"; StringTokenizer st = new StringTokenizer(s, " "); while(st.hasMoreTokens()) { System.out.println(st.nextToken()); } 💡 Key Takeaway: Mutable strings are efficient when frequent modifications are required. #TapAcademy #Java #LearningJava #Day23 #CodingJourney #JavaBasics #100DaysOfCode

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories