Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact

Home Page

AKGBackup - The backup program



Section 9 : The java.lang Package

Describe the significance of the immutability of String objects.





It is a public, final class. Implements Serializable, Comparable.


Unlike many other languages that implement strings as character arrays, Java implements strings as objects of type String. This allows Java to provide a full complement of features that make string handling convenient. Also, String objects can be constructed a number of ways making it easy to obtain a string when needed.


When you create a String object, you are creating a string that cannot be changed. Each time you need an altered version of an existing string, a new string object is created that contains the modifications. This approach is used because fixed, immutable strings can be implemented more efficiently than changeable ones. For those cases in which a modifiable string is desired, there is a companion class to String called StringBuffer.




  1. String ()

The new string represents an empty string.

  1. String (String value)

Initializes the new string with value.

  1. String (char [] value)

  2. String (char [] value, int offset, int length)

  3. String (StringBuffer sb)

  4. String (byte [] value)

  5. String (byte [] value, int offset, int length)

  6. String (byte [] value, int offset, int length, String encoding)

  7. String (byte [] value, String encoding)


Even though Java’s char type uses 16 bits to represent the Unicode character set, the typical format for strings on the internet uses arrays of 8 bit bytes constructed from the ASCII character set. The constructors 6 and 7 initialize a string when given a byte array. In both, the byte-to-character conversion is done by using the default character encoding of the platform.


The contents of the array are copied whenever you create a string object from an array. If you modify the contents of the array after you have created the string, the string will be unchanged.


Note: If you pass null in a constructor, compiler error says that it is ambiguous (for String and StringBuffer versions).




  • char charAt (int index)

  • int compareTo (Object o)

If o is not a string ClassCastException is thrown.

  • int compareTo (String s)

  • int compareToIgnoreCase (String s)

  • String concat (String s)

  • static String copyValueOf (char [] data)

  • static String copyValueOf (char [] data, int offset, int length)

  • boolean endsWith (String s)

Result will be true if “s” is an empty string or equal to this string.

  • boolean equals (Object o)

  • boolean equalsIgnoreCase (String s)

  • byte [] getBytes ()

  • byte [] getBytes (String encoding)

  • void getChars (int srcBegin, int srcEnd, char [] dest, int destBegin)

  • int indexOf (char c)

Returns the first occurrence. If no occurrence then returns –1.

  • int indexOf (char c, int fromIndex)

  • int indexOf (String s)

  • int indexOf (String s, int fromIndex)

  • String intern ()

If the string literal pool already contains a string equal to this string then the string from the pool is returned; otherwise this string object is added to the pool, and a reference to this string is returned.

  • int lastIndexOf (char c)

  • int lastIndexOf (char c, int index)

  • int lastIndexOf (String s)

  • int lastIndexOf (String s, int index)

  • int length ()

  • boolean regionMatches (int toffset, String other, int offset, int length)

  • String replace (char oldChar, char newChar)

Replaces all occurrences of oldChar. If oldChar is not in the string then reference to this string is returned otherwise a new string is returned.

  • boolean startsWith (String prefix)

  • boolean startsWith (String prefix, int offset)

  • String substring (int beginIndex)

  • String substring (int beginIndex, int endIndex)

  • char [] toCharArray ()

  • String toLowerCase ()

  • String toUpperCase ()

  • String trim ()

If this string object represents an empty character sequence, or the first and last characters both have codes greater than ‘\u0020’ ( the space character), then this string is returned.

  • static String valueOf (boolean b)

  • static String valueOf (char c)

  • static String valueOf (char [] data)

  • static String valueOf (char [] data, int offset, int length)

  • static String valueOf (double d)

  • static String valueOf (float f)

  • static String valueOf (int n)

  • static String valueOf (long l)

  • static String valueOf (Object o)

if Object o=null; string.valueOf (o) returns null. But string.valueOf (null); throws NullPointerException.



String conversion and toString()


When Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf(). For the simple types, valueOf() returns a string that contains the human-readable equivalent of the value with which it is called. For objects, valueOf() calls toString() method on the object.

For most arrays, valueOf() returns a rather cryptic string, which indicates that it is an array of some type. For arrays of char, however, a string object is created that contains the characters in the char[].






String buffers are safe for use by multiple threads. These are used by the compiler (append () method) to implement the binary string concatenation operator +.




  • StringBuffer ()

No characters, initial capacity of 16 characters.

  • StringBuffer (int length)

  • StringBuffer (String s)

Initial capacity 16+s.length()





  • StringBuffer append (boolean b)

  • StringBuffer append (char c)

  • StringBuffer append (char [] data)

  • StringBuffer append (char [] data, int offset, int length)

  • StringBuffer append (double d)

  • StringBuffer append (float f)

  • StringBuffer append (int n)

  • StringBuffer append (Object o)

  • StringBuffer append (String s)

This method converts the arguments as if by String.valueOf ().

When the argument is String and is null, then the four characters “null” is appended to this string buffer and  returned.

When the argument is Object and is null, then null is returned.

When the argument is direct null (i.e. sb.append (null)), then a compiler error occurs saying that it is ambiguous (for char [] and String versions).

  • int capacity ()

the capacity is the amount of storage available for newly inserted characters; beyond which an allocation will occur.

  • char charAt (int index)

  • StringBuffer delete (int start, int end)

Removes the sub-string starting at start and extending to the character at index end-1 or to the end of the StringBuffer if no such character exists. Throws StringIndexOutOfBoundsException if start is negative, greater than length(), or greater than end.

  • StringBuffer deleteCharAt (int index)

  • void ensureCapacity (int minimumCapacity)

The new capacity is the larger of the the minimumCapacity argument and twice the old capacity plus 2. If the argument is negative, then the method takes no action and simply returns.

  • void getChars (int srcBegin, int srcEnd, char [] dest, int destBegin)

  • StringBuffer insert (int offset, boolean b)

  • StringBuffer insert (int offset, char c)

  • StringBuffer insert (int offset, char [] data)

  • StringBuffer insert (int offset, char [] data, int offset, int length)

  • StringBuffer insert (int offset, double d)

  • StringBuffer insert (int offset, float f)

  • StringBuffer insert (int offset, int n)

  • StringBuffer insert (int offset, Object o)

  • StringBuffer insert (int offset, String s)

For all versions of insert (), discussion is same as that in append().

  • StringBuffer replace (int start, int end, String s)

  • StringBuffer reverse ()

  • void setCharAt (int index, char c)

  • void setLength (int newLength)

The string buffer is altered to represent a new character sequence, whose length is specified by newLength. The string buffer is truncated or null characters added.

  • String substring (int start, int end)

  • String toString ()



section9-1 | section9-2

Sections : 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11








Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact  


 Number of Pages viewed on this site since January' 2003 : Hit Counter eXTReMe Tracker

For any queries, comments or suggestions, write to me .

This site never compromises your privacy, please read this site's privacy policy.