Home >> Java >> Strings In Java
Strings In Java

Strings In Java


In java strings are not array of character and string is not a primitive data type. Strings are the objects of class String  with rigid methods to manipulate it. Strings are immutable, that means once string is defined its contents cannot be changed. To handle mutable strings java provides two other classes StringBuffer and StringBuilder. These all three classes String, StringBuffer and StringBuilder are declared as final so no any class can extend class String. We need not import any package in program to use these classes because they all are defined inside java.lang package. And last thing these all three classes implements CharSequence inteface. CharSequence interface contain four methods length(), charAt(), subString() and toString() methods.


I’m going to describe eight ways to create and initialize string object using constructor. These are

  1. String s = new String();
    • This constructor is parameter less and create a string with no value inside it.
  2. String s = new String(char[] charArray);
    • This constructor takes an array of character and convert that array into String  object and assigns to the s.
  3. String s = new String(char[] charArray, int startIndex, int lengthOfString);
    • This constructor take thrre parameters one array of character, index from which characters should be inserted into string and last the number of characters that should be inserted.
  4. String s = new String(String str);
    • This will take an String as an argument and initialize another string with it’s value.
  5. String s = new String(Byte[] byteArray);
    • This will take an array of byte values, convert each values in ascii characters and inserts them into string just  as in case of character array.
  6. String s = new String(Byte[] byteArray, int startIndex, int lengthOfString);
    • It will create an string and assign a portion of ascii array of size lengthOfIndex starting from startIndex position.
  7. String s = new String(StringBuffer sb);
    • This will create an string from StringBuffer class object.
  8. String s = new String(StringBuilder sb);
    • This will create an string from StringBuilder class object.


Here is example showing the creation of array in various ways.


The output of this code is as follows.


In first case the str1 is created and no value is assigned inside it. So it print nothing after 1.

In second case array of character chs is passed to constructor and that convert array characters to string nad assign it to str2. So the in output it printed LOGRAJ after 2.

In third case character array chs, startIndex 3 and LengthOfString 3 are passed. Which cause taking a portion of size three starting at position 3rd and aconverts it into string. Then in turn that is assigned to str3. So it give output RAJ after 3.

In fourth case we passed str3 as argument and str4 also get the value of str3 i.e. RAJ. So RAJ is printed after 4.

In fifth case we passe an array of (8 bit) bytes which can be regarded as array of ascii character. General array of (16 bit)character represent array of Unicode chars. The ascii character sequence is converted to string and assigned to str5. So we get 8A,LBW after 5. Because the corresponding ascii characters for  values 56, 65, 44, 76, 66 and 87 are ‘8’, ‘A’, ‘,’, ‘L’, ‘B’ and ‘W’ respectively(refer to ASCII Table for detail).

In sixth case the subarray of size 3 of byte array b from index 2 is converted to string and assigned to str6. So we get “,LB” after 6.

In seventh and eighth case the objects of classes StringBuffer and StringBuilder are converted to strings and assigned.

About Log Raj Bhatt

Leave a Reply

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