Strings in Java Explained (with Examples)

Strings in Java Explained (with  Examples)

In this article, we will be looking into a fundamental topic which is strings in Java, I will try to cover everything that needs to work with strings in Java, so be ready to learn something new, and I hope this article will going to be a good guide for you to understand everything about strings in Java. If you are a beginner and want to learn Java or any other programming language, I highly suggest you look into codedamn interactive tutorials.


String in Java is a fundamental topic, Which is used for working with text, and characters the String is also an Object which refers to memory. It can be created with literals and the new keyword. A string can be declared as shown in the example below.

String name = "Amol"; System.out.println(name); // it will print Amol.
Code language: Java (java)

Now Let’s take a deep look into Strings in Java.

What is string in Java

As with any other programming language Java also has a character sequence data type that is known as String, but a string in Java is class-based and can work as a data type too. I have shown in the introduction how we can initialise a string in Java with the code demonstration. As working with the textual content is a part of the programming, we eventually need a way of storing text into variables or any other type of data-storing containers, and the String in Java gives us that power of storing textual content.

String operations in Java

While working with Strings in Java, we often need to work with strings and modify them, Let’s look into some important operations string operations in Java.

Get length of a String

Getting a length of a string is pretty easy in Java because of the length(). What the length() does? It takes a String as a parameter and returns its length.

String demoText = "Im Demo Text"; System.out.println(demoText.length()); // Output 11
Code language: Java (java)

Join two Java Strings

The string method concat() which combines two strings and returns the combined string as a result, it appends the second string at the end of the first string. As shown in the example below.

String message = "Hello there,"; message = message.concat(" Amol"); System.out.println(message); // The output: Hello there, Amol;
Code language: Java (java)

Compare two Java Strings

Java Strings can be compared by using the method compareTo(), and what the method compareTo() does is it compares two strings and return the results.

String message = "Hello there,"; String message2 = "Hello there,"; System.out.println(message2.compareTo(message)); // output 0 both are similar
Code language: Java (java)

String objects: how do I create them?

String object can be created in two different ways, let’s have a good look at them.

String literal

A string object can be created in two ways in Java, The first one is by using string literal and the second one is by using the new keyword. The string object created with the string literal get stored in the string constal poll in the memory.

String message = "Hello there,"; System.out.println(message);
Code language: Java (java)

As shown in the example, we can create the string object with the help of the string literal. There is another way of creating a string object so let’s now look at it.

By new keyword

The new keyword works the same as the string literal the only difference is that it stores the string object directly inside the memory heap. As we saw that the string literal stores the string objects inside the string constant pole.

String name = new String("Amol"); System.out.println(name);
Code language: Java (java)

Create String using literals vs new keyword

Both literals and new keywords create strings the only difference between them comes with the new keyword, as it creates a new object in the heap memory. On other hand, the literals may point to the existing objects which are present in the string constant poll. The code example of creating strings with the help of literals and new keywords is already shown in the article.

Java String Example

As I cover already some string examples in this article, here is another example of Java Strings. Let’s suppose I created an information system In that system, I need to store the information of some users such as name, address, bio, etc. Here strings come into play because now I have to work with characters, so I’m going to use the string to store the information about them.

As shown in the example below.

String name = new String("Amol Shelke"); String address = new String("Nagpur, Maharashtra "); String bio = new String("I'm a software developer"); System.out.println(name + " lives in " + address + "and work as " + bio); // output: Amol Shelke lives in Nagpur, Maharashtra and work as I'm a software developer
Code language: Java (java)

Escape character in Java Strings

Escape characters help us to work with strings easily in Java, there are different types of Escape characters present in Java.

Escape characters in Java strings are useful to break the line or insert single or double quotes inside the string and many more made working with strings easily. Let’s have a closer look at them. An Escape character is a combination of two characters which starts with an \.

  • \n: creates a newLine in the string.
  • \t: creates a tab.
  • \’: add a single quote between the string.
  • \”: inserts double quotes in strings.
  • \\: It’s a backslash Escape character.

Java Strings are Immutable

Java Strings are immutable and cannot be changed because the strings are not only a data type in Java but also an object and as you know the object stores reference inside the memory, not the values. Let’s suppose you created a name string with the string literal, and then you assign that name to another variable what it will do, is that another variable will also refer to the name object which is stored in the memory. To remove this buggy thing developers removed the mutable concept from the Java environment.

Methods of Java String

There are different methods present on every Java String, which we can use to perform various operations on it, This article is an introduction to Java strings so I will not be going to be in-depth if you want to know more about them you can look into the docs.

  • String.length(): Returns the length of the string on which the method is called.
  • String.indexOf(): Returns the index of the character specified in the method.
  • String.compareTo(): compares two strings and returns a number.
  • String.concat(): Concatenates two strings and returns the result.

There are dozens of string methods present in Java.

String Manipulation

String manipulation is nothing but manipulating a string, we can manipulate strings in Java with the help of different methods that Java strings contain. Let’s manipulate a string in Java with a small example.

Suppose we have a string and we want to change its one character, we can do that with the help of replace().

String name = new String("AMOL"); name = name.concat(" SHELKE"); name = name.replace('A', 'a'); System.out.println(name);
Code language: Java (java)

In the above example, I first added the last name to the string with the concat() and then use the replace() to replace a character of a string.

String Comparison

Like any other language, we cannot just compare one string with another. Different things come into play while comparing strings in Java, like references, values, strings, etc. The compareTo() compares two strings and returns the results based on that comparisons.

For similar cases.

String name = new String("AMOL"); String name2 = new String("AMOL"); System.out.println(name.compareTo(name2)); // output: 0
Code language: Java (java)

For Different cases.

String name = new String("CMOL"); String name2 = new String("AMOL"); System.out.println(name.compareTo(name2)); // Output: 2, becuase C > A by 2.
Code language: Java (java)

Searching in a String

To search for a specific character present inside a string there is a method on every string in Java, i.e. indexOf() what this method does is it returns an index of the character which we want to search. As shown in the example below.

Let’s suppose we have a string and we want to find a character, we will just invoke the indexOf() on the string, it will return the expected result.

String name = new String("AMOL"); System.out.println(name.indexOf('M')); // output will be 1 cuase it start counts from 0
Code language: Java (java)


And that’s the end for this article, I hope this article has everything that a Java String guide should have. If you found this article helpful, I appreciate that, I will see you in another article post. I have written various articles on codedamn news so you can look at them too.

Till then, happy coding😊💕.


What is string data type?

A string is a data type that is used for working with alphabetical characters in Java. As we write text in the English language, the string data type is used for writing text in programming, the example is shown below.

String name = "Amol"; System.out.println(name); // it will print Amol.
Code language: Java (java)

What is the string class in Java?

A string class in Java has every method which we can use to manipulate and do some operations on every string in Java.

Why do we use string in Java?

we use a string in Java for storing user inputs, text that users are inserting and many more.

How to declare string in Java?

A Java String can be declared with the help of String literals or the new keyword.

What is the Java syntax for declaring strings?

String in Java can be declared as shown below in the example.

String name = new String("Amol"); System.out.println(name);
Code language: Java (java)

Sharing is caring

Did you like what Amol Shelke wrote? Thank them for their work by sharing it on social media.


No comments so far