Java- String Handling - JavaTechWorld

Saturday, 20 May 2017

Java- String Handling

    0






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 java.io.Serializable 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.
    Example:
    String s = new String();
    System.out.println(s);

    2.    String(String value)
    Creates String object with given string object characters. It perform String copy operation.
    Example:
    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.
    Example:
    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.
    Example:
    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.
    Example:
    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[].
    Example:
    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";
        
        System.out.print(s1==s2);
        System.out.print(s5==s6);
        System.out.print(s7==s8);
        System.out.print(s7=s9);
    }
}
String object creation and memory structure:
String object  contain char[] object to store string character.
StringObjectDiagram

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()
  14.  
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.