Strings in Java

Generally a string is defined as a sequence of characters. In Java, strings are implemented as an object not as a primitive type. This is different from the manner in which strings are implemented in C language. In C strings are arrays of chars.(i.e array of primitive type.)

For example( in C)

char s[]="hello";

Here "hello" is an array of primitive type - chars . It is NOT an object. But in Java strings are different and is represented by the object of type java.lang.String class.

To understand strings in Java you have to understand two basic terms:

1)  String Literals
2)  Object of type java.lang.String class.

String Literals:

Any sequence of characters enclosed in double quotes that is appearing in your code is known as string literal. For example,

"abc"       //a string literal having value abc
""             //an empty string literal
" "          //a string literal containing space as its contents.
"\"""     // a string literal having value "(double quote). Here escape sequence ( \ ) is used.

All above are string literals.

Note:-Do not confuse with string and String .String (with S in caps) is a class in java.lang package while string (with s in lowecase) is a general term used for sequence of characters.

Object of type String:

In Java each string literal is represented by an object of type java.lang.String class. When your class or interface is loaded, a String object is created to represent that string literal.This object has the same value as the corresponding string literal.

For example in the following statement

String s="abc";       

"abc" is a string literal. When the class containing this line would be loaded, a String object with value "abc" is created and reference of that object is returned into variable s.

You can think in this way that every string literal has a backing String object and reference to this object is used in place of that literal.

Similarly in the statement,


a String object with value "Hello!" created. But we are not assigning its reference to anywhere.

Ways to create Strings in Java:

There are two ways to create Strings :

1) Using String literal.
2) Using new operator in one of construtors avalaible in String class.

Using String literal:

String can be created using string literal as:

String s="Hello!"; 

In above statement a String object s is created with value Hello! using string literal Hello!


String s1="";  // an empty String object created

String s1=" ";  // a String object created containing space

String s2="\""; // a String object with value double quote("), using escape sequence

String s3="\\";  // a String object with value  backslash(\), using escape sequence

System.out.println("Hello!"); // a String  object will be created with value Hello!

Using new operator:

String can also be created by using new operator in one of the constructors available in String class as:

String s= new String("Hello");

In above statement you are creating a new String object with contents Hello. Reference to this newly created String object is assigned to variable s.

When we create string using new as:

String s= new String("Hello");     // two String objects will be created

Here two String objects will be created - one for String literal Hello enclosed in double quotes (as described in previous section) and one by new operator whose referenced will be assigned to variable s.

Creating strings by using string literals is always preferred and considered better way to create strings than using new operator because this method allows the sharing of string objects with same value through String pool and prevents creation of new String objects in our code in order to save memory.

I would like to know your comments and if you liked the article then please share it on social networking buttons.

No comments:

Post a Comment