Introduction to Java: Working with String

The String is one of the most used classes in Java programming language.

In this tutorial, we’ll learn how to work with String in Java.

1. The String Class

String class is a class provided by the Java API. It’s placed inside java.lang package. This is a special package that doesn’t require importing. All classes from the package are automatically imported into our code.

The String consists of a set of character data types. We can assess each character using indexes. Indexes start with 0 and increment by one for each next character.

2. Create String Objects

Now, let’s see how to create String objects. When it comes to String, we can create new objects in two different ways.

The first approach is to create it using the keyword new followed by the constructor call. For instance, if we’d like to create a String object with the value “Lion“, we’d do that in the following way:

String animal = new String(“Lion”);

The second approach is to assign the value directly:

String animal = “Lion”;

Regardless of which approach we use, both will create a String object. We could access this object through the reference animal.

Furthermore, we should be aware of one key difference between the two approaches – Java stores the object in different locations inside the memory.

3. String Concatenation

We can concatenate two String values using the “+” operator. This operation is called String concatenation.

Now, let’s define two String values and concatenate them:

String a = "bee";
String b = "nary";
String c = a + b; // beenary

Here, we concatenated values stored in the variables a and b and defined a new variable c.

However, we should be careful when concatenating String with numeric values. We should follow the rules:

  1. If both values are numeric, the operator “+” means addition
  2. If one value is of type String, the operator “+” means concatenation
  3. The expression evaluates from left to right.

Now, let’s see some examples:

System.out.println(1 + 2); // 3
System.out.println("a" + "b"); // ab
System.out.println(1 + 2 + "c"); // 3c
System.out.println(1 + 8 + "5"); // 95

Lastly, let’s see a little complicated example:

int three = 3;
String four = "4";
System.out.println(1 + 2 + three + four);

The result of the snippet above will be 64. Now, let’s see why.

The expression is evaluated from left to right. In the beginning, we have two numerical values, 1 and 2, so + means additions. The result will be 3.

Next, variable three is numerical as well. Once again we should apply addition.

Finally, variable four is String. If one of the operands is String (regardless if it’s on the left or right side of the expression), the + means concatenation. Using concatenation on the values 6 and “4” we got 64 as a result.

4. Immutability

String objects are immutable. Once we create an object with a String type, we cannot modify its value.

However, we can assign the value to a new variable, but we can’t change the current object.

Let’s consider the following example:

String s1 = " abc";

The trim() method should remove whitespace from the beginning and end while the toUpperCase() method converts the given value to an uppercased value. However, the after we call these methods, the s1 variable will still have the “ abc” value.

Let’s check another example:

String s1 = "   abc";
String s2 = s1.trim().toUpperCase();

Here, the value of s2 will be “ABC”. We called the trim() and toUpperCase() methods on the s1 variable but we saved the result into the s2 variable.

It’s important to note that the value of the s1 variable stayed the same (“   abc“).

5. String Pool

Finally, let’s see how Java stores String objects in memory.

As we mentioned, Java stores object in memory. The way we create the String object will determine where Java will store it.

It can either store it on the heap or add value in the String Pool.

To save space in memory, Java will put objects created with direct assignation to the String Pool:

String animalType = “Lion”;

The value of the animalType variable will be stored in the String Pool.

Additionally, if we create a new String object containing the same value, Java would use the existing value from the String Pool. In other words, it wouldn’t create a new String object:

String animalType1 = “Lion”;
String animalType2 = “Lion”;

On the other hand, if we create a String object as we’d create any other object, Java will store it on the heap:

String animalType = new String(“Lion”);

Objects will be removed from the memory by the Garbage Collector.

Furthermore, every time we create a new String object in this way, Java will create a new object regardless of its value. So, we may end up having multiple objects with the same value. This could cause unnecessary usage of memory.

To reduce memory usage to a minimum, we should consider creating new String objects using the direct assignment.

6. Key Takeaways

In this tutorial, we learned about the String class in Java.

To sum up, it is recommended to use a direct assignment approach when creating new String instances. Additionally, values are stored in the stack part of the memory. Lastly, Strings are immutable – meaning we cannot change their values.

If you want to find out more about the String, check out my String methods article.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x