A String in Java is a sequence of characters, used to represent text. Strings are objects in Java and are created using double quotes, like "Hello, Java!"
. Once a String is created, it cannot be changed, which is why Strings are called immutable. They are stored in a special memory area called the String pool to save memory. You can perform various operations on strings, like concatenation, comparison, and substring extraction..
If you’re preparing for an interview, check out our Top 10 Java String Interview Questions and Answers to help you understand important concepts and get ready for your next Java interview.
1. What is a String in Java?
In Java, a String is an object that represents a sequence of characters. It’s widely used to handle text in Java programs and is one of the most essential data types in Java programming.
2. How to Create String Objects in Java?
There are two primary ways to create String objects in Java:
- String Literal: A string can be created using double quotes, like
"JAVA"
. These strings are stored in the String Constant Pool, which makes them immutable. Once created, the value cannot be changed. - Using the
new
Keyword: A string created with thenew
keyword is stored in the Heap memory and can be modified.
3. Key String Methods in Java or some String methods names ? Imp question for fresher
Java provides several methods to perform operations on String objects. Some of the most commonly used string methods include:
compare()
concat()
equals()
hashCode()
split()
length()
replace()
compareTo()
intern()
substring()
4. How to Compare Strings in Java?
There are three primary ways to compare String objects in Java:
-
Using
equals()
Method: Compares the content of two strings. This method checks if the characters in both strings are the same, regardless of whether the strings are stored in the String Pool or Heap.Example:
String s1 = "JAVA"; String s2 = "JAVA"; System.out.println(str1.equals(str2)); // true System.out.println(str1 == str2); // true (both are from String pool)
-
Using
==
Operator: Compares the references (memory location) of the two strings. If both strings point to the same object in memory, it returnstrue
; otherwise, it returnsfalse
. -
Using
compareTo()
Method: Compares strings lexicographically. The method returns: 0
if both strings are equal.- A positive number if the first string is lexicographically greater.
- A negative number if the first string is lexicographically smaller.
5. What is an Immutable Object in Java & how to create Immutable class? Most Imp question for fresher and experience
An Immutable Object is one whose state cannot be changed once it’s created. In multi-threaded applications, immutability is useful to prevent issues related to concurrent modification by multiple threads.
To create an immutable class in Java, follow these guidelines:
- Make the class
final
so it cannot be subclassed. - Make fields
final
so they are initialized only once. - Do not provide setter methods, ensuring the fields cannot be modified from outside.
- For methods that might modify the state, return a new object instead of modifying the existing one.
Example: Immutable Class in Java
final class Employee { // Class is final
private final String penCardNumber; // Field is final
public Employee(String penCardNumber) {
this.penCardNumber = penCardNumber;
}
public String getPenCardNumber() {
return penCardNumber; // Only getter, no setter
}
}
public class ImmutableClassDemo {
public static void main(String[] args) {
Employee employee = new Employee("Aryan123");
System.out.println("Pen Card Number: " + employee.getPenCardNumber());
}
}
6. What is a Buffer in Java?
A Buffer is a temporary storage area in the memory, often used to store data that is being transferred between a device and the CPU or other components. Buffers speed up data transfer since accessing data in memory is faster than fetching it from storage devices like hard drives.
7. What is the difference between String, StringBuilder, and StringBuffer? Imp question for fresher
- String: Immutable, meaning it cannot be changed after creation.
- StringBuilder: Mutable (can be changed), faster but not thread-safe.
- StringBuffer: Mutable, slower than StringBuilder but thread-safe.
8. What is String concatenation in Java? Imp question for fresher
Concatenation is joining two or more strings together. You can do this with the +
operator or the concat()
method.
Example:
String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName;
9. How to convert a String to a number in Java? Imp question for fresher
You can convert a String to a number using methods like Integer.parseInt()
or Double.parseDouble()
.
Example:
String number = "100"; int result = Integer.parseInt(number);
10. What is the substring()
method in Java? Imp question for fresher
The substring() method extracts part of a string. You specify the starting index and (optionally) the ending index.
Example:
String text = "Hello, Java!"; String result = text.substring(7); // Output: "Java!"
11. How do you find the length of a String in Java? Imp question for fresher
To find the length of a String, use the length()
method.
Example:
String text = "Hello, Java!";
int length = text.length(); // Output: 12
10. What is the difference between String.valueOf()
and toString()
? Imp question for fresher
String.valueOf()
: Converts any data type (like int, double) into a String.toString()
: Converts objects into a String. If the object is null,toString()
will throw an exception, butvalueOf()
will return “null.”
Example:
int number = 100;
String str = String.valueOf(number); // Output: "100";
For more advanced Java String coding examples, including Java 8 Stream String Programs, check out our detailed resources to enhance your understanding. Whether you’re preparing for an interview or looking to improve your coding skills, we’ve got you covered.