Java

Java – String Class And Methods

A string is a group of characters, such as the five letters in the word “Hello.” String is an immutable object in Java, which implies that once it has been created, it cannot be altered. With the help of examples, we will learn about the String class and its methods in this lesson.

Creating a String

In Java, there are two methods to produce a String.

  1. String literal
  2. Using new keyword

1. String literal

A series of characters wrapped in double quotation marks (“”) is referred to as a string literal. By assigning a String literal to a String object in Java, Strings may be created:

String str1 = "BeginnersBook";
String str2 = "BeginnersBook";

The problem with this approach: As I said at the outset, in Java, a string is an object. However, despite utilizing the new keyword in the aforementioned commands, no string objects have been generated.

Internally, the compiler searches the memory for the string (this memory is often referred as string constant pool). If the string cannot be discovered, the program constructs an object with the string value—in this case, “Welcome”—and assigns a reference to it.

In our example, a reference to the string “BeginnersBook” is copied to the string str1, but for the string str2, the compiler locates the string in the string constant pool and assigns the same reference to the string str2 rather than creating a new object.

What if we want to use the same string on two separate objects? To do so, we would need to use the new keyword to generate strings.

2. Using New Keyword

The new keyword is used to create a new instance of a string. As seen in the accompanying figure, when we generate a string using the new keyword, it is formed in heap memory rather than the string constant pool. No matter if the string is already existent in the heap memory or not, employing the new keyword always creates a new string.

String str3 = new String("BeginnersBook");
String str4 = new String("BeginnersBook");

Example 1: Java String vs new String

To further understand the differences between the two methods that a string may be created in Java, let’s develop a string programming application.

class JavaExample
{
  public static void main(String args[])
  {
    //creating string using string literal
    String s1 = "BeginnersBook";
    String s2 = "BeginnersBook";

    //creating strings using new keyword
    String s3 = new String("BeginnersBook");
    String s4 = new String("BeginnersBook");

    if(s1 == s2){
      System.out.println("String s1 and s2 are equal");
    }else{
      System.out.println("String s1 and s2 are NOT equal");
    }

    if(s3 == s4){
      System.out.println("String s3 and s4 are equal");
    }else{
      System.out.println("String s3 and s4 are NOT equal");
    }

  }
}

Output:

Example 2: A Simple Java String Example

public class JavaExample{
  public static void main(String args[]){
    String str = "Beginnersbook";

    //declaring a char array
    char arrCh[]={'h','e','l','l','o'};

    //converting char array arrCh[] to string str2
    String str2 = new String(arrCh);

    //creating another java string str3 by using new keyword
    String str3 = new String("Java String Example");

    //Displaying all the three strings
    System.out.println(str);
    System.out.println(str2);
    System.out.println(str3);
  }
}

Output:

Beginnersbook
hello
Java String Example

Example 3: Displaying first and last character of a String

We are utilizing the string class’s charAt() and length() methods for this. Links to these tutorials are given following these examples. We have covered every approach in depth.

public class JavaExample {
  public static void main(String[] args) {
    String str = "Welcome to BeginnersBook.com";

    //finding length of the string using length() method.
    int len = str.length();

    // First character of the string
    System.out.println("First character: "+ str.charAt(0));

    // Last character
    System.out.println("Last character: "+ str.charAt(len-1));
  }
}

Output:

First character: W
Last character: m

Example 4: Comparing the string vs new string using equals()

As we saw when we used the == operator to compare the strings, it compared the references of the strings. Use the equals() function of the string class to compare the values of the strings.

public class JavaExample {
  public static void main(String[] args) {
    String str = "Hello"; //creating using literal
    String str2 = new String("Hello"); //using new keyword

    if(str.equals(str2)){
      System.out.println("Strings str and str2 are equal");
    }else{
      System.out.println("Strings str and str2 are NOT equal");
    }
  }
}

Output:

Java – String Class And Methods

Strings str and str2 are equal

Example 5: String concatenation

public class JavaExample {
  public static void main(String[] args) {
    String str = "Welcome";
    String str2 = "Home";
    System.out.println(str.concat(" ").concat(str2));
  }
}

Output:

Welcome Home

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button