Java Strings – Concatenation, Numbers & Special Characters
Strings are essential in Java, and often we need to combine strings, work with numbers in strings, or use special characters. This tutorial covers all these aspects with examples.
Java String Concatenation
String concatenation means joining two or more strings together.
Using + Operator
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // John Doe
Using concat() Method
String str1 = "Java";
String str2 = " Programming";
String result = str1.concat(str2);
System.out.println(result); // Java Programming
Concatenation with Numbers
When you combine strings and numbers using +, numbers are converted to strings automatically.
int age = 25;
String message = "Age: " + age;
System.out.println(message); // Age: 25
Tip: Use parentheses to control operations with numbers.
int a = 10, b = 20;
System.out.println("Sum: " + a + b); // Sum: 1020
System.out.println("Sum: " + (a + b)); // Sum: 30
Java Numbers and Strings
Java allows converting numbers to strings and vice versa.
Number to String
int num = 100;
String str = String.valueOf(num); // "100"
String str2 = Integer.toString(num); // "100"
String to Number
String str = "200";
int number = Integer.parseInt(str); // 200
double value = Double.parseDouble("99.99"); // 99.99
Example: Concatenate Numbers with Strings
int x = 50;
int y = 30;
String result = "Total: " + (x + y);
System.out.println(result); // Total: 80
Java Special Characters
Special characters in Java allow you to include characters that are not easily typed or control text formatting.
Common Escape Sequences
| Escape Sequence | Description | Example |
|---|---|---|
\\n | New line | "Hello\\nWorld" → |
| Hello | ||
| World | ||
\\t | Tab | "Hello\\tWorld" → Hello World |
\\\\ | Backslash | "C:\\\\Java\\\\Files" → C:\Java\Files |
\\" | Double quote | "He said, \\"Hello\\"" → He said, "Hello" |
\\' | Single quote | "It\\'s Java" → It's Java |
\\r | Carriage return | "Hello\\rWorld" |
\\b | Backspace | "Java\\bScript" → JaaScript |
Example: Using Special Characters
public class SpecialChars {
public static void main(String[] args) {
System.out.println("Line1\\nLine2");
System.out.println("Column1\\tColumn2");
System.out.println("Path: C:\\\\Java\\\\Files");
System.out.println("Quote: \\"Java is fun\\"");
}
}
Output:
Line1
Line2
Column1 Column2
Path: C:\\Java\\Files
Quote: "Java is fun"
Points to Know :
- Use
+orconcat()for string concatenation. - Numbers can be converted to strings using
String.valueOf()ortoString(). - Strings with numbers can be concatenated; use parentheses to avoid errors.
- Escape sequences allow special characters in strings and formatting.
- Strings are immutable; concatenation always creates new objects.
🏆 Top 5 Java Interview Questions - Strings: Concatenation & Special Characters
1. Difference between + operator and concat() method in Java
Answer:
| Feature | + Operator | concat() Method |
|---|---|---|
| Type | Operator | Method |
| Null Handling | Converts null to "null" | Throws NullPointerException if string is null |
| Concatenation | Works with strings + other data types | Only works with strings |
| Usage | String s = "Hello " + "World"; | String s = "Hello ".concat("World"); |
Example:
String s1 = "Hello";
String s2 = "World";
System.out.println(s1 + " " + s2); // Hello World
System.out.println(s1.concat(" ").concat(s2)); // Hello World
2. How to convert a number to a string and vice versa
Answer:
- Number → String:
int num = 100;
String str1 = String.valueOf(num);
String str2 = Integer.toString(num);
- String → Number:
String str = "100";
int num = Integer.parseInt(str);
double d = Double.parseDouble("3.14");
3. What are escape sequences in Java? Name 5 commonly used ones
Answer: Escape sequences are special characters used inside strings to represent non-printable or special characters.
| Escape Sequence | Description |
|---|---|
\\n | New line |
\\t | Tab |
\\\\ | Backslash |
\\" | Double quote |
\\r | Carriage return |
Example:
System.out.println("Hello\\nWorld"); // Prints on two lines
System.out.println("Hello\\tWorld"); // Adds a tab space
4. What happens if you concatenate numbers with strings without parentheses?
Answer:
- Java evaluates from left to right.
- If a string appears first, the rest are converted to strings.
- Without parentheses, addition happens first if numbers are together, then concatenation.
Example:
System.out.println(1 + 2 + "Java"); // "3Java"
System.out.println("Java" + 1 + 2); // "Java12"
5. Why are strings immutable even when using concatenation?
Answer:
- Concatenation creates a new String object; the original string remains unchanged.
- This maintains security, memory efficiency, and thread safety.
- Example:
String s = "Hello";
s = s + " World"; // Creates a new string object
System.out.println(s); // Hello World
