Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, a string is a sequence of characters. The java.lang.String
class is used to represent strings, and it provides various methods for working with strings, such as concatenation, comparison, and manipulation. In this tutorial, we will learn about different ways to define and create strings in Java.
Example:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; System.out.println(str1); System.out.println(str2); } }
Output:
Hello World
Example:
public class Main { public static void main(String[] args) { String str1 = new String("Hello"); String str2 = new String("World"); System.out.println(str1); System.out.println(str2); } }
Output:
Hello World
Example:
public class Main { public static void main(String[] args) { int number = 42; String str = String.valueOf(number); System.out.println(str); } }
Output:
42
Example:
public class Main { public static void main(String[] args) { char[] charArray = {'H', 'e', 'l', 'l', 'o'}; String str = new String(charArray); System.out.println(str); } }
Output:
Hello
In conclusion, there are various ways to define and create strings in Java. The choice depends on the specific use case and requirements. String literals created with double quotes are interned in the String pool, while strings created with the 'new' keyword or other methods create separate objects in the heap memory.
Definition of String class in Java:
The String
class in Java is a predefined class that represents a sequence of characters. It belongs to the java.lang
package and is widely used for handling textual data.
String str = "Hello, Java!";
Creating and manipulating strings in Java:
Strings can be created using the String
constructor or by using string literals. Various methods like concat()
, substring()
, and replace()
allow manipulation of strings.
String str1 = new String("Hello"); String str2 = "Java"; String result = str1.concat(" " + str2);
String literals in Java: String literals are sequences of characters enclosed in double quotes. They are automatically interned, meaning that only one copy of each unique string literal is stored in the String pool.
String literal1 = "Java"; String literal2 = "Java"; // Reuses the existing "Java" from the pool
Immutable nature of strings in Java: Strings in Java are immutable, meaning their values cannot be changed once they are created. Any operation that seems to modify a string actually creates a new string.
String original = "Hello"; String modified = original.concat(", Java!");
Concatenating and formatting strings in Java:
String concatenation can be done using the +
operator or concat()
method. Formatting can be achieved using String.format()
.
String name = "John"; int age = 25; String formattedString = String.format("My name is %s and I am %d years old.", name, age);
Java String vs StringBuilder vs StringBuffer:
String
is immutable, while StringBuilder
and StringBuffer
are mutable. StringBuilder
is not thread-safe, while StringBuffer
is. Use StringBuilder
for better performance in single-threaded scenarios.
StringBuilder sb = new StringBuilder("Hello"); sb.append(", Java!"); String result = sb.toString();
Java String initialization and declaration:
Strings can be initialized and declared using the String
keyword, and values can be assigned using literals or by creating new instances.
String greeting = "Hello, World!";