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

Java Definition String

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.

  • Using double quotes (String literals): The simplest way to define a string is by enclosing the text within double quotes. This creates a string literal, which will be interned in the String pool.

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
  • Using the 'new' keyword (String objects): You can create a new string object using the 'new' keyword, followed by the 'String' constructor. This creates a new string object in the heap memory, even if an identical string already exists in the String pool.

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
  • Using the 'valueOf()' method (String objects from other data types): The 'String.valueOf()' method is a static method that converts various data types into strings. It is useful when you need to create a string from a non-string value, such as an integer, float, or boolean.

Example:

public class Main {
    public static void main(String[] args) {
        int number = 42;
        String str = String.valueOf(number);
        System.out.println(str);
    }
}

Output:

42
  • Using character arrays: You can create a string from a character array by passing the array to the 'String' constructor.

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.

  1. 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!";
    
  2. 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);
    
  3. 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
    
  4. 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!");
    
  5. 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);
    
  6. 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();
    
  7. 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!";