Explain different types of String Constructors with examples.

5.A] Explain different types of String Constructors with examples.

Answer:-

Java provides several constructors to create String objects. Each constructor serves a specific purpose and allows the creation of strings in different ways.

1. Default Constructor

  • Creates an empty String object.
  • Syntax: String str = new String();
  • Example:
  String str = new String();
  System.out.println("String: '" + str + "'"); // Output: String: ''

2. String Literal Constructor

  • Directly assigns a string literal to a String variable. Although it’s not a constructor, it’s the most common way to create a String.
  • Syntax: String str = "Hello";
  • Example:
  String str = "Hello";
  System.out.println(str); // Output: Hello

3. Constructor with String Argument

  • Creates a new String object that is a copy of the argument string.
  • Syntax: String str = new String(String anotherString);
  • Example:
  String original = "Hello";
  String copy = new String(original);
  System.out.println(copy); // Output: Hello

4. Constructor with char[] Array

  • Creates a String object from a character array.
  • Syntax: String str = new String(char[] charArray);
  • Example:
  char[] charArray = {'H', 'e', 'l', 'l', 'o'};
  String str = new String(charArray);
  System.out.println(str); // Output: Hello

5. Constructor with char[] Array and Offset

  • Creates a String object from a portion of a character array, specified by an offset and a count.
  • Syntax: String str = new String(char[] charArray, int offset, int count);
  • Example:
  char[] charArray = {'H', 'e', 'l', 'l', 'o'};
  String str = new String(charArray, 1, 3);
  System.out.println(str); // Output: ell

6. Constructor with byte[] Array

  • Creates a String object from a byte array. Each byte is interpreted as a character.
  • Syntax: String str = new String(byte[] byteArray);
  • Example:
  byte[] byteArray = {72, 101, 108, 108, 111};
  String str = new String(byteArray);
  System.out.println(str); // Output: Hello

7. Constructor with byte[] Array and Charset

  • Creates a String object from a byte array using a specified charset.
  • Syntax: String str = new String(byte[] byteArray, Charset charset);
  • Example:
  byte[] byteArray = {72, 101, 108, 108, 111};
  String str = new String(byteArray, Charset.forName("UTF-8"));
  System.out.println(str); // Output: Hello

8. Constructor with int[] Array

  • Creates a String object from an array of Unicode code points.
  • Syntax: String str = new String(int[] codePoints, int offset, int count);
  • Example:
  int[] codePoints = {72, 101, 108, 108, 111};
  String str = new String(codePoints, 0, 5);
  System.out.println(str); // Output: Hello

Summary

  • Default Constructor: Creates an empty string.
  • String Literal: Most common, directly assigns a string.
  • String Argument: Copies an existing string.
  • char[] Array: Converts a character array to a string.
  • char[] Array with Offset: Creates a string from a portion of a character array.
  • byte[] Array: Converts a byte array to a string.
  • byte[] Array with Charset: Converts a byte array to a string using a specific charset.
  • int[] Array: Converts Unicode code points to a string.

Leave a Reply

Your email address will not be published. Required fields are marked *