Site Search:

Creating and manipulating Strings


Back OCAJP



String is the most common used object in java programming. Let's spend some time to look at its class source code.


public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];


First of all, String is an array of char.

Secondly, String is a normal class, so you can use new String("string"); to create a new String object. You can also calls String object's methods to deal with Strings.

Finally, final keyword tells us that String is immutable -- once it's value is set, it can not be changed. 

Although String is immutable, your String object reference can point to another String object instead. 
That is what happens behind String concatenation.




OCAJP>cat test.java 
class test {
  public static void main(String... args) {
    String a = new String("first part "), b = new String("second part"); //two Strings created
    String c = a.concat(b);  //third String "first part second part" created, c point to it.
    String d = new String();  //an empty String
    String f = new String();  //an empty String
    System.out.println(d.equals(f));  //same content
    System.out.println(d==f);  //different memory address
    System.out.println("a="+a);  //a is immutable
    System.out.println("c="+c);
  }
}
OCAJP>javac test.java 
OCAJP>java test
true
false
a=first part 
c=first part second part


As we saw in the above example, regardless the content, each call of new String() creates a new String object, and a new chunk of memory is allocated to hold it, which costs a lot if large amount of Strings are used.

Since String is used so frequently and repeats so much in java programming, java caches and reuses them in a special location in JVM, called string pool. In order to create a String object that will be cached by string pool, you can not use new keyword, you have to create it the "normal" way instead:

String a = "normal way";


OCAJP>cat test.java 
class test {
  public static void main(String... args) {
    String a = "";
    String b = "";
    System.out.println(a.equals(b));
    System.out.println(a == b);  //a and b are pointing to the same object 
  }
}
OCAJP>javac test.java 
OCAJP>java test
true
true


Besides creating a cached String the "normal" way, whenever you need to concatenate them, you need to concatenate them the "normal" way also, in order to preserve the caching behavior.

String a = "string1 " + "string2 " + "string3";
Here + means string concatenation, the concatenated string is also cached in string pool.


OCAJP>cat test.java 
class test {
  public static void main(String... args) {
    String a = "string1 " + "string2 " + "string3";
    String b = "string1 ".concat("string2 ").concat("string3");
    String c = "string1 " + "string2 " + "string3";
    String d = "string1 string2 string3";
    System.out.println(a);
    System.out.println(b);
    System.out.println(a.equals(b));
    System.out.println(a == b);
    System.out.println(a == c);
    System.out.println(a == d);
  }
}
OCAJP>javac test.java
OCAJP>java test
string1 string2 string3
string1 string2 string3
true
false
true
true


There could have trick questions around Addition(+)

OCAJP>cat test.java 
class test {
  public static void main(String... args) {
    System.out.println("a" + 1);  //string + number, number convert to string
    System.out.println("a" + 1 + 2);  //from left to right, "a" + 1 is string "a1", "a1" + 2 is "a12"
    System.out.println(1+ 2 + "a" + 1 + 2);  //from left to right, 1+2 is 3, 3 + "a" is "3a"
  }
}
OCAJP>javac test.java 
OCAJP>java test
a1
a12
3a12


String class provided many utility methods, we need to memorize those hot ones because they are tested in OCA exam.

OCAJP>cat test.java 
class test {
  public static void main(String... args) {
    String a = "123456789";
    System.out.println(a.length());
    System.out.println("charAt");
    System.out.println(a.charAt(0));  //character at index 0
    //System.out.println(a.charAt(9));  //java.lang.StringIndexOutOfBoundsException: String index out of range: 9
    System.out.println("indexOf");
    System.out.println(a.indexOf(5));  //-1
    System.out.println(a.indexOf('5'));  //4
    System.out.println(a.indexOf('A'));  //-1
    System.out.println(a.indexOf("2345"));  //1
    System.out.println(a.indexOf(2345));  //-1
    System.out.println("substring");  
    System.out.println(a.substring(0,1));  //startIndex = 0, endIndex = 1, endIndex exclusive 
    String b = a.substring(0);  //startIndex = 0 to the end of 
    System.out.println(b); 
    System.out.println(b == a);
    System.out.println("startsWith endWith");
    //System.out.println(a.startsWith('1'));  //incompatible types: char cannot be converted to String
    System.out.println(a.startsWith("12345"));
    System.out.println(a.endsWith("4567")); 
    System.out.println("contains"); 
    System.out.println(a.contains("12")); 
    System.out.println(a.contains("ab")); 
    System.out.println("replace"); 
    String c = a.replace("2345","bcde");
    System.out.println(c); 
    System.out.println(c == "1bcde6789"); 
    System.out.println(a.replace('1','A')); 
    System.out.println("trim"); 
    System.out.println(" ab ".trim()); 
    System.out.println("toUpperCase toLowerCase"); 
    System.out.println("abcd".toUpperCase()); 
    System.out.println("ABCD".toLowerCase()); 
    System.out.println("equalsIgnoreCase"); 
    System.out.println("a".equalsIgnoreCase("A")); 
    System.out.println("a".equals("a")); 
  }
}
OCAJP>javac test.java 
OCAJP>java test
9
charAt
1
indexOf
-1
4
-1
1
-1
substring
1
123456789
true
startsWith endWith
true
false
contains
true
false
replace
1bcde6789
false
A23456789
trim
ab
toUpperCase toLowerCase
ABCD
abcd
equalsIgnoreCase
true
true

Back OCAJP