Java - String

A string in literal terms is a sequence of characters like the word “hello”. Hey, did you say characters, isn’t it a primitive data type in Java. Yes, so in technical terms, the basic Java String is basically an array of characters. In the java programming language, string is object.

 

Immutable String

Java String is a immutable object. For an immutable object you cannot modify any of its attribute’s values. Once you have created a java String object it cannot be modified to some other object or a different String. A reference to a java String instance is mutable. There are multiple ways to make an object immutable. Simple and straight forward way is to make all the attributes of that class as final. Java String has all attributes marked as final except hash field.

 

We all know java String is immutable but do we know why java String is immutable? Main reason behind it is for better performance. Creating a copy of existing java String is easier as there is no need to create a new instance but can be easily created by pointing to already existing String. This saves valuable primary memory.

 

String Syntax:

String as an array of characters like:


char[] charArray = {'V','I','S','I','O','N'};
String str = new String(charArray);

String in Java as:


String srt = new String("VISION");

 

Initialization of String:

JVM maintains a memory pool for String. When you create a String, first this memory pool is scanned. If the instance already exists then this new instance is mapped to the already existing instance. If not, a new java String instance is created in the memory pool.


String str1 = "VISION";
String srt2 = new String();
String str3 = new String("VISION");
String str4 = new String(char[]);
String str5 = new String(byte[]);
String str6 = new String(new StringBuffer());
String str7 = new String(new StringBuilder());

 

We have an empty constructor for String. It is odd, java String is immutable and you have an empty constructur which does nothing but create a empty String. I don’t see any use for this constructor, because after you create a String you cannot modify it.

 

Note: The String class is immutable; so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then you should use String Buffer & String Builder Classes.

 

Example:


public class Main{
	public static void main(String args[]){
		String str = new String("visions");
		System.out.println(str);
	}
}		

 

Output:

visions

 

String length:

String length() method returns number of characters contained in the String object.

 

Example:


public class Main{
	public static void main(String args[]){
		String str = new String("visions");
		System.out.println("String length : "+str.length());
	}
}		

 

Output:

String length : 7

 

Concatenating String:

Concatenating is joining of two or more string into one string.

We have two string str1=”visions” and str2=”developer”. If we add these two strings, we should be having a result as str3  =”visionsdeveloper”. String having two methods for concatenating string first is “+”(plus operators) and concat() method.

 

Example:


public class Main{
	public static void main(String args[]){
		String str1 = "visions";
		String str2 = "developer";
		String srt3 = str1+str2;
		System.out.println(str3);
		String str4 = str1.concat(str2);
		System.out.println(str4);
        }
}

 

Output:

visionsdeveloper

visionsdeveloper

 

Note: Don’t use “+” operator because it is not good for performance.