Let's take a look at how the string objects are created and understand the immutability.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.string; | |
public class StringImmutable { | |
public static void main(String[] args) { | |
String st = new String("OCJP"); | |
String st2 = st; | |
st = st.concat(" - Java"); | |
System.out.println("st variable ==> " + st); | |
System.out.println("st2 variable ==> " + st2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.string; | |
public class StringImmutable { | |
public static void main(String[] args) { | |
String book = new String("Book"); | |
book.concat(" - Java"); | |
book.upperCase(); | |
System.out.println("book variable ==> " + book); // it prints "Book" | |
} | |
} |
As you can see it prints "Book" because we don't refer the new String created to "Java - Book", so the object is in the heap however nobody refers to it.
Commom methods
Let's see common methods in action.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.string; | |
public class StringCommon { | |
public static void main(String[] args) { | |
String example = "Java Example "; | |
System.out.println("charAt() ==> " + example.charAt(0)); // it prints J | |
System.out.println("concat() ==> " + example.concat(" -1")); // it prints Java Example - 1 | |
System.out.println("equalsIgnoreCase() ==> " + example.equalsIgnoreCase("java example ")); // it prints true | |
System.out.println("length() ==> " + example.length()); // it prints 14 | |
System.out.println("replace() ==> " + example.replace("a","@")); // it prints J@v@ Ex@mple | |
System.out.println("substring() ==> " + example.substring(0)); // it prints Java Example | |
System.out.println("substring() ==> " + example.substring(0,4)); // it prints Java | |
System.out.println("toLowerCase() ==> " + example.toLowerCase()); // it prints java example | |
System.out.println("toString() ==> " + example.toString()); // it prints Java Example | |
System.out.println("toUpperCase() ==> " + example.toUpperCase()); // it prints JAVA EXAMPLE | |
System.out.println("trim() ==> " + example.trim()); // no space at the end... it prints Java Example | |
} | |
} |
Pool of String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.string; | |
public class StringPool { | |
public static void main(String[] args) { | |
String t = "java"; // Creates an object in a heap and puts it in a pool of String | |
String t1 = new String("java"); //Creates a new object in a heap and puts it in a pool of String | |
System.out.println(t == t1); // prints false | |
String s = "ocjp"; | |
String s1 = "ocjp"; // Retrieve the string ocjp from the pool | |
System.out.println(s == s1); // prints true | |
} | |
} |
Also you can use the method intern() (String class) this way we can get the string from the pool if the string pass using the method equals().
StringBuffer and StringBuilder friends of memory
Both classes are intented to help you handle Strings without wasting memory, as we mentioned Strings are immutable and if we have to handle lots and lots of them StringBuffer and StringBuilder will help you.
The unique difference between StringBuffer and StringBuilder is StringBuilder is not thread-safe which means its methods aren't synchronized, as you can imagine StringBuilder is faster than StringBuffer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.ocjp.string; | |
public class StringBufferBuilder { | |
public static void main(String[] args) { | |
StringBuffer sb = new StringBuffer(); | |
sb.append("Java Test - "); | |
sb.append("OCJP"); | |
System.out.println(sb); // Prints Java Test - OCJP | |
StringBuffer sd = new StringBuffer("0123456789"); | |
sd.delete(4, 6); | |
System.out.println(sd); //Prints 01236789 | |
StringBuffer si = new StringBuffer("0123456789"); | |
si.insert(4, "----"); | |
System.out.println(si); //Prints 0123----46789 | |
StringBuffer sr = new StringBuffer("0123456789"); | |
sr.reverse(); | |
System.out.println(sr); //Prints 987654321 | |
StringBuilder builder = new StringBuilder("abcdef"); | |
builder.append("ghij").reverse().delete(5,20); | |
System.out.println(builder); // Prints jihgf | |
} | |
} |
The most import thing here is StringBuffer and StringBuilder are not immutable like String and the StringBuffer contains the synchronized methods;
No comments:
Post a Comment