allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œBut those who hope in the Lord will renew their strength. They will soar on wings like eagles; they will run and not grow weary, they will walk and not be faint.โ€
[Isaiah 40:31]


String and Expressions


3.2.1 Understanding Strings in Java

In Java, strings are not primitive types โ€” they are objects of the String class.
A string represents a sequence of characters and is one of the most frequently used data types in any Java application.
In web development, strings appear everywhere: reading user input, displaying messages, handling URLs, parsing JSON, building dynamic HTML, and more.

A unique thing about strings in Java is that they are immutable.
This means once you create a string, its value cannot be changed.
When you โ€œchangeโ€ a string, you are actually creating a new string in memory.
This behavior helps make string operations safe and predictable, especially in multi-threaded environments.

Java provides many built-in methods for working with strings โ€” such as length(), charAt(), substring(), indexOf(), toUpperCase(), toLowerCase(), trim(), and replace().
Mastering these methods is essential for processing user data and creating dynamic content.

Mini Lab

java
public class StringBasics { public static void main(String[] args) { String message = "Hello, Java!"; System.out.println("Message: " + message); int length = message.length(); System.out.println("Length: " + length); char firstChar = message.charAt(0); System.out.println("First character: " + firstChar); String upper = message.toUpperCase(); System.out.println("Uppercase: " + upper); String replaced = message.replace("Java", "World"); System.out.println("Replaced: " + replaced); } }

3.2.2 String Concatenation and Formatting

String concatenation means joining multiple strings (or strings and other data types) into one combined string.
In Java, the + operator is used for simple concatenation, which makes it easy to join strings with variables or literals.
For example, you might display โ€œWelcome, [username]!โ€ by combining a fixed message with a userโ€™s name.

While + is simple, it can become inefficient when building large or complex strings inside loops.
For these cases, Java provides the StringBuilder class, which is designed for efficient, mutable string operations.
Using StringBuilder avoids creating many intermediate string objects and helps performance.

Another important tool is String.format(), which lets you insert values into a string template in a cleaner way, similar to printf in C.
This is especially useful for creating dynamic messages or building output for reports, logs, and web pages.

Understanding when to use +, StringBuilder, or String.format() helps you write clean, fast, and readable code.

Mini Lab

java
public class StringConcatAndFormat { public static void main(String[] args) { String name = "Alice"; int age = 30; // Using + String greeting = "Hello, " + name + "! You are " + age + " years old."; System.out.println(greeting); // Using StringBuilder StringBuilder sb = new StringBuilder(); sb.append("Hello, ").append(name).append("! Next year, you will be ").append(age + 1).append("."); System.out.println(sb.toString()); // Using String.format String formatted = String.format("Hello, %s! Your age is %d.", name, age); System.out.println(formatted); } }

3.2.3 Using Strings in Expressions

Strings often appear as part of expressions in Java โ€” combinations of variables, operators, and method calls that produce a value.
Expressions can mix strings with numbers, booleans, and other data types.
When you use + with a string and a number, Java automatically converts the number to a string โ€” this is called implicit type conversion.

Strings are also used in conditions and control flow.
For example, you might check if a userโ€™s input matches a keyword, or whether a URL contains a specific parameter.
Comparing strings uses .equals() or .equalsIgnoreCase(), because == checks for reference equality, not value equality.

In web development, strings are a key part of building dynamic content.
They help construct SQL queries, generate HTML templates, and pass data through APIs.
Learning to safely build and handle strings โ€” especially user input โ€” is critical for avoiding bugs and security issues like injection attacks.

Mini Lab

java
public class StringExpressions { public static void main(String[] args) { String input = "Hello"; String keyword = "hello"; // Comparing strings boolean match = input.equals(keyword); boolean matchIgnoreCase = input.equalsIgnoreCase(keyword); System.out.println("Exact match: " + match); System.out.println("Match ignoring case: " + matchIgnoreCase); // Using string in an expression with number int count = 5; String result = "You have " + count + " new messages."; System.out.println(result); // Condition with string if (input.startsWith("He")) { System.out.println("Input starts with 'He'"); } } }