Skip to content

Java String Class | Syntax | Methods | Examples

  • by

The string is a sequence of characters. With Java String class can create a string object. Java String Object is immutable, which means once string objects have created then its values can’t be changed.

All string literals (sentence/word etc.) in Java programs, for example "xyz", are implemented as instances of this class.

Java String Class Syntax Methods Examples

Syntax

Here are ways and syntax of creating a string in JAVA.

String literal

String str = “EyeHunts”;

Using a new keyword

String s = new String (“EyeHunts”);

Java String Example 

Here is how to work with string data type in java and printing the same in the console(terminal).

public class Main {

    public static void main(String[] args) {

        String str = "Hello String";
        System.out.println(str);
    }
}

Output: Hello String

Strings Methods

Let’s check some most using method with examples, to get a list of complete methods of the string does follow post end link of the official Java document website.

charAt (int index) – Returns the character value of the passed index value in integer.

Space in a sentence also counts as an index.

String str = "Hello String";
System.out.println(str.charAt(6));

Output: S

length () – Method Returns the length of this string.

Space in a sentence also calculated and return int value.

String str = "Hello String";
System.out.println(str.length());

Output: 12

replace (char oldChar, char newChar) – Replacing all occurrences of oldChar in this string with newCharand return the result in a string.

replace only char not a complete word, use replaceAll for substring changes.

String str = "Hello";
System.out.println(str.replace("H", "B"));

Output: Bello

replaceAll (String regex, String replacement) – Replaces each substring of this string that matches with given replacement.

String str = "Hello String";
System.out.println(str.replaceAll("String", "EyeHunts"));

Output: Hello EyeHunts

Q: Why is String immutable in Java?

Answer: A string is immutable because of reasons, see the some of mentioned below:-

String Constant Pool – If the string is mutable, changing the string with one reference will lead to the wrong value for the other references.

Security: In a network, database connection parameters like username, password etc represented as String, So If it were mutable, these parameters could be easily changed.

Synchronization and concurrency: making String immutable automatically makes them thread safe thereby solving the synchronization issues.

Thread Safe: In programme single string instance can be shared across different threads so it safe if string is immutable.

Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/String.html (Official document)

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.5 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples t are in Java 11, so it may change its different from Java 9 or 10 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading