Java String Basics

In this post we will introduce you to Java String so that you can understand some of the basics such as, how to create a string, how to compare two strings, join strings and how to split a string.

String is a sequence of characters, such as “Coding with Cake”. Strings in Java are constants meaning their values cannot be changed once created i.e. immutable. Because String objects are immutable they are thread safe and can be shared. Java String is a fundamental class to java development it is used in almost every development project so learning this will be very useful.

Creating a Java String:

To create a String in java you can do one of the following:

for example, using string literals

alternatively for example, using the new operator

However, there is a slight difference between the two examples above. Using string literals will only create one String object, whereas using the new operator will create two distinct objects. The reason for this is because the JVM manages a pool of string literals, hence the JVM will only create a string if it is not in its pool of strings. On the other hand, using the new operator forces the JVM to create a new String object (which is not apart of the string pool).

Comparing two Strings using the equals method:

In order to check if two strings are equal you should use the equals() method. This is because it will check the contents of the string objects, while using the == operator will check if they are the same object (not checking the string values).

Output:

Concatenating two string objects using the concat() method:

In order to concatenate (join) strings, you can use the ‘+’ operator, concat() or even the StringBuilder class. Strings are immutable objects, meaning once you have created the object you cannot change it. So if you are creating a very long string over a loop for example. Then it is more efficient to use StringBuilder then using the contact() method. Below are some examples to illustrate this

Output:

Splitting a string using the split() method:

Below are some examples to illustrate how to spilt a string

Output:

The String class has many other useful methods such as:

  1. length(): returns the length of the string, useful when a string needs to be a minimum or not exceeding a maximum length.
  2. equalsIgnoreCase(): Compares this string to another string ignoring case.
  3. startsWith(): returns true if the string starts with the specified prefix, useful when for example finding a filename with a prefix.

In conclusion, the Java String class has a lot of useful methods which we have only covered a handful in this post. In addition I would recommend that you visit the API documentation so that you can learn more about the String class which can be found here. I hope you found this post useful, so that you become more familiar with the Java language.

Similar posts:

  1. How to convert a byte array to a string
  2. Convert string to an integer
  3. How to convert an InputStream to a String

Leave a Reply