GhostSnyper
Active Members
So I have been looking into different ways to optimize and tune my code, when I ran into an article about strings. Now there are two types of objects in java, mutable objects, and immutable object; rather, changeable and unchangeable objects.
Unfortunately, a String is among those immutable objects. It cannot change once it has been created. Now you say, "Well, how is that possible? It's easy to set a String to a new value!" Of course that it is easy, but that is because the String class handles all the low level operations to 'modify' the String.
For example, let's say we have a simple class that takes a name input
This class, from our point of view, is simple, and gets the job done quick. However, if we were to modify this to serve a large number of users concurrently, we'd run into a problem. When we do things, such as modify a string, we are not actually modifying it. We are creating two objects to replace it. When we used the line:
Since the String is immutable, the only thing we can do is replace it with another string object with the same name, right? Exactly, so what that line is actually translated to is.
To better explain, to modify the username, we create a new StringBuilder object, and add the username, with what we're appending. Then, it creates a new String with the contents of the StringBuilder( using the toString method).
With a small application like the above, using the original code would be sufficient, and we'd not see a noticeable difference. However, if this were modified, and were used on a web server serving thousands of clients simultaneously, there would be a lot of overhead to be creating all these objects. Fortunately, The if we imitate the process to modifying a String on the get go, we can avoid the extra overhead of setting a string twice.
Let's take a look at a slightly modified version of our RegisterUser class:
While this simple implementation is still imperfect, there is a large improvement in the long run. Sure, only creating 3 total objects instead of 4 may not seem like much, but if we were to use this on a much larger scale where a given string may be modified hundreds of times, we save a vast amount of memory and CPU cycles by using our own StringBuilder to manipulate Strings.
[/QUOTE]
Unfortunately, a String is among those immutable objects. It cannot change once it has been created. Now you say, "Well, how is that possible? It's easy to set a String to a new value!" Of course that it is easy, but that is because the String class handles all the low level operations to 'modify' the String.
For example, let's say we have a simple class that takes a name input
Code:
import java.io.Serializable;
import java.util.Scanner;
public class RegisterUser implements Serializable {
private static transient Scanner iReader;
private static transient boolean nameOK = false;
private static String username = null;
public static void main (String args[]) {
println("Hello, Welcome to the new user registration.");
println("Fortunately, all we need at this point is your name.");
iReader = new Scanner(System.in);
while (!nameOK) {
println("If you please, what is your first name?");
username = formatCase(iReader.nextLine());
println("Is " +username+ " correct? if it is, please enter Y");
if (scanner.nextLine().toString().equalsIgnoreCase("y"))
nameOK = true;
}
nameOK = false;
while (!nameOK) {
println("Thank you. Now, what is your last name?");
String lastName = formatCase(iReader.nextLine());
println("Is " +lastName+ " spelled right? if it is, please enter Y");
if (scanner.nextLine().toString().equalsIgnoreCase("y")) {
nameOK = true;
username += " " + lastName;
}
}
println("Thank you very much, "
+username+ ", your username has successfully been registered.");
}
static String formatCase(String s) {
return (s.length()>0) ?
Character.toUpperCase(s.charAt(0))+s.substring(1) : s;
}
static void println(String s) {
System.out.println(s);
}
}
we're actually not just saying "put these together, let's be quick."username += " " + lastName;
Since the String is immutable, the only thing we can do is replace it with another string object with the same name, right? Exactly, so what that line is actually translated to is.
Code:
username = new StringBuilder().append(username).append(" ").append("lastName").toString();
With a small application like the above, using the original code would be sufficient, and we'd not see a noticeable difference. However, if this were modified, and were used on a web server serving thousands of clients simultaneously, there would be a lot of overhead to be creating all these objects. Fortunately, The if we imitate the process to modifying a String on the get go, we can avoid the extra overhead of setting a string twice.
Let's take a look at a slightly modified version of our RegisterUser class:
Code:
import java.io.Serializable;
import java.util.Scanner;
public class RegisterUser implements Serializable {
private static transient Scanner iReader;
private static transient boolean nameOK = false;
private static transient StringBuilder nBuilder= null;
private static String username = null;
public static void main (String args[]) {
println("Hello, Welcome to the new user registration.");
println("Fortunately, all we need at this point is your name.");
iReader = new Scanner(System.in);
while (!nameOK) {
println("If you please, what is your first name?");
nBuilder = new
StringBuilder().append(formatCase(iReader.nextLine()));
println("Is " +nBuilder.toString()+ " correct? if it is, please enter Y");
if (scanner.nextLine().toString().equalsIgnoreCase("y"))
nameOK = true;
}
nameOK = false;
while (!nameOK) {
println("Thank you. Now, what is your last name?");
String lastName = formatCase(iReader.nextLine());
println("Is " +lastName+ " spelled right? if it is, please enter Y");
if (scanner.nextLine().toString().equalsIgnoreCase("y")) {
nameOK = true;
nBuilder.append( " ").append(lastName);
}
}
username = nBuilder.toString();
nBuilder = null;
println("Thank you very much, "
+username+ ", your username has successfully been registered.");
}
static String formatCase(String s) {
return (s.length()>0) ?
Character.toUpperCase(s.charAt(0))+s.substring(1) : s;
}
static void println(String s) {
System.out.println(s);
}
}
[/QUOTE]