Strings In Java

927 views 5 minutes read
A+A-
Reset

Introduction:

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.

Constructors:

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.

Example:

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

public class TestAgain{
   public static void main(String args[]){
   //1. Constructor without parameter
	   String str1 = new String();
	   System.out.println("1. "+str1);
   //2. Intializing With array of character
	   char[] chs = {'L','O','G','R','A','J'};
	   String str2 = new String(chs);
	   System.out.println("2. "+str2);
   //3. Initializing with Portion of character array
 	   String str3 = new String(chs, 3,3);
	   System.out.println("3. "+str3);
   //4. Initializing with another string
	   String str4 = new String(str3);
	   System.out.println("4. "+str4);
   //5. Initialiizen with array of bytes (ascii) array
	   byte[] b = {56,65,44,76,66,87};
	   String str5= new String(b);
	   System.out.println("5. "+str5);
   //6. Initializing with a portion of ascii array
	   String str6= new String(b,2,3);
	   System.out.println("6. "+str6);
   //7. Intializing with StrinBuffer object
	   StringBuffer sb = new StringBuffer("Queen");
	   String str7 = new String(sb);
	   System.out.println("7. "+str7);
   //8. Initialization with StringBuilder object
	   StringBuilder sbb = new StringBuilder("KingNQueen");
	   String str8 = new String(sbb);
	   System.out.println("8. "+str8);
   }
}

 

The output of this code is as follows.

1.
2. LOGRAJ
3. RAJ
4. RAJ
5. 8A,LBW
6. ,LB
7. Queen
8. KingNQueen

 Explanation:

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.

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.