String constructor in java

In java, String is the most commonly used data type. It is used in almost every program. We have already discussed many things about the String in java. Like How to create String, what is java substring, mutable string, immutable string?
In this post, we will see the java string constructor class. We will not discuss each and every constructor, we will discuss only some important constructors.  Let’s discuss the string constructor with examples.

string constructor in java

1. String()

2. String(String string)

3. String(char[] value)

4. String(StringBuffer buffer)

5. String(StringBuilder builder)

string constructor in java

1. String()

In Java, the String class has a constructor that doesn’t take any parameter and creates an empty string in java. When we use this constructor the JVM creates an object in heap memory and string constant pool. If you are not familiar with memory storage of string please read it from here. After the creation of an empty string, you can modify it but it will create a new object in memory.

public class StringExample
{
	public static void main(String[] args) 
	{
		String str = new String();
		System.out.println("Is String empty: "+str.isEmpty());
		
		str = str + "Hi";
		System.out.println("Is String empty: "+str.isEmpty());
		System.out.println("String value: "+str);
	}
}

Output: Is String empty: true
Is String empty: false
String value: Hi

2. String(String string)

Java provides another constructor that takes one parameter of string type and returns the string value. We can create a string by use of this constructor we just need to pass the value to the constructor. We can pass a group of characters enclosed in “ ”(inverted commas) or pass any variable that holds the string.

public class StringExample
{
	public static void main(String[] args) 
	{
		// Passing a group of characters 
		String str1 = new String("Hi JavaGoal!!!!!");
		
		String s = "Hello!!!!!";
		// Passing a string variable 
		String str2 = new String(s);
		
		//Creation of string by numeric data
		String str3 = new String("123");
		
		System.out.println("String value of str1: " +str1);
		System.out.println("String value of str2: " +str2);
		System.out.println("String value of str3: " +str3);
	}
}

Output: String value of str1: Hi JavaGoal!!!!!
String value of str2: Hello!!!!!
String value of str3: 123

3. String(char value[])

In java String class provides a constructer that accepts a char array and returns the string. It will take only one parameter of a char array. Let’s how to convert a char array to string java.

public class StringExample
{
	public static void main(String[] args) 
	{
		char[] charArray = {'J', 'a', 'v', 'a', 'G', 'o', 'a', 'l'};
		String str = new String(charArray);
		System.out.println("Value of String: "+ str);
	}
}

Output: Value of String: JavaGoal

4. String(StringBuffer buffer)

This constructor takes one parameter that should be the type of StringBuffer and it returns a string. To convert a Stringbuffer to astring we can use this constructor. Let’s see how to use the StringBuffer in java and convert it to string.

public class StringExample
{
	public static void main(String[] args) 
	{
		StringBuffer buffer = new StringBuffer("Hello");
		System.out.println("String from String buffer: "+ buffer);
		
		buffer.append(" JavaGoal");
		
		String str = new String(buffer);
		
		System.out.println("Value from str: "+ str);
		
	}
}

Output: String from String buffer: Hello
Value from str: Hello JavaGoal

5. String(StringBuilder builder)

This constructor takes one parameter that should be the type of StringBuilder and it returns a string. To convert a StringBuilder to string we can use this constructor. Let’s see how to use the java StringBuilder example:

public class StringExample
{
	public static void main(String[] args) 
	{
		StringBuilder builder = new StringBuilder("Hi");
		System.out.println("String from String builder: "+ builder);
		
		builder.append(" JavaGoal");
		
		String str = new String(builder);
		
		System.out.println("Value from str: "+ str);
		
	}
}

Output: String from String builder: Hi
Value from str: Hi JavaGoal

2 thoughts on “String constructor in java”

Leave a Comment