Strings in Java

A String is a chain of characters. String Class is provided by Java, and it implements strings as objects of the class String. In this lesson, we will learn about Strings in Java.

Let us see how to create strings in Java with examples.

Ways to Create a String in Java

There are multiple ways to create a string in Java:

Way 1: The following creates an empty string using the new:

String student = new String();

Way 2: Or, use the following to create an empty string:

String student;

Ways to Create a String and Initialize in Java

We can also create a string and initialize it in a single line:

Way 1: The following creates a string and also initializes it:

String message = "Amit";

Way 2: Or, use the following to create and initialize a string using the new:

String message = new String("Amit");

String – Other Examples

The following creates a string object. Initialized in the form of an array of characters,

String message = new String("Studyopedia provides free tutorials!"");

The following is another usage of Strings,

String result = "New York tops the list of most expensive cities for Business";

Coding example to create a string in Java

Let us see an example to create strings in Java:

// Create a String in Java

class Demo53 {
    public static void main(String[] args) {

        String message = "Hi";
        
        // String message = new String ("Hi");
        System.out.println(message);
    }
}

The following is the output,

Hi

String Concatenation in Java

Let us see how to concatenate two strings. To concat a string in Java, use the String concat() method,

// String Concatenation in Java

class Demo54 {
    public static void main(String[] args) {

        String message1 = "Hi";
        System.out.println(message1);

        String message2 = ", Amit";
        System.out.println(message2);

        System.out.println("After concatenation = " + message1.concat(message2));
    }
}

Output

Hi
, Amit
After concatenation = Hi, Amit

Why are Strings Immutable

Strings in Java cannot be changed once created. Therefore, String objects are immutable. Remember that this does not apply to reference variables.

An example would make it easier to understand.

String str1 = “Example”;

Now, referred to str2,

String str2 = str1;

The following displays the text Example,

System.out.println(str1);
System.out.println(str2);

Now let us use the concat() method,

str1 = str1.concat(“Question”);

Printing str1 displays, Example Question, i.e., concatenation,

System.out.println(str1);

Printing str2, displays, Example,

System.out.println(str2);

Since concatenation did not refer to string str2, the concatenation result is not visible.

Let us now see the methods in the String class that allow you to manipulate strings.

Built-in String Methods

String class methods allow easier manipulation of strings in Java. Below, the methods are explained with suitable examples.

  • byte getBytes()
    Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.
  • int indexOf(int ch)
    Returns the index within this string of the first occurrence of the specified character.
  • String intern()
    Returns a canonical representation for the string object.
  • String toLowerCase()
    Converts all of the characters in this String to lowercase using the rules of the default locale.
  • String toUpperCase()
    Converts all of the characters in this String to upper case using the rules of the default locale.
  • String trim()
    Returns a copy of the string, with leading and trailing whitespace omitted.

Reference: Oracle Java String Class

In this lesson, we learned how to work with Strings in Java.

Multidimensional Arrays in Java
String chartAt() method in Java
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment