Basics. Part 4

A String is a special class that is defined in the package java.lang. A package is just a folder or directory structure. All Java programs have access to this package by default, so you don’t need to do anything special to use it.

A String can be created in two different ways.
1) The literal notation uses double quotes:

String s = “Hello”; 

2) By using the new keyword (this is a less common approach)

String s = new String(“Hello”); 

When creating Strings using the literal notation, that String is either matched when one already created or a new object is created in memory. When creating a String by using the new keyword, you’ll always create a new object in memory.

The String Pool is a special place in memory that holds String values. The String Pool manages all Strings created using the literal notation and aids in pointing new references to existing values. The String Pool is not used when the new keyword is used to create a String.

If Strings have the same value and they reference the same instance in memory then two Strings are said to be equivalent. For example, the following two Strings when created will be equal to one another.

String s = “Hello”; 
String t = “Hello”;
if (s == t)
System.out.println(“The strings are equivalent”);
else
System.out.println(“The strings are not equivalent”);

The above program would print “The strings are equivalent”. When you create a String by using the new keyword, then two Strings are NOT equivalent

String s = “Hello”; 
String t = new String(“Hello”);
if (s == t)
System.out.println(“The strings are equivalent”);
else
System.out.println(“The strings are not equivalent”);

In Java, both the String class and the value stored inside a String object are final, and thus immutable. The String class cannot be extended, and once instantiated, the value inside a String object cannot be changed. Every time you modify a String object in Java, a new String object is created.

String var1 = new String ("Hello ");
String var2 = new String ("World");
var1 = var1 + var2;
var2 = "Example";

Each line above creates a new String object in memory.

If you want to create a mutable, i.e. changeable string, you would use a StringBuilder or StringBuffer object. These objects allow the text value they contain to be changed at will, and they provide several utility methods for manipulating their contents in different ways.

However, consider a situation where two separate processes (called threads) are running at the same time, and both processes need to access the same text at the same time, and one process will change it. With a String object, this is not a problem — when one of the processes changes a String, it gets a new String object and the original remains unchanged. If the text value were mutable though, then the value returned to one process would depend on whether the other has edited it yet. This means that our editable text is not thread-safe.

You can use a StringBuffer whenever you need a mutable string object, and also need it to be thread-safe. A StringBuffer object will guarantee that only one thread can access it at a time, preventing confusion. This can slow performance

A StringBuilder is a wrapper for a mutable string, but it is not thread-safe. You use a StringBuilder when you are not using threads, or are performing thread control safely somewhere else.

SOFTWARE ENGINEER — WEB DEVELOPER WITH HEART FOR FRONT-END, MINIMALISM AND RASMENTALISM.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store