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

Frequently Asked Questions about String constructors in Java

What is the difference between creating a String with a constructor and using a string literal?

Using a string literal (for example String s = "Hello";) reuses an object from the string constant pool, while using a constructor such as new String("Hello") creates a new object in heap memory. The article shows the constructor approach in the String(String string) section.

How do I convert a char array to a String in Java?

Pass the char array to the String(char[] value) constructor, as demonstrated in the String(char value[]) section of this article. The constructor copies the characters into a new String.

Can I create a String from a StringBuffer or StringBuilder?

Yes. The String(StringBuffer buffer) and String(StringBuilder builder) constructors accept those mutable types and return a String, as shown in sections 4 and 5 above.

What does the no-argument String() constructor do?

It creates an empty String. The article notes that the JVM creates the object in heap memory and the string constant pool, and that any later modification creates a new object because String is immutable.

Are there other String constructors besides the five listed here?

The article states that it discusses only some important constructors and does not cover each and every constructor. The five covered are String(), String(String), String(char[]), String(StringBuffer), and String(StringBuilder).

2 thoughts on “String constructor in java”

  1. I would like to add to that statement,but I fear your wrath Eddie, lol! Permission to speak please ; )

    Reply
    • Yes please, You can add

      Reply

Leave a Comment