What is String?

What is String

In Java, An object that represents sequences of the alphanumeric alphabet or characters is called String. It’s a derived data. It’s a non-primitive data type. Actually, Strings are objects of a predefined class named ‘String’. The ‘String’ class is part of the ‘java.lang’ package and is automatically imported into every Java Program, so we don’t need to import it explicitly. And, all string variables are instances of the ‘String’ class. Every character can be used in the string but must be in double quotes.

For example:

String email="michrotech124@";

Here, we have created a String variable named email. The variable is initialized with the string “michrotech124@

Creating a String

There are two ways to create a String in Java

  1. By string literal
  2. By using new keyword

String literal:

Java String literal is created by using double quotes. It is a convenient way to represent a string value directly in the source code. String literals are a part of the Java language syntax and are automatically converted to instances of the String class at compile time.

String literals make it easy to work with textual data in Java programs. They are widely used for representing messages, user inputs, file paths, and various other forms of textual information. Since string literals are automatically converted to String objects, you can use the extensive set of methods provided by the String class to manipulate and process string data.

For Example:
String s="Hello, World!" ;

In this example, “Hello, World!” is a string literal. It represents the string “Hello, World!” and is allocated to the String-type variable message. The double quotes define the beginning and end of the string literal, and any characters inside the quotes are part of the string value.

String literals can contain any combination of characters, including letters, digits, special symbols, whitespace, escape sequences, and Unicode characters. Here are a few more examples of string literals:

public class StringExample {
    public static void main(String[] args) {
        String name = "James Grosling"; // A string with letters and whitespace
        String greeting = "Welcome to \'Java World!\'!\n"; // A string with an escape sequence for a new line
        String emoji = "\uD83D\uDE03"; // A string with Unicode characters representing an emoji
        String filePath = "C:\\data\\file.txt"; // A string with escape sequences for backslashes

James Grosling

Welcome to ‘Java World!’!




In this example, this code demonstrates the use of different types of strings. Here’s a breakdown of what’s happening:

String name = “James Grosling”: This line defines a string variable named name and assigns it the value “James Grosling”.

  • String greeting = “Welcome to \’Java World!\’!\n”: Here, a string variable named greeting is created with the value “Welcome to ‘Java World!’!” including an escape sequence \n which represents a new line.
  • String emoji = “\uD83D\uDE03”: This line declares a string variable emoji containing Unicode escape sequences that represent an emoji (πŸ˜ƒ).
  • String filePath = “C:\\data\\file.txt”: This line creates a string variable filePath with the value “C:\data\file.txt”. The double backslashes (\\) are escape sequences to represent a single backslash in the string.
  • System.out.println(name): This line prints the value of the name string to the console.
  • System.out.println(greeting): This line prints the greeting string to the console. The \n escape sequence causes a new line to be printed after “Welcome to ‘Java World!’!”.
  • System.out.println(emoji): This line prints the emoji stored in the emoji string to the console.
  • System.out.println(filePath): Finally, this line prints the filePath string, which represents a file path, to the console.

The code demonstrates how to create and display various types of strings in Java, including those with special characters, escape sequences, and Unicode characters.

When we assign a variable of data type string then the value (string object) does not store directly in heap memory. At first, a particular memory area is created in heap memory, known as the “string constant pool”. Then the value of the string variable is stored in that particular memory.

Using new Keyword

Another way of creating a string in Java is by using the String class constructor. While string literals are automatically generated by the Java compiler, using the constructor enables you to create string objects programmatically at runtime. Using the ‘String’ class constructor to create strings gives you more flexibility when you need to construct strings dynamically, such as when reading data from external sources or performing complex string manipulations.

String address = new String("123 Main Street");

In this example, we use the new keyword and the String constructor to create a new string object representing the address “123 Main Street.” The String constructor takes a string literal or a character array as an argument and converts it into a String object.

Another example:
public class StringExample {
    public static void main(String[] args) {
        char[] charArray = { 'H', 'e', 'l', 'l', 'o' };
        String greeting = new String(charArray);



This code demonstrates how to create a `String` object from an array of characters (`char[]`). First, we will analyze the code by going through each step in detail.

  1. `char[] charArray = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ };`: Here, an array of characters named `charArray` is created and initialized with the characters ‘H’, ‘e’, ‘l’, ‘l’, and ‘o’. This array represents the individual characters of the word “Hello”.
  2. `String greeting = new String(charArray);`: In this line, a new `String` object named `greeting` is created using the constructor of the `String` class. This constructor takes an argument of type `char[]`, which is the array of characters we defined earlier (`charArray`). The constructor converts the array of characters into a string and assigns it to the `greeting` variable. So, the `greeting` string now holds the value “Hello”.
  3. `System.out.println(greeting);`: This line prints the value of the `greeting` string to the standard output using the `System.out.println()` method. As a result, “Hello” will be printed to the console.

In summary, the code creates a character array containing the letters of the word “Hello”, converts this character array into a `String` object using the `String` class constructor, and then prints out the resulting string, which is “Hello”.

Here JVM will create a new string object in normal heap memory. Here no “string constant pool” memory is created.


Visited 1 times, 1 visit(s) today

Comments are closed.