Java StringsLast Updated : 10 Jan 2026 In Java, string is basically an object that represents sequence of char values. An array of characters works as a string in Java. For example: is same as: Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. The java.lang.String class implements Serializable, Comparable and CharSequence interfaces. ![]() CharSequence InterfaceThe CharSequence interface is used to represent the sequence of characters. String, StringBuffer and StringBuilder classes implement it. It means, we can create strings in Java by using these three classes. ![]() The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes. We will discuss immutable string later. Let's first understand what String in Java is and how to create the String object. What is String in Java?Generally, a string is a sequence of characters. However, a string in Java is an object that represents a sequence of characters, and the java.lang.String class is used to create string objects. Java Strings are immutable that means their values cannot be changed once created. There are two ways to create String object:
Here, we are going to discuss these objects one by one. 1. Creating String Using String LiteralJava String literal is created by using double quotes. For Example: Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. For example: ![]() In the above example, only one object will be created. Firstly, JVM will not find any string object with the value "Welcome" in string constant pool that is why it will create a new object. After that it will find the string with the value "Welcome" in the pool, it will not create a new object but will return the reference to the same instance. ExampleThe following example demonstrate how to create string using string literals. Output: Welcome Welcome Note: String objects are stored in a special memory area known as the "string constant pool".2. Creating String Using new KeywordA string can be created using the new keyword, which explicitly creates a new String object in memory rather than using the string literal pool. For example: Here,
In such case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap (non-pool). Compile and RunOutput: Welcome Welcome Finding String LengthYou can find the number of characters in a string by calling the length() method on the string object. SyntaxIt has the following syntax: ExampleThe following example demonstrate how to find string length. Output: Length of string: 11 Finding a Character in a StringYou can find a character or substring in a string by using the indexOf() method that returns its position. SyntaxIt has the following syntax: ExampleThe following example demonstrate how to find a character in a string. Output: Index of 'o': 4 Comparing StringsYou can compare two strings using equals() for equality or compareTo() for lexicographical order. SyntaxIt has the following syntax: ExampleThe following example demonstrate how to compare strings. Output: Equal? false CompareTo result: -1 Concatenating StringsYou can join two or more strings into one using the concat() method or the + operator. SyntaxIt has the following syntax: ExampleThe following example demonstrate how to concatenate strings. Java String ImmutabilityIn Java, strings are immutable. It means that its value cannot be changed once a String object is created. If any operation appears to modify a String, what happens is the creation of a new String object. The original string remains unchanged. This immutable characteristic of strings in Java has several implications for performance, security, and functionality. ExampleThe following example demonstrate the string immutability. Output: Original string: Java After modification, original string: Java Modified string: Java Programming After calling toUpperCase on original string: Java Original string in uppercase: JAVA Memory Allotment of StringIn Java, memory allocation for strings is influenced by immutability and the string pool. String Literal StorageWhen a string is created using a literal, Java first checks the string pool.
This mechanism helps save memory and improves performance, especially when the same strings are used multiple times in a program. Syntax It has the following syntax: new Keyword and String PoolStrings created with the new operator do not use the Pool by default. They are stored in the heap memory outside the Pool, which means each new operation results in a new object, even if it contains the same string data Syntax: It has the following syntax: InterningWe can manually add a string to the Pool or ensure it uses the Pool by calling the intern() method on a string object. If the Pool already contains an equal string, the string from the Pool is returned. Otherwise, the string is added to the Pool. Syntax: It has the following syntax: Example Demonstrating Memory Allotment of StringConsider the following example, demonstrating the memory allotment of string. Output: str1 == str2: true str3 == str4: false str1 == str5: true str1 == str6: true Construct String from a subset of the char arrayYou can create a string from a part of a character array using a special String constructor. You need to provide three things: the char array, the starting index, and the number of characters to include. SyntaxIt has the following syntax: Here,
Example: Constructing String from Subset of Char ArrayThe following example demonstrates creating (constructing) a string from the subset of a character array. Output: World Let us see another example of Java String which is created using char[] array. Example: Creating String Using Char[] ArrayThe following example demonstrates creating (constructing) a string from a character array (char[] Array). Output: java strings example The above code, converts a char array into a String object. And displays the String objects s1, s2, and s3 on console using println() method. String Class Characteristics and InterfacesJava String class is a part of the java.lang package which is used to create and manipulate strings. The String class provides many useful methods for operations like comparison, concatenation, and searching. The String class is one of the most fundamental types in Java that is designed to represent immutable sequences of characters. Here are the key characteristics and the interfaces it implements:
Next TopicWhy-string-is-immutable-in-java |
We request you to subscribe our newsletter for upcoming updates.