Followers

coreJava : Strings

Introduction
In java, string is an object that represents sequence of char values. An array of characters works same as java string. For example:
char[] ch={'b','e','n','g','a','l','u','r','u'};  
String s=new String(ch);  
is same as:  String s="bengaluru";  
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces.
The java String is immutable i.e. it cannot be changed. Whenever we change any string, a new instance is created. For mutable string, you can use StringBuffer and StringBuilder classes.
What is String in java
Generally, string is a sequence of characters. But in java, string is an object that represents a sequence of characters. The java.lang.String class is used to create String object.
String object Creation?
There are two ways to create String object:
1.    By string literal
2.    By new keyword
1) String Literal
Java String literal is created by using double quotes. For Example:
     String s="welcome";  
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
     String s1="Welcome";  
     String s2="Welcome";//will not create new instance  


In the above example only one object will be created. Firstly JVM will not find any String object with the value "Welcome" in string constant pool, so it will create a new object. After that it will find the string with the value "Welcome" in the pool, it will not create new object but will return the reference to the same instance.
Note: String objects are stored in a special memory area called string constant pool.
Why java uses concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

2) By new keyword
String s=new String("Welcome"); 
In such case, JVM will create a new string object in normal(non pool) heap memory.

public class StringEx1{  
public static void main(String args[]){  
String s1="java";//creating string by java string literal  
char ch[]={'s','t','r','i','n','g','s'};  
String s2=new String(ch);//converting char array to string  
String s3=new String("example");//creating java string by new keyword  
System.out.println(s1);  
System.out.println(s2);  
System.out.println(s3);  
}}  
Java String class methods
The java.lang.String class provides many useful methods to perform operations on sequence of char values.
Method
Description
char charAt(int index)
returns char value for the particular index
int length()
returns string length
String substring(int beginIndex)
returns substring for given begin index
String substring(int beginIndex, int endIndex)
returns substring for given begin index and end index
boolean contains(CharSequence s)
returns true or false after matching the sequence of char value
boolean equals(Object another)
checks the equality of string with object
boolean isEmpty()
checks if string is empty
String concat(String str)
concatinates specified string
String replace(char old, char new)
replaces all occurrences of specified char value
String replace(CharSequence old, CharSequence new)
replaces all occurrences of specified CharSequence
static String equalsIgnoreCase(String another)
compares another string. It doesn't check case.
String[] split(String regex)
returns splitted string matching regex
String[] split(String regex, int limit)
returns splitted string matching regex and limit
String intern()
returns interned string
int indexOf(int ch)
returns specified char value index
int indexOf(int ch, int fromIndex)
returns specified char value index starting with given index
int indexOf(String substring)
returns specified substring index
int indexOf(String substring, int fromIndex)
returns specified substring index starting with given index
String toLowerCase()
returns string in lowercase.
String toUpperCase()
returns string in uppercase.
String trim()
removes beginning and ending spaces of this string.
static String valueOf(int value)
converts given type into string. It is overloaded.


Immutable String in Java
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.
Once string object is created its data or state can't be changed but a new string object is created.
Let's try to understand the immutability concept by the example given below:
class Testimmutablestring{  
 public static void main(String args[]){  
   String s="Sindhu";  
   s.concat(" olympics");//concat() method appends the string at the end  
   System.out.println(s);//will print Sindhu because strings are immutable objects  
 }  
}  
Because java uses the concept of string literal.Suppose there are 5 reference variables,all refers to one object "Sindhu". If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.
Java String compare
We can compare strings in java on the basis of content and reference.
It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.
There are three ways to compare strings in java:
  1. By equals() method
  2. By = = operator
  3. By compareTo() method
1) equals() method
The String equals() method compares the original content of the strings. It compares values of string for equality. String class provides two methods:
  • public boolean equals(Object another) compares this string to the specified object.
  • public boolean equalsIgnoreCase(String another) compares this String to another string, ignoring case.
class TestEqualsEx{  
 public static void main(String args[]){  
   String s1="Sindhu";  
   String s2="Sindhu";  
   String s3=new String("Sindhu");  
   String s4="Saurav";  
   System.out.println(s1.equals(s2));//true  
   System.out.println(s1.equals(s3));//true  
   System.out.println(s1.equals(s4));//false  
 }  
}  
class TestCaseSensitive{  
 public static void main(String args[]){  
   String s1="Sachin";  
   String s2="SACHIN";  
  
   System.out.println(s1.equals(s2));//false  
   System.out.println(s1.equalsIgnoreCase(s3));//true  
 }  
}  
2) String compare by == operator
The = = operator compares references not values.
class TestDoubleEqualsEx{  
 public static void main(String args[]){  
   String s1="Sindhu";  
   String s2="Sindhu";  
   String s3=new String("Sindhu");  
   System.out.println(s1==s2);//true (because both refer to same instance)  
   System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool i.e heap)  
 }  
}  
3) String compare by compareTo() method
The String compareTo() method compares values lexicographically and returns an integer value that describes if first string is less than, equal to or greater than second string.
Suppose s1 and s2 are two string variables. If:
  • s1 == s2 :0
  • s1 > s2   :positive value
  • s1 < s2   :negative value
class TestCompareTo{  
 public static void main(String args[]){  
   String s1="Sindhu";  
   String s2="Sindhu";  
   String s3="Sachin";  
   System.out.println(s1.compareTo(s2));//0  
   System.out.println(s1.compareTo(s3));//1(because s1>s3),it may return any positive number
   System.out.println(s3.compareTo(s1));//-1(because s3 < s1 ), it may return any negative number
 }  
}  
String Concatenation in Java
In java, string concatenation forms a new string that is the combination of multiple strings. There are two ways to concat strings in java:
  1. By + (string concatenation) operator
  2. By concat() method
1) String Concatenation by + (string concatenation) operator
Java string concatenation operator (+) is used to add strings. For Example:
class TestStringConcatenation1{  
 public static void main(String args[]){  
   String s="Sindhu"+" olympics";  
   System.out.println(s);//Sindhu olympics  
 }  
}  
In java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String concatenation operator produces a new string by appending the second operand onto the end of the first operand. The string concatenation operator can concat not only string but primitive values also. For Example:
class TestStringConcatenation2{  
 public static void main(String args[]){  
   String s=10+10+"Sindhu"+20+20;  
   System.out.println(s);//20Sindhu2020  
 }  
}  Note: All the + will be treated as string concatenation operator after a string literal
2) String Concatenation by concat() method
The String concat() method concatenates the specified string to the end of current string. Syntax:
1.    public String concat(String another)  
Let's see the example of String concat() method.
class StringConcatEx{  
 public static void main(String args[]){  
   String s1="Sindhu ";  
   String s2=" P V";  
   String s3=s1.concat(s2);  
   System.out.println(s3);//Sindhu P V  
  }  
}  
Substring in Java
A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.
Note: Index starts from 0.
You can get substring from the given string object by one of the two methods:
  1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
  2. public String substring(int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex (Inclusive) to endIndex (exclusive).
In case of string:
  • startIndex: inclusive
  • endIndex: exclusive
Let's understand the startIndex and endIndex by the code given below.
1.    String s="Sindhu";  
2.    System.out.println(s.substring(0,2));//Si  
In the above substring, 0 points to S but 2 points to e (because end index is exclusive).
Example of java substring
public class TestSubstring{  
 public static void main(String args[]){  
   String s="Sachin Tendulkar";  
   System.out.println(s.substring(6));//Tendulkar  
   System.out.println(s.substring(0,6));//Sachin  
 }  
}  
Java String class methods
The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.
Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web based or mobile application.
Let's see the important methods of String class.
Java String toUpperCase() and toLowerCase() method
The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.
String s="Sachin";  
System.out.println(s.toUpperCase());//SACHIN  
System.out.println(s.toLowerCase());//sachin  
System.out.println(s);//Sachin(no change in original)  
Java String trim() method
The string trim() method eliminates white spaces before and after string.
String s="  Sachin  ";  
System.out.println(s);//  Sachin    
System.out.println(s.trim());//Sachin  
Java String startsWith() and endsWith() method
String s="Sachin";  
 System.out.println(s.startsWith("Sa"));//true  
 System.out.println(s.endsWith("n"));//true  
Java String charAt() method
The string charAt() method returns a character at specified index.
String s="Sachin";  
System.out.println(s.charAt(0));//S  
System.out.println(s.charAt(3));//h  
Java String length() method
The string length() method returns length of the string.
String s="Sachin";  
System.out.println(s.length());//6  
Java String intern() method
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
                    String s1 = "abc";
              String s2 = new String("abc");
              System.out.println(s1.equals(s2)); //true
              System.out.println(s1 == s2); //false
              s2 = s2.intern();
            System.out.println(s1 == s2); //true                                               System.out.println(s1.equals(s2)); //true

Java String valueOf() method
The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string.
int a=10;  
String s=String.valueOf(a);  
System.out.println(s+10);  
Java String replace() method
The string replace() method replaces all occurrence of first sequence of character with second sequence of character.
String s1="Java is a programming language. Java is a platform. Java is an Island.";    
String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "Kava"    
System.out.println(replaceString);    
Output:
Kava is a programming language. Kava is a platform. Kava is an Island.
Java StringBuffer class is used to created mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.

Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order.

Important Constructors of StringBuffer class
  1. StringBuffer(): creates an empty string buffer with the initial capacity of 16.
  2. StringBuffer(String str): creates a string buffer with the specified string.
  3. StringBuffer(int capacity): creates an empty string buffer with the specified capacity as length.
Important methods of StringBuffer class
  1. public synchronized StringBuffer append(String s): is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
  2. public synchronized StringBuffer insert(int offset, String s): is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
  3. public synchronized StringBuffer replace(int startIndex, int endIndex, String str): is used to replace the string from specified startIndex and endIndex.
  4. public synchronized StringBuffer delete(int startIndex, int endIndex): is used to delete the string from specified startIndex and endIndex.
  5. public synchronized StringBuffer reverse(): is used to reverse the string.
  6. public int capacity(): is used to return the current capacity.
  7. public void ensureCapacity(int minimumCapacity): is used to ensure the capacity at least equal to the given minimum.
  8. public char charAt(int index): is used to return the character at the specified position.
  9. public int length(): is used to return the length of the string i.e. total number of characters.
  10. public String substring(int beginIndex): is used to return the substring from the specified beginIndex.
  11. public String substring(int beginIndex, int endIndex): is used to return the substring from the specified beginIndex and endIndex.
What is mutable string
A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.
1) StringBuffer append() method
The append() method concatenates the given argument with this string.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.append("Java");//now original string is changed  
System.out.println(sb);//prints Hello Java  
}  
}  
2) StringBuffer insert() method
The insert() method inserts the given string with this string at the given position.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello ");  
sb.insert(1,"Java");//now original string is changed  
System.out.println(sb);//prints HJavaello  
}  
}  
3) StringBuffer replace() method
The replace() method replaces the given string from the specified beginIndex and endIndex.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.replace(1,3,"Java");  
System.out.println(sb);//prints HJavalo  
}  
}  
4) StringBuffer delete() method
The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.delete(1,3);  
System.out.println(sb);//prints Hlo  
}  
}  
5) StringBuffer reverse() method
The reverse() method of StringBuilder class reverses the current string.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer("Hello");  
sb.reverse();  
System.out.println(sb);//prints olleH  
}  
}  
6) StringBuffer capacity() method
The capacity() method of StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity());//default 16  
sb.append("Hello");  
System.out.println(sb.capacity());//now 16  
sb.append("java is my favourite language");  
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
}  
}  
7) StringBuffer ensureCapacity() method
The ensureCapacity() method of StringBuffer class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
class A{  
public static void main(String args[]){  
StringBuffer sb=new StringBuffer();  
System.out.println(sb.capacity());//default 16  
sb.append("Hello");  
System.out.println(sb.capacity());//now 16  
sb.append("java is my favourite language");  
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
sb.ensureCapacity(10);//now no change  
System.out.println(sb.capacity());//now 34  
sb.ensureCapacity(50);//now (34*2)+2  
System.out.println(sb.capacity());//now 70  
}  
}  
Java StringBuilder class
Java StringBuilder class is used to create mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.
Important Constructors of StringBuilder class
  1. StringBuilder(): creates an empty string Builder with the initial capacity of 16.
  2. StringBuilder(String str): creates a string Builder with the specified string.
  3. StringBuilder(int length): creates an empty string Builder with the specified capacity as length.
    Important methods of StringBuilder class
Method
Description
public StringBuilder append(String s)
is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
public StringBuilder insert(int offset, String s)
is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
public StringBuilder replace(int startIndex, int endIndex, String str)
is used to replace the string from specified startIndex and endIndex.
public StringBuilder delete(int startIndex, int endIndex)
is used to delete the string from specified startIndex and endIndex.
public StringBuilder reverse()
is used to reverse the string.
public int capacity()
is used to return the current capacity.
public void ensureCapacity(int minimumCapacity)
is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index)
is used to return the character at the specified position.
public int length()
is used to return the length of the string i.e. total number of characters.
public String substring(int beginIndex)
is used to return the substring from the specified beginIndex.
public String substring(int beginIndex, int endIndex)
is used to return the substring from the specified beginIndex and endIndex.


Java StringBuilder Examples
Let's see the examples of different methods of StringBuilder class.
1) StringBuilder append() method
The StringBuilder append() method concatenates the given argument with this string.
class A{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.append("Java");//now original string is changed  
System.out.println(sb);//prints Hello Java  
}  
}  
2) StringBuilder insert() method
The StringBuilder insert() method inserts the given string with this string at the given position.
class A{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello ");  
sb.insert(1,"Java");//now original string is changed  
System.out.println(sb);//prints HJavaello  
}  
}  
3) StringBuilder replace() method
The StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex.
class A{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.replace(1,3,"Java");  
System.out.println(sb);//prints HJavalo  
}  
}  
4) StringBuilder delete() method
The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.
class A{  
public static void main(String args[]){  
StringBuilder sb=new StringBuilder("Hello");  
sb.delete(1,3);  
System.out.println(sb);//prints Hlo  
}  
}  





  5) StringBuilder reverse() method
   The reverse() method of StringBuilder class reverses the current string.
        class A{  
        public static void main(String args[]){  
        StringBuilder sb=new StringBuilder("Hello");  
        sb.reverse();  
        System.out.println(sb);//prints olleH  
        }  
        }  
   6) StringBuilder capacity() method
   The capacity() method of StringBuilder class returns the current capacity of the Builder. The            default capacity of the Builder is 16. If the number of character increases from its current 
   capacity,   it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 1 16, it will       be (16*2)+2=34.
          class A{  
          public static void main(String args[]){  
          StringBuilder sb=new StringBuilder();  
          System.out.println(sb.capacity());//default 16  
          sb.append("Hello");  
          System.out.println(sb.capacity());//now 16  
          sb.append("java is my favourite language");  
          System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
         }  
         }  
     7) StringBuilder ensureCapacity() method
   The ensureCapacity() method of StringBuilder class ensures that the given capacity is the              minimum to the current capacity. If it is greater than the current capacity, it increases 
   the capacity b  y (oldcapacity*2)+2. For example if your current capacity is 16, 
   it will be (16*2)+2=34.
         class A{  
         public static void main(String args[]){  
         StringBuilder sb=new StringBuilder();  
         System.out.println(sb.capacity());//default 16  
         sb.append("Hello");  
         System.out.println(sb.capacity());//now 16  
         sb.append("java is my favourite language");  
         System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
         sb.ensureCapacity(10);//now no change  
         System.out.println(sb.capacity());//now 34  
         sb.ensureCapacity(50);//now (34*2)+2  
         System.out.println(sb.capacity());//now 70  
         }  
         }  

  Difference between String and StringBuffer
   There are many differences between String and StringBuffer. A list of differences between 
    String and StringBuffer are given below:
No.
    String
StringBuffer
1)
String class is immutable.
StringBuffer class is mutable.
2)
String is slow and consumes more memory when you concat too many strings because every time it creates new instance.
StringBuffer is fast and consumes less memory when you cancat strings.
3)
String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.
StringBuffer class doesn't override the equals() method of Object class.



         
            Performance Test of String and StringBuffer
          public class ConcatTest{  
          public static String concatWithString()    {  
          String t = "Java";  
           for (int i=0; i<10000; i++){  
            t = t + " Class";  
         }  
        return t;  
         }  
         public static String concatWithStringBuffer(){  
         StringBuffer sb = new StringBuffer("Java");  
         for (int i=0; i<10000; i++){  
             sb.append("Class");  
         }  
         return sb.toString();  
        }  
         public static void main(String[] args){  
         long startTime = System.currentTimeMillis();  
         concatWithString();  
         System.out.println("Time taken by Concating with String: "+
         (System.currentTimeMillis()-start T        ime)+"ms");  
        startTime = System.currentTimeMillis();  
        concatWithStringBuffer();  
        System.out.println("Time taken by Concating with  StringBuffer: "+
        (System.currentTimeMillis()-startTime)+"ms");  
          }  
     }         
   

            String and StringBuffer HashCode Test
     As you can see in the program given below, String returns new hashcode value when you concat     String but StringBuffer returns same.
         public class InstanceTest{  
         public static void main(String args[]){  
         System.out.println("Hashcode test of String:");  
         String str="java";  
         System.out.println(str.hashCode());  
         str=str+"Class";  
         System.out.println(str.hashCode());  
         
         System.out.println("Hashcode test of StringBuffer:");  
         StringBuffer sb=new StringBuffer("java");  
         System.out.println(sb.hashCode());  
         sb.append("Class");  
         System.out.println(sb.hashCode());  
         }  
    }  


       Difference between StringBuffer and StringBuilder
    There are many differences between StringBuffer and StringBuilder. A list of differences between
    StringBuffer and StringBuilder are given below:

No.
StringBuffer
StringBuilder
1)
StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously.
StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.
2)
StringBuffer is less efficient than StringBuilder.
StringBuilder is more efficient than StringBuffer.

   StringBuffer Example
         public class BufferTest{  
         public static void main(String[] args){  
         StringBuffer buffer=new StringBuffer("hello");  
         buffer.append("java");  
         System.out.println(buffer);  
         }  
         }  
   StringBuilder Example
         public class BuilderTest{  
         public static void main(String[] args){  
         StringBuilder builder=new StringBuilder("hello");  
         builder.append("java");  
         System.out.println(builder);  
         }  
         }  
   Performance Test of StringBuffer and StringBuilder
     Let's see the code to check the performance of StringBuffer and StringBuilder classes.
         public class ConcatTest{  
         public static void main(String[] args){  
         long startTime = System.currentTimeMillis();  
         StringBuffer sb = new StringBuffer("Java");  
         for (int i=0; i<10000; i++){  
             sb.append("Class");  
         }   
                      S    ystem.out.println("Time taken by StringBuffer: " + (System.currentTimeMillis() - startTime) + "ms");  
        startTime = System.currentTimeMillis();  
        StringBuilder sb2 = new StringBuilder("Java");  
        for (int i=0; i<10000; i++){  
            sb2.append("Class");  
        }  
        System.out.println("Time taken by StringBuilder: " + (System.currentTimeMillis() - startTime) +   "        ms");  
    }  
}  
   How to create Immutable class?
    There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, 
   Double etc. In short, all the wrapper classes and String class is immutable. We can also create 
   immutable class by creating final class that have final data members as the example 
   given below:
   Example to create Immutable class

In this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method.

         public final class Employee{  
         final String pancardNumber;  
  
         private Employee(String pancardNumber){  
         this.pancardNumber=pancardNumber;  
          }  
            
          public String getPancardNumber(){  
          return pancardNumber;  
          }                
           }    
     The above class is immutable because:
  • The instance variable of the class is final i.e. we cannot change the value of it after creating an object.
  • The class is final so we cannot create the subclass.
  • There is no setter methods i.e. we have no option to change the value of the instance variable.
    These points makes this class as immutable.
   Java toString() method
    If you want to represent any object as a string, toString() method comes into existence.
    The toString() method returns the string representation of the object.
    If you print any object, java compiler internally invokes the toString() method on the object. So o   Overriding the toString() method, returns the desired output, it can be the state of an object etc. d   depends on your implementation.
    Advantage of Java toString() method
     By overriding the toString() method of the Object class, we can return values of the object, so         we don't need to write much code.
    Understanding problem without toString() method
      Let's see the simple code that prints reference.
           class Student{  
           int rollno;  
           String name;  
           String city;  
            
          Student(int rollno, String name, String city){  
          this.rollno=rollno;  
          this.name=name;  
          this.city=city;  
          }  
                
         public static void main(String args[]){  
         Student s1=new Student(101,"Raj","lucknow");  
         Student s2=new Student(102,"Vijay","ghaziabad");  
        
         System.out.println(s1);//compiler writes here s1.toString()  
         System.out.println(s2);//compiler writes here s2.toString()  
         }  
         }  
        As you can see in the above example, printing s1 and s2 prints the hashcode values    of the objects but I want to print the values of these objects. Since java         compilerinternally calls toString() method, overriding this method will return the specified values. Let's understand it with the example given below:

     Example of Java toString() method
       Now let's see the real example of toString() method.
           class Student{  
           int rollno;  
           String name;  
           String city;  
           
           Student(int rollno, String name, String city){  
           this.rollno=rollno;  
           this.name=name;  
           this.city=city;  
           }  
              
          public String toString(){//overriding the toString() method  
          return rollno+" "+name+" "+city;  
         }  
         public static void main(String args[]){  
         Student s1=new Student(101,"Raj","lucknow");  
         Student s2=new Student(102,"Vijay","ghaziabad");  
               
          System.out.println(s1);//compiler writes here s1.toString()  
          System.out.println(s2);//compiler writes here s2.toString()  
         }  
         }  
     
    StringTokenizer in Java
    
     The java.util.StringTokenizer class allows you to break a string into tokens. It is simple 
      way to break string. It doesn't provide the facility to differentiate numbers, quoted strings,
      identifiers etc. like StreamTokenizer class.
     Constructors of StringTokenizer class
       There are 3 constructors defined in the StringTokenizer class.
        
 Constructor
Description
StringTokenizer(String str)
creates StringTokenizer with specified string.
StringTokenizer(String str, String delim)
creates StringTokenizer with specified string and delimeter.
StringTokenizer(String str, String delim, boolean returnValue)
creates StringTokenizer with specified string, delimeter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.

        Methods of StringTokenizer class
           The 6 useful methods of StringTokenizer class are as follows:
             
    Public method
Description
boolean hasMoreTokens()
checks if there is more tokens available.
String nextToken()
returns the next token from the StringTokenizer object.
String nextToken(String delim)
returns the next token based on the delimeter.
boolean hasMoreElements()
same as hasMoreTokens() method.
Object nextElement()
same as nextToken() but its return type is Object.
int countTokens()
returns the total number of tokens.

       

     Simple example of StringTokenizer class
     Let's see the simple example of StringTokenizer class that tokenizes a string "my name is
     khan" on the basis of whitespace.
          import java.util.StringTokenizer;  
          public class Simple{  
          public static void main(String args[]){  
          StringTokenizer st = new StringTokenizer("my name is khan"," ");  
          while (st.hasMoreTokens()) {  
          System.out.println(st.nextToken());  
         }  
         }  
    }                    
 Example of nextToken(String delim) method of StringTokenizer class
         import java.util.*;       
         public class Test {  
         public static void main(String[] args) {  
         StringTokenizer st = new StringTokenizer("my,name,is,khan");  
             
          // printing next token  
          System.out.println("Next token is : " + st.nextToken(","));  
         }      
         }  

                   

















No comments:

Post a Comment