dsa

Java String

In this tutorial, we will learn about Java String, how to create it and its various methods with the help of examples.

Strings in Java are Objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. Below is the basic syntax for declaring a string in Java programming language.

Syntax:

 <String_Type> <string_variable> = “<sequence_of_string>”;

Example:

 // Java code to illustrate String 
  
import java.io.*; 
import java.lang.*; 
  
class Test { 
    public static void main(String[] args) 
    { 
        // Declare String without using new operator 
        String c = "Codemistic"; 
  
        // Prints the String. 
        System.out.println("String c = " + c); 
  
        // Declare String using new operator 
        String c1 = new String("Codemistic"); 
  
        // Prints the String. 
        System.out.println("String c1 = " + c1); 
    } 
} 
Output:
String c = Codemistic
String c1 = Codemistic

Java String Methods

Methods Description
concat() joins the two strings together
equals() compares the value of two strings
charAt() returns the character present in the specified location
getBytes() converts the string to an array of bytes
indexOf() returns the position of the specified character in the string
length() returns the size of the specified string
replace() replaces the specified old character with the specified new character
substring() returns the substring of the string
split() breaks the string into an array of strings
toLowerCase() converts the string to lowercase
toUpperCase() converts the string to uppercase
valueOf() returns the string representation of the specified data

Let's take a few examples:

Example 1: Java find string's length

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

    // create a string
    String cod = "Hello! World";
    System.out.println("The string is: " + cod);

    //checks the string length
    System.out.println("The length of the string: " + cod.length());
  }
}

Output:

The string is: Hello! World
The length of the string: 12

In the above example, we have created a string named cod. Here we have used the length() method to get the size of the string.

Example 2: Java join two strings using concat()

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

    // create string
    String cod = "Hello! ";
    System.out.println("First String: " + cod);

    String name = "World";
    System.out.println("Second String: " + name);

    // join two strings
    String joinedString = cod.concat(name);
    System.out.println("Joined String: " + joinedString);
  }
}

Output:

First String: Hello!
Second String: World
Joined String: Hello! World 

n the above example, we have created 2 strings named cod and name.
Here, we have used the concat() method to join the strings. Hence, we get a new string named joinedString.

Example 3: Java join strings using + operator

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

    // create string
    String cod = "Hello! ";
    System.out.println("First String: " + cod);

    String name = "World";
    System.out.println("Second String: " + name);

    // join two strings
    String joinedString = cod + name;
    System.out.println("Joined String: " + joinedString);
  }
}

Output:

First String: Hello!
Second String: World
Joined String: Hello! World 

Here, we have used the + operator to join the two strings.

Example 4: Java compare two strings

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

    // create strings
    String first = "codemistic";
    String second = "codemistic";
    String third = "java programming";

    // compare first and second strings
    boolean result1 = first.equals(second);
    System.out.println("Strings first and second are equal: " + result1);

    //compare first and third strings
    boolean result2 = first.equals(third);
    System.out.println("Strings first and third are equal: " + result2);
  }
}

Output:
Strings first and second are equal: true
Strings first and third are equal: false

In the above example, we have used the equals() method to compare the value of two strings.

Example 5: Java get characters from a string

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

    // create string using the string literal
    String cod = "Hello! World";
    System.out.println("The string is: " + cod);

    // returns the character at 3
    System.out.println("The character at 3: " + cod.charAt(3));

    // returns the character at 8
    System.out.println("The character at 8: " + cod.charAt(7));
  }
}

Output:
The string is: Hello! World
The character at 3: l
The character at 8: o

In the above example, we have used the charAt() method to access the character from the specified position.

Example 6: Java Strings other methods

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

    // create string using the new keyword
    String name = new String("Codemistic! World");

    // returns the substring World
    System.out.println("Using the subString(): " + name.substring(13));

    // converts the string to lowercase
    System.out.println("Using the toLowerCase(): " + name.toLowerCase());

    // converts the string to uppercase
    System.out.println("Using the toUpperCase(): " + name.toUpperCase());

    // replaces the character '!' with 's'
    System.out.println("Using the replace(): " + name.replace('!', 's'));
  }
}

Output:
Using the subString(): World
Using the toLowerCase(): codemistic! world
Using the toUpperCase(): CODEMESTIC! WORLD
Using the replace(): Codemistics World

In the above example, we have created a string named name using the new keyword