Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – String Constructors

Since String object can be created using the new keyword, String class have several constructors.
Few of them are explained here.

1) Parameterless constructor

String();

The parameterless constructor is used to create String object with no string.

Example

String name = new String();

2) Parameterized constructors

i) Using string constant or string reference

String(String sl)

Used to pass a String constant or String reference.

Example

String name1 = new String("James");     // String constant is passed

String name2 = new String(name1);      // String reference is passed

Here, String objects referred by name1 & name2 are initialized with string “James”.

ii) Constructor that uses array of characters to initialize String object

String(char characters[])

Used to pass character array.

Example

char name[] = {'J','a','m','e','s'};

String name1 = new String(name);

String object referred by name1 is initialized with string “James”.

iii) Using constructor that initialize String object with the subrange of a character array.

String(char characters[], int startingIndex, int charNums)

StartingIndex – Tells the index at which subrange begins.
charNums – Number of characters present in the subrange.

Example

char name[] = {'J','a','m','e','s',' ','S','m','i','t','h'};
String name1 = new String(name,2,7);

Here, name1 is initialized with string “mes Smi”.

Java Programming Language String Constructor 1

Note

String and array, start with index zero.

iV) Using constructor that pass array of bytes

String(byte byteArray[])

Used to pass an array of bytes.

byte ASCII[] = {74,65,77,69,83};
String name = new String(ASCII);

Java Programming Language String Constructor 2

Example

 public class ArrayOfBytes 
 {
     public static void main(String args[])
     {
         byte ASCII[] = {74,65,77,69,83};
         String name = new String(ASCII);
		
         System.out.println(name);	
     }
 }

Output

JAMES

v) Using constructor that pass an array of bytes to form subrange

String(byte byteArray[], int startingIndex, int charNums)

StartingIndex – Tells the index at which subrange begins.
charNums – Number of characters present in the subrange.

byte ASCII[] = {74,65,77,69,83};
String name = new String(ASCII,1,3);

Java Programming Language String Constructor 3

Example

 public class ArrayofBytes2 
 {
     public static void main(String args[])
     {
         byte ASCII[] = {74,65,77,69,83};
         String name = new String(ASCII,1,3);
		
         System.out.println(name);	
     }
 }

Output

AME

vi) Using constructor that passes StringBuffer

String(StringBuffer strBuff)

Used to pass StringBuffer.

Example

 public class StringBuffCons 
 {
     public static void main(String args[]) 
     {
         StringBuffer sb = new StringBuffer("James");
         String name  = new String(sb);
         System.out.println(name);
     }
 }

Output

James

 

Leave a reply