String Class in java

A String class creates an immutable object which contains a sequence of characters, that means whenever any object is created using String class value of that object cannot be modified.

java.lang.String class provides many methods to manipulate strings such as length() , split(), compareTo(), substring() and many more.


What does immutability of object mean?

Immutable object (unchangeable object) is an object whose state cannot be modified after it is created.
Once a String object is created, its content cannot be altered . If you need to change the string a new object is created and returned to us.

How do we create String object?

There are two ways to create string object:
1) using String Literal- we can assign any string literal directly to String type variable.
2) using new keyword -we can use any constructor of java.lang.String class to create object.

String Literals

Literals : Representation fixed data type. Fixed value which can be directly assign to variable.


String Literals : Fixed String value which can be directly assign to String type variable.

for example:
String x="John"
"John", is String Literal because "John" is fixed value we are assigning to String type variable x.
creating string object using string literal-


String x="Java tutorial";
String y="[email protected]";

/*program to create the string using
string literal
*/
class Test
{
	public static void main(String[] args) {
		String x="Testing";
		String y="Hello Java";
		String z="God Job";
		System.out.println(x);
		System.out.println(y);
		System.out.println(z);
	}

}
Testing
Hello Java
God Job

Creating string by using 'new' keyword

We can use any constructor of java.lang.String class to create the object of String class. On this object, we can call any method of String class to manipulate the content of String.
example:

/*program to create the string 
by using 'String class constructor'
*/
class Test
{
	public static void main(String[] args) {
		//creating the object
		String x;
		x=new String("MyName is Durgesh");
		String y=new String("Hello Ashu");
		String z=new String("Java is multi-threaded ");
		System.out.println(x);
		System.out.println(y);
		System.out.println(z);
		//now printing the length of string
		int length=x.length();
		System.out.println(x +"\t:"+length);
	}

}
MyName is Durgesh
Hello Ashu
Java is multi-threaded
MyName is Durgesh       :17

String class-Constructors

Following constructors of String class can be used to create String object-

  public String() :- create empty string

  public String(String content) :- create string with content

  public String(char ch[]) :- it takes char array and convert to string

  public String(byte b[]) :- it takes byte array and convert to string

  public String(StringBuffer b) :- create a new string that contains the sequence of characters currently contained in the string buffer argument.

  public String(StringBuilder b) :- create a new string that contains the sequence of characters currently contained in the StringBuilder argument.

/*using constructor of String class
*/
class Test
{
	public static void main(String[] args) {
		//creating the object
		String x=new String();
		System.out.println(x);

		String y=new String("I am good");
		System.out.println(y);

		char c[]={'N','I','C','E'};
		String myCharString=new String(c);
		//we can also use the sub array like this
		//public String(char[],int startpositon,int numberOfChar)
		String myCharString1=new String(c,0,3);
		System.out.println(myCharString);
		System.out.println(myCharString1);


		byte b[]={97,98,99,100};
		String myByteString=new String(b);
		System.out.println(myByteString);

		//if we have buffer object
		StringBuffer br=new StringBuffer("i am content of buffer");
		String str=new String(br);
		System.out.println(str);

		//if we have builder object
		StringBuilder bl=new StringBuilder("i am content of builder");
		String str1=new String(bl);
		System.out.println(str1);

	}	

}
I am good
NICE
NIC
abcd
i am content of buffer
i am content of builder