Home‎ > ‎Applied Core Java‎ > ‎Introduction‎ > ‎

Test String Class

Problem : Describe java.lang.String class methods

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestString.java  and copy below contents.


 * A program display
 * Test String methods

public class TestString {

    public static void main(String[] args) {

        String name = "Vijay Dinanath Chouhan";

        System.out.println(" String Length- " + name.length());
        System.out.println(" 7 ths caharcter is- " + name.charAt(6));
        System.out.println(" Dina index is- " + name.indexOf("Dina"));
        System.out.println(" First i Position- " + name.indexOf("i"));
        System.out.println(" Last i Position- " + name.lastIndexOf("i"));
        System.out.println(" a is replaced by b- " + name.replace("a", "b"));
        System.out.println(" All a is replaced by b- "
                + name.replaceAll("a", "b"));
        System.out.println(" Chota vijay- " + name.toLowerCase());
        System.out.println(" Bada vijay- " + name.toUpperCase());
        System.out.println(" Starts With Vijay- " + name.startsWith("Vijay"));
        System.out.println(" Ends with han- " + name.endsWith("han"));
        System.out.println(" Substring- " + name.substring(6));



Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestString.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestString.java with help of
    • javac TestString.java command. Command will create class file in the same folder.
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestString command.


 String Length- 21
 7 ths caharcter is- D
 Dina index is- 6
 First i Position- 1
 Last i Position- 7
 a is replaced by b- Vijby Dinbnth Chouhbn
 All a is replaced by b- Vijby Dinbnth Chouhbn
 Chota vijay- vijay dinanth chouhan
 Starts With Vijay- true
 Ends with han- true
 Substring- Dinanth Chouhan


Q1. What is String class?

 A : The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. 

Q.2 : What is function of compareTo() method ?

A : Compareto method is used to compare two object values. It compares only values  not the reference of the object,  and return a value which is either positive or negative or 0. for Example  if you do this "a".compareTo("z"); then the result will be  -25 and   if you do this "z".compareTo("a"); then the result will be 25 . If result is negative than a is less than z and if value is positive than z is greater than a . It is use for natural Ordering.

Q. 3 What is difference between  == operator and equals() method?

a.     Str1.equals(str2)

b.     Str1 == str 2  

A : Differences
  • The equals( ) method compares the characters inside a String object The == operator compares two object references to see whether they refer to the same memory address.
  •  == operator Compares references, not values. Where as Equal Method Compares values for equality. Because this method is defined in the Object class, from which all other classes are derived, it's automatically defined for every class.
  • Equal is a method of object Class where as == is an Operator.

 Q. 4 :   What are differences between following statements

a.       String s1 =”Hello”;

b.      String s2 = new String (“Hello”);

 A : When JVM find two string object with the  same content, in order to save memory, insted of create two objects, this two objects stay referenced to the same memory location. when there is already a object having value HELLO , JVM detect that already exist Hello string so it did not create a new object, JVM just make a reference the two objects to the same "Hello", so S1 and S2 are "pointing" to the same place in the memory

Q.5 What is difference between String and StringBuffer?

A : String is Immutable whereas String Buffer is mutable . By immutable, we mean that the value stored in the String object cannot be changed. By mutable we mean that the value stored in the String object can be changed.

For instance : 1
String str1="Hello";

OUTPUT : HelloSunilOS

For instance : 2

StringBuffer str1 = new StringBuffer("Hello");

OUTPUT : HelloSunilOS

Explanation: srt1 is pointing to memory address of string "Hello". when "SunilOS" is added to "Hello" than new memory address is allocated and str1 will start point to that address . That means whenever appending will be done memory get changed changes cannot be made to old object . new object will be created every time.
But when String Buffer class object is created by default a block of memory is provided to str1 and appending can be done in that block . Again n again new object is not created.only block size is changed .

Q.6 : What is difference between StringBuffer and StringBuilder ?

A : StringBuffer and StringBuilder can change their values. Both are Mutable . The only difference is that StringBuilder is unsynchronized whereas StringBuffer is synchronized. So when the application needs to be run only in a single thread then it is better to use StringBuilder. StringBuilder is more efficient than StringBuffer.

Q.7 : What is the criteria to choose among String, StringBuffer and StringBuilder ?

A :
If your text is not going to change use a string Class because a String object is immutable.
If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
If your text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.

nakul shukla,
Mar 19, 2009, 7:38 AM