Sunday, 5 January 2014

String & String Buffer

String

         In Java, Strings are immutable objects.

                      String s = new String();

This line of code creates a new object of class String, and assigns it to the reference variable s

s = "abcdef";
This line give the String a value.


String s = new String("abcdef");
More efficient shortcut.


Because you'll use strings all the time, you can even say this
String s = "abcdef";


Now let's say that you want a second reference to the String object referred to by s:
String s2 = s; // refer s2 to the same String as s


So far so good. Now immutability?
Once you have assigned a String a value, that value can never change.


String s = ”abc”;
s = ”xyz”;
Confused!    
String object is immutable, its reference variable is not.



How String objects are immutable?
String s = ”abcde”;
s = s.concat(“ xyz”);

We have 3 String objects here
1. “abcde”
2. “ xyz”
3. “abcde xyz”



Other Example:

String x = "Java";
x.concat(" Rules!");
System.out.println("x = " + x);  


 // the output is
"x = Java"



String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2)

//Output
spring winter  spring summer

What is the output and  how many String objects and how many
reference variables were created?
8 objects and 2 reference variables



Important Facts About Strings and Memory

  • JVM sets aside a special area of memory called the "String constant pool."
  • When the compiler encounters a String literal, it checks the pool to see if an identical String already exists
  • If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created
why making String objects immutable is such a good idea?
If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String's value.


Important Methods in the String Class

  •  charAt() Returns the character located at the specified index
  •  concat() Appends one String to the end of another ( "+" also works)
  •  equalsIgnoreCase()Determines the equality of two Strings, ignoring case
  •  length()Returns the number of characters in a String
  •  replace() Replaces occurrences of a character with a new character
  •  substring()Returns a part of a String
  •  toLowerCase() Returns a String with uppercase characters converted
  •  toString()Returns the value of a String
  •  toUpperCase()Returns a String with lowercase characters converted
  •  trim()Removes whitespace from the ends of a String

 StringBuffer

The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters

StringBuffer vs. StringBuilder
  • StringBuilder has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe
  • In other words, its methods are not synchronized
  • StringBuilder will run faster
Using StringBuffer and StringBuilder

StringBuffer sb = new StringBuffer("abc");
sb.append("def");
System.out.println("sb = " + sb);    // output is "sb = abcdef"


Important Methods in the StringBuffer and StringBuilder Classes

public StringBuffer append(String s)
public StringBuffer reverse()
public delete(int start, int end)
public insert(int offset, int i)
replace(int start, int end, String str)