String Class in Java


Hi guys! After a long time..Huh :D Apart from my web articles, I thought to bring you more articles on Java. Actually Java is the most prettiest and versatile language for me. There's nothing that Java can not do in programming. Today I'm going to give you a comprehensive guide for Java String class. This will consists of information about StringBuffer and StringBuilder classes also. Hope you will get a good understanding about strings, at the end of this article. Let's go!!!

What is a String in Java? - It's a unique char sequence

What are the String classes in Java?

  • String
  • StringBuilder
  • StringBuffer


String Class


Differences between String classes

String - Immutable, Inefficient
StringBuilder and StringBuffer - Mutable, Relatively efficient

String class is immutable. That means strings can not be modified once it is initialized.
StringBuilder and StringBuffer classes are mutable. That means strings can be modified once it is initialized.

Declaration of Strings

There are two ways to declare a string in Java. Let's look into these two methods separately.

1. Use of String Literals
2. Use of new Key Word

What is the difference?

When we create without new keyword, it is created in a place called String Pool which is located in Java Heap Space. These are called String Literals. But If we use new keyword, those strings are stored in the Heap Space directly as objects.

String str1 = "Salitha";
String str2 = "Salitha";
String str3 = new String("Salitha");

When we consider the word "Salitha", it is always same if we assign it into 1000 variables. The above code contains 3 string variables. Strings referenced as str1, str2 and str3 are assigned to "Salitha". Since the value is same, both str1 and str2 references variables are pointing to the same char sequence. Both are created in the String Pool. But when it comes to str3, it's created as an object in the Heap.

The above image shows how 3 variables are stored in memory. If we check the equality of strings with Java, we can implement it like this..Here Java checks for variable references difference with the sequence of chars.


But you can see the content is equal in 4 variables.. So how to check the content of a string? We can do it. We have to use equals() method available in String class. If it finds that there is the same char sequence in the content, without looking at the place where it's located, it returns true!


If Strings are IMMUTABLE, what do methods in String class do? Don't they modify the string?

There is a list of methods that can handle strings in String class. Among them, most common one is concat() which helps to join strings. If strings immutable, how is this possible?
Let me explain!
Actually what these methods do is, only take a copy of the original string and perform the operations on it. Always the original is kept safe. We can check it like this using the below code snippet..

String s1 = "Hello ";
String s2 = "World";
String s3 = s1.concat(s2);
System.out.println(s3); // HelloWorld
System.out.println("s1: "+s1+" "+"s2: "+s2); // s1: Hello S2: World
s1 = s1.concat(s2);
System.out.println("s1: "+s1+" "+"s2: "+s2); // s1: HelloWorld s2: World

As soon as we assign the result into a variable(like in 6th code line), then only the variable value is changed. Whenever we use the string in a method like concat(), always copy of the string is passed into it..Not the original string..!!!

Some common methods in String class is listed below..


String str5 = "1@2.3";
System.out.println(str1.length()); // returns length of string
String[] splits = str5.split("."); // divide string by "."(dots) -> creates String array
System.out.println(splits);
System.out.println(str1.concat(str2)); // join two strings
System.out.println(str1.charAt(0)); // returns first char of string
System.out.println(str1.getBytes()); // convert string into bytes
System.out.println(str5.replace("@", ".")); // replace @ signs with dots in string
System.out.println(str1.substring(0, 2)); // returns the sub string indexed from 0 to 1


That's all about String class. Let's move on to the StringBuilder and StringBuffer classes..


StringBuilder Class

1. StringBuilder is MUTABLE.
2. Not Synchronized.
    - Not Thread Safe (Two threads can access the same StringBuilder object.)
3. More efficient.

How to declare?

StringBuilder sb = new StringBuilder("Hello ");
System.out.println(sb); // Hello


StringBuffer Class

1. StringBuffer is MUTABLE.
2. Synchronized.
    - Thread Safe (Two threads can not access the same StringBuffer object.)
3. More efficient.

How to declare?

StringBuffer bf = new StringBuffer("Hello");
System.out.println(bf); // Hello


Both these classes contains almost same set of methods..You can try out the methods shown in the below code snippet.


StringBuilder sb = new StringBuilder("Hello ");
sb.append("Good Morning!"); // append another string in to the end
sb.insert(0, "My"); // add char set into specified index
sb.substring(0, 2); // returns the sub string indexed from 0 to 1
sb.replace(0, 4, "Nice"); // replace the char set from 0 to 3


That's all guys! I hope you got a good understanding about String classes in Java by reading this article..That was my aim..This String class is one of the most used classes when we code a logic or do programming. So, it's very important to know these classes well....

Now i'll wrap up for today..

Good Bye guys!!!





Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "String Class in Java"