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 overriding the toString() method, returns the desired output, it can be the state of an object etc. 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

Example:

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(120,"Raja","bangalore");  
   Student s2=new Student(125,"rajan","hyderabad");  
System.out.println(s1); //compiler writes here s1.toString()  
System.out.println(s2); //compiler writes here s2.toString()  
 }  
}  

Output:

Student@15db9742
Student@6d06d69c
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 compiler internally calls toString() method, overriding this method will return the specified values.

Example of Java toString() method

see the real example of toString() method.

Example:

class Student1{  
 int rollno;  
 String name;  
 String city;  
 Student1(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(120,"Raja","bangalore");  
   Student s2=new Student(125,"Rajan,"hyderabad");  
   System.out.println(s1); //compiler writes here s1.toString()  
   System.out.println(s2); //compiler writes here s2.toString()  
  }  
 }  

Output:

120 Raja bangalore
125 Rajan hyderabad

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.
ConstructorDescription
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 methodDescription
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.

Example of StringTokenizer class:

import java.util.StringTokenizer;  
public class Example{  
 public static void main(String args[]){  
   StringTokenizer st = new StringTokenizer("bangalore is big city"," ");  
     while (st.hasMoreTokens()) {  
         System.out.println(st.nextToken());  
     }  
   }  
}

Output:

bangalore
is
big
city