Wednesday, 1 February 2017

STRING CLASS IN JAVA

In general string can be defined as it is a sequence of characters. String is constant their values cannot be changed in the same memory after they are created. This behavior is nothing but immutability of String object. Object of the string class could be created wither by using new operator or by using double quotes “”. Whenever JVM finds a “”, JVM will automatically create an object of the string class with the mentioned sequence of characters and every characters having a unique index starting from 0.

Why string class is given when char array is available to represent sequence of characters –

String class is given to store characters dynamically without size limitations and also to perform different common operations of string data. This means the limitations on char arrays is size. Once array is created with some size then its size cannot be modified. Hence if we use character array to represent/store sequence of characters then we can only store characters up to its size.

If we want to store new characters beyond its size, we must create an array with the new required size and should copy all the old array characters to new array and then we have to store new value at end. We should repeat the same process every time when array is filled  and want to add new characters. Hence using String class we can store sequence of characters without size limitations.

Possible ways to create string object –
1. By assigning string literal directly.
2. By using any one of the string class constructors.

  • ·        String() – Creates empty string object, not null string object.
  • ·        String(String value) – Creates string object with given string object characters. It performs string copy operation.
  • ·        String(StringBuffer sb) – Creates new string object with the given StringBuffer object data. Performs string copy operation from StringBuffer object to string object.
  • ·        String(StringBuilder sb) – Creates new string object with the given StringBuilder object’s data. Performs string copy operation from StringBuilder object.
  • ·        String(char[] ch) – Creates string object with the given char array values. Performs string copy operation from char[] object to String object.
  • ·        String(char[] ch,int offset, int count) – Creates new string object with the given count number of characters from the given offset in the char[] object. Here offset is the starting index from which characters must be copied.
  • ·        String(byte[] b) – Creates new string object by copying the given byte[] numbers by converting them into ASCII characters.
  • ·        String(byte[] b,int offset,int count) – Creates new string object with the given count number of bytes from the given offset in the byte[]. All bytes are stored in the ASCII character form.


Example –
public class String1 {
          public static void main(String[] args) {
                   String s = new String();
                   System.out.println(s); // no output empty string object
                   String s1 = "hello java";
                   System.out.println(s1);
                   String s2 = new String(s1); // copy string
                   System.out.println(s2);
                   String s3 = new String("Howz u"); // direct string literal
                   System.out.println(s3);
                   String s4 = s3; // string assignment
                   System.out.println(s4);
                   char[] ch ={'a','b','c','d'};
                   String s5 = new String(ch); // string with char array value
                   System.out.println(s5);
                   String s6 = new String(ch,1,3); // string with offset and count no of char
                   System.out.println(s6);
                   byte[] b = {97,98,99};
                   String s7 = new String(b);   // string with byte[] numbers
                   System.out.println(s7);
                   String s8 = new String(b,1,2); // string with byte offset and count
                   System.out.println(s8);
          }
}

Output –

hello java
hello java
Howz u
Howz u
abcd
bcd
abc
bc 

No comments:

Post a Comment