Introduction to String Class

Last revised On: 15/05/2017 Author Amit Gupta

The String class is one of the most widely used class in Java. String represents character sequence. For example:- "Java" is a string which has five characters. All the string literals are instance of String class.

There are two ways to create String object:-

By string literal

For example:- 

String str1 = "Modern Pathshala";
String str3 = "Modern Pathshala";

When we create a string object using string literal, that string literal is added to the string pool, if it is not present there already and later if we create another object with same string using literal, then a reference of the string literal already present in string pool is returned as shown in below image - String Java

By new keyword

For example:-

String str2 = new String("Modern Pathshala");

When we create any string using new operator then the object is created in heap memory. The above diagram represents above strings created in memory.

Note:-

  • A java.lang.String class is final which implies no class and extend it.
  • The String class is immutable, so once it is created you cannot change that object. If there is a necessity to make a lot of modifications to Strings of characters, then you should use StringBuffer or StringBuilder Classes. 

In this chapter, we learnt about String class and how to create String in Java. In our next chapter we will understand various methods of String class and their usage in programming.

Author
Author: Amit Gupta
Published On: 18/05/2016
Last revised On: 15/05/2017

Share this chapter
Comments
comments powered by Disqus

Navigation

Social Media