Java- String Handling - JavaTechWorld

Saturday, 20 May 2017

Java- String Handling


String in java- String is sequence of character placed in double quote (" "). Performing different operation on string data is called String Handling. All String literals in java program, such as "xyz" are implementd as instance of this class. String are constant; their value can't be changed in the same memory after they are created.
So string is defined as it is an immutable sequence of character. 
In java.lang package we have below three classes to store and perform differnet operation on string of characters.
  1. String
  2. StringBuffer
  3. StringBuilder
All these classes are siblings, and they are subclasses of CharSequence interface.Below digram shows the inheritance realtion of these three classes.
String Inheritance Diagram
Note :
  •  All these classes are final classes.
  • Threse three classes are also subclasses of interface.
  • Java String class is a subclass of Comparable interface.
Why String class is given when we have char array is available ?

The limitation of arrays is size. Once array is created with particular size then its size can not be modified so if we use character array them we can only store character up to its size.
If we want to store new character beyond its size then we have to create character array with new size and copy old charachter array to new array. We should repeat the same process every time when array is filled which is time consuming and performance of the application will decrease.

Possible ways to create java String object -
We can create Java String object in two ways
  • String literal directly like String s="xyz";
  • Using string class constructors.
Below are the 8 important constructors of java string.
  1.  String()
    Creates empty string object.
    String s = new String();

    2.    String(String value)
    Creates String object with given string object characters. It perform String copy operation.
    String s=”xyz”;
    String s1 = new String(s); //string copy

    //creating direct string literal
    String s2= new String(“abc”);

    3.    String(StringBufffer sb)
    Creates new String object with the given StringBuffer object data.

    4.    String(StringBuilder sb)
    Creates new String object with the given StringBuilder object content.

    5.    String(char[] ch)
    Creates String object with the given char array values.
    Char[] ch= {‘x’,’y’,’z’};
    String s1=new String(ch);
    System.out.println(“ value is: ”+s1);

    6.    String(char[] ch, int offset, int count)
    Create 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.
    Char [] ch = {‘a’, ‘b’, ‘c’, ‘d’, ’e’, ‘f’, ‘g’, ‘h’};
    String s1= new String(ch, 2,4};
    System.out.println(“s1: “ +s1);  // cdef

    7.    String(byte[] b)
    Creates new string object by copying the given byte[] number by converting them into their ASCII characters.
    byte[] b= {101,102,103,104};
    String s= new String(b);
    System.out.println(s); //efgh

    8.    String(byte [] b, int offset, int count)
    Create new String object with the given count number of bytes from the given offset in the byte[].
    byte[] b = {101,102,103,104,105};
    String s = new String(b,2,2);
    System.out.println("value of s: "+s);
Why String object is immutable?
Because of String pooling concept.

What is Java String Pool?
If we create String object by assigning same string literal, only one object is created and all reference variable are intialized with that same String object reference. Then all referenced variables are pointing to same String object. This is reason Sring Object become immutable.

Below diagram show String Pooling with JVM architechture-
String Pooling with JVM Architechture
import java.util.*;
/** * Created by namdeva on 20/05/2017. */
public class StringPooling {
    String s="abc";
    String s1="abc";

    System.out.println(s==s1); //true   
    System.out.println(s.equals(s1)); //ture
    String str= new String("abc");
    String str1= new String("abc");

    System.out.println(str==str1);      //false    
    System.out.println(str.equals(str1)); //true
This String pool memory management diagram in java is not applicalbe for String Object those are created using "new keyword and constructor".
Java String have seperate object and those string object are created in heap area directly.
Below given program for practice purpose Let me know your output in the comment box : 
class Practice {
    public static void main(String[] args) {
        String s1="abc";
        String s2="abc";
        String s3="abc";
        String s4="bc";
        String s5= new String("abc");
        String s6= new String("abc");
        String s7= "ab";
        String s8= s1+"b";
        String s9= "a"+"b";
String object creation and memory structure:
String object  contain char[] object to store string character.

String Method in java:
  1. isEmpty()
  2. length()
  3. equalsIgnoreCase()
  4. compareTo
  5. startsWith() /endsWith()
  6. indexOf
  7. contain(String s)
  8. findWithIndexOf() /findWithIndexOf()
  9. substring()
  10. concat()
  11. replace(char oldChar , char newChar)
  12. trim()
  13. split()
For more update keep visiting to this website you can post your query in the comment box i will try to response you as early as possible.

© 2014 JavaTechWorld. Designed by Bloggertheme9
Powered by Blogger.