Tuesday, 5 November 2013

Java string


A String is a sequence of characters, numbers, alphabets and symbols. The String class in java.util package. The String class in java provides number of methods for string manipulation.

Java supports direct string concatenation with “+” symbol, without use of any other method. A String is always read-only and once it is created it cannot be modified.

Creating a string is very simple. Just enclose the string in double quotes and assign it to a literal using assignment operator. String creation through assignment operator does requires new keyword.
Example:
String str=”New String”;
String str1=”Hello Java”;

The String class have 13 constructors overloaded and having different parameters.
  1. String() 
  2. String(byte[] bytes) 
  3. String(byte[] ascii, int hibyte)
  4. String(byte[] bytes, int offset, int length) 
  5. String(byte[] ascii, int hibyte, int offset, int count) 
  6. String(byte[] bytes, int offset, int length, String charsetName) 
  7. String(byte[] bytes, String charsetName) 
  8. String(char[] value)
  9. String(char[] value, int offset, int count)
  10. String(int[] codePoints, int offset, int count) 
  11. String(String original) 
  12. String(StringBuffer buffer)
  13. String(StringBuilder builder)

You can create sting string using any of the above constructor.
Example :
 String str1 = new String();
 String str2=new String(”Hello Java”);

 String str3=new String(str2);

char ch[]={‘H’,’E’,’L’,’L’,’O’};
 String str4=new String(ch);

String is one of the most commonly used class in java.

No comments:

Post a Comment