Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on Strings in Java Overview
0
(0)

In this article we will learn how to declare and use strings in our Java programs with appropriate sample code examples.

 

Like in any programming language, a string is a collection of characters. Instead of maintaining strings as character arrays (for example in C language), Java provides in-built support for working with strings through three pre-defined classes: String, StringBuffer and StringBuilder. All these three classes implements the CharSequence interface and are a part of java.lang package. So, there is no need to import any of these three classes as they are already available in all Java programs.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


First, let’s look at creating strings using these three classes and later we will learn about each class in detail in future articles.

 

String Class Constructors:

Let’s start with the String class. To enable a programmer for creating strings in different ways, the String class provides several constructors. To create an empty string we can use the following constructor:

 

String s = new String();

 

In the above example String() constructor creates an empty string (a string with no characters).

 

To create a string from character arrays, we can use the following constructors:

 

String(char  chars[])

String(char chars[], int startIndex, int numChars)

 

An example for the above constructors is given below:

 

char chars[] = {‘s’, ‘u’, ‘r’, ‘y’, ‘a’, ‘t’, ‘e’, ‘j’, ‘a’};

String s1 = new String(chars);

String s2 = new String(chars, 5, 3);

 

The constructor String(chars) in the above example creates a string s1 whose content is “suryateja”. The constructor String(chars, 5, 3) creates a string s2 whose content is “tej” as the parameter 5 specifies the starting index in the character array and 3 specifies the number of characters from the starting index. Remember that the index in a string starts from 0 and not from 1.

 

To create a string from another string object, we can use the following constructor:

 

String(String obj)

 

An example for the above constructor is given below:

 

char c[] = {‘t’, ‘e’, ‘j’, ‘a’};

String s1 = new String(c);

String s2 = new String(s1);

 

In the above example, the constructor String(s1) creates a new string s2 with the content of the string s1. So, both strings s1 and s2 contains “teja” as content.

 

Remember that eventhough s1 and s2 contain the same content, they don’t refer to the same memory location. There will be two copies of the string in memory.

 

We can also create strings from existing StringBuffer objects using the following constructor:

 

String(StringBuffer  sbobj)

 

An example for above constructor is given below:

 

StringBuffer sbobj = new StringBuffer(“hai”);

String s = new String(sbobj);

 

We can also create strings from StringBuilder objects using the following constructor:

 

String(StringBuilder sbobj)

 

An example for above constructor is given below:

 

StringBuilder sbobj = new StringBuilder(“hai”);

String s = new String(sbobj);

 

There are few more constructors for creating strings. Refer Java manual for them. They are not covered in this article as they are not frequently used.

 

The most commonly used String constructor is Java programs is given below:

 

String s = new String(“Your string here”);

 

So far we have seen different ways for creating strings using String class constructors. Now, we will look at constructors of StringBuffer class.

 

StringBuffer Class Constructors:

Before learning about the constructors of StringBuffer class, let’s learn an interesting difference between the strings created using String class and StringBuffer class.

 

All the strings created using String class are immutable i.e. the contents of a string object cannot be modified once created. But the strings created using StringBuffer and StringBuilder classes are mutable.

 

To create an empty string using StringBuffer class, use the following constructor:

 

StringBuffer sb = new StringBuffer();

 

Above StringBuffer() constructor reserves a space of 16 characters for sb. This is the default size of the buffer for strings created using this constructor.

 

To explicitly specify the buffer size, use the following StringBuffer constructor:

 

StringBuffer sb = new StringBuffer(20);

Now the capacity of the string sb is 20.

 

To create a StringBuffer object from a String object use the following constructor:

 

String s = new String(“teja”);

StringBuffer sb = new StringBuffer(s);

 

In the above example, sb contains the string “teja” plus an additional reserved space for 16 more characters.

 

To create a StringBuffer object from a sequence of characters use the following constructor:

 

StringBuffer(CharSequence chars)

 

StringBuilder’s constructors are same as that of StringBuffer’s constructors. So, there is no need to discuss them again.

 

String vs StringBuffer vs StringBuilder:

Now you might be wondering why there are three classes for creating strings in Java. Each class has its own merits and demerits. Let’s look at this in detail.

 

As we already know, strings created using String class are immutable. Although this looks like a disadvantage, several methods available in String class allows us to overcome that disadvantage. Also strings created using String class gives greater performance.

 

Strings created using StringBuffer class are mutable. This benefit has a downside in terms of lesser performance (for maintaining relocatable strings). One more important benefit of StringBuffer class is, strings created using this class are thread safe.

 

Strings created using StringBuilder class are also mutable and faster than strings created using StringBuffer class as no synchronization mechanisms are maintained for StringBuilder class, i.e. strings created using StringBuilder class are not thread safe.

 

I am finally going to end this article with suggesting when to select each of these three classes for creating strings:

  • If your program requires greater performance and you don’t care about mutability of strings, use String class.
  • If your program should be thread safe, use StringBuffer class.
  • If your program needs mutable strings and greater performance, use StringBuilder class.

 

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *