String Constructor

The String class supports several constructors.

String()
Creates empty string
String(byte[] bytes)
Creates a String from specified array of bytes using the platform's default charset.
String(byte[] bytes, Charset charset)
Creates a String by decoding the bytes using the specified charset.
String(byte[] bytes, int offset, int length)
Creates a String by decoding the subarray of bytes using the platform's default charset.
String(byte[] bytes, int offset, int length, Charset charset)
Creates a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] bytes, int offset, int length, String charsetName)
Creates a String by decoding the subarray of bytes using the specified charset.
String(byte[] bytes, String charsetName)
Creates a String by decoding the array of bytes using the specified charset.
String(char[] value)
Creates a String from the character array argument.
String(char[] value, int offset, int count)
Creates a new String from a subarray of char array argument.
String(int[] codePoints, int offset, int count)
Creates a new String that contains characters from a subarray of the Unicode code point array argument.
String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
String(StringBuffer buffer)
Creates a new string that contains the sequence of characters currently contained in the string buffer argument.
String(StringBuilder builder)
Creates a new string that contains the sequence of characters currently contained in the string builder argument.

To create an empty String, you call the default constructor.

String s = new String();

will create an instance of String with no characters in it.


public class Main {
  public static void main(String args[]) {
    char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
    String s = new String("hello");

    // use String constructors
    String s1 = new String();
    String s2 = new String(s);
    String s3 = new String(charArray);
    String s4 = new String(charArray, 6, 3);

    System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
  }
}
new String(byte[] bytes)

Initialize a string with given a byte array.


public class Main {
  public static void main(String args[]) {
    byte ascii[] = { 65, 66, 67, 68, 69, 70 };

    String s1 = new String(ascii);
    System.out.println(s1);

    String s2 = new String(ascii, 2, 3);
    System.out.println(s2);
  }
}
new String(byte[] ascii, int hibyte)

public class MainClass {

  static byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
  static byte b[] = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };

  public static void main(String args[]) {

  System.out.println("a = " + new String(a, 0));//a = ABCDEFGHIJ
  }

}

The following code creates a string from sub byte array.

new String(byte[] bytes, int offset, int length)

public class Main {
  public static void main(String args[]) {
    byte ascii[] = { 65, 66, 67, 68, 69, 70 };

    String s1 = new String(ascii);
    System.out.println(s1);

    String s2 = new String(ascii, 2, 3);
    System.out.println(s2);
  }
}

To create a String initialized by an array of characters:

new String(char[] value)

public class Main {
  public static void main(String args[]) {
    char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
    String s = new String("hello");

    // use String constructors
    String s1 = new String();
    String s2 = new String(s);
    String s3 = new String(charArray);
    String s4 = new String(charArray, 6, 3);

    System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
  }
}

Creating string from part of a char array

new String(char[] value, int offset, int count)

public class Main {
  public static void main(String args[]) {
    char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
    String s = new String("hello");

    // use String constructors
    String s1 = new String();
    String s2 = new String(s);
    String s3 = new String(charArray);
    String s4 = new String(charArray, 6, 3);

    System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
  }
}

Construct a String object that contains the same character sequence as another String object using this constructor:

new String(String original)

public class Main {
  public static void main(String args[]) {
    char charArray[] = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' };
    String s = new String("hello");

    // use String constructors
    String s1 = new String();
    String s2 = new String(s);
    String s3 = new String(charArray);
    String s4 = new String(charArray, 6, 3);

    System.out.printf("s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n", s1, s2, s3, s4);
  }
}
Home 
  Java Book 
    Essential Classes  

String:
  1. String type and Literals
  2. String Concatenation
  3. String.CASE_INSENSITIVE_ORDER
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String