String Handling

  • In java String is a sequence of characters unlike in another languages String is not an array of characters, It is a object of type String.
  • As java implements String as built-in it provides complementary features to make String handling convenient.
  • String objects can be constructed a number of ways, making it easy to obtain a string when needed.

  • Once String object is created we can not change that String means it will create anew object with the modifications but the original object will remain same.

  • This is due to immutable and fixed feature of String which makes the performance of String effectively.

  • To provide the modifications on String Java allows us by using StringBuffer and StringBuilder both holds strings which can be modified after creating it.

  • The String, StringBuffer, and StringBuilder classes are defined in java.lang so in every program they can be accessed automatically.

  • All are declared final so none of these classes may be subclassed. This allows certain optimizations that increase performance to take place on common string operations.

  • All three implement the CharSequence interface.

  • A variable declared as a String reference can be changed to point at some other String object at any time.

The String Constructors

  • To create a String we have several constructors as follows

String s = new String();//default constructor with no characters

String(char chars[ ]);//To initialize with an array of characters.

String(char chars[ ], int startIndex, int numChars)//To initialize with a sub range of a character array

String(String strObj)//To initialize the string that has the same characters as another object.

  • As typical format for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII character set.
  • We have constructors for these type also

    String(byte chrs[ ])//To initializes with a array of byte

String(byte chrs[ ], int startIndex, int numChars) //To initializes with a range of byte array

// Construct one String from another.
public class MakeString 
{
 public static void main(String args[]) 
 {  
  String s1=new String(); //default

 char c[] = {'J', 'a', 'v', 'a'};
 String s2 = new String(c);//initalizes with an array of characters
  String s3 = new String(c,1,3);//with a range of character array

 String s4 = new String(s2); //Same object

   byte ascii[] = {65, 66, 67, 68, 69, 70 };
   String s5=new String(ascii);
   String s6=new String(ascii,2,4);

   System.out.println(s1);
   System.out.println(s2);
   System.out.println(s3);
   System.out.println(s4);
   System.out.println(s5);
   System.out.println(s6);

 }
}

Output:

Java
ava
Java
ABCDEF
CDEF

  • We can construct a String from a StringBuffer by using the following constructor :

String(StringBuffer strBufObj)

  • We can construct a String from a StringBuilder by using this constructor:

String(StringBuilder strBuildObj)

  • To extend the unicode character set we uses following constructor

String(int codePoints[ ], int startIndex, int numChars)

codePoints is an array that contains Unicode code points.

The resulting string is constructed from the range that begins at startIndex and runs for numChars.

String Length

The length of a string is the number of characters that it have which can obtain by length()

int length( )

// Construct one String from another.
public class MakeString 
{
 public static void main(String args[]) 
 {  
  String s1=new String(); //default

 char c[] = {'J', 'a', 'v', 'a'};
 String s2 = new String(c);//initalizes with an array of characters

   System.out.println("length of String s1 is::"+s1.length());

   System.out.println("length of String s2 is::"+s2.length());
 }
}

Output:

length of String s1 is::0
length of String s2 is::4

results matching ""

    No results matching ""