String Class

A string is a group of characters. String classes are used to represents these groups of characters. We can create an object of a string class without using new operator instead of a new operator we use double quotes (““ ). As soon as JVM encounters double quotes, it understands that it has to create an object of the string class with the content present in double quotes and for each and every individual character inside double quotes it assigns a unique index value starting from zero.

 

Example:

String s = “Welcome”;

Address of the object is assigned to the variable S. this object has a very near relation with arrays. Once a String object created, the data inside the object cannot be modified. This is the reason, why we say

that,

Strings are immutable. Different ways of creating String objects are shown below

String S1 = new String();

String S2 = new String(“

abc

”);

String S3 = “

xyz

”;

Example:

Consider the following two statements

A a1 = new A();

String s = “Welcome”;

Both the above statements create the objects of the respective classes.

Now, when we say System.out.

println

(a1); it prints the address of the objects,

where as

for System.out.println(s); it prints the Welcome.

Here the a1 prints the address and

s prints

the Welcome because these are the values of the respective objects.

The

println

mechanism, called in the above two cases are not similar, one

prinln

method is defined to accept the object of object class and its functionality is to print the address of the object. The second

println

method is defined to accept

object

of String class and its functionality is to print the contents of the object pointed by the address.

 

String Class Homework Help