FirstClown

firstclown at firstclown.us

Archive for May, 2008

Java Tips & Tricks: Unchangeable String

Unchangeable String

One of the fundamental data types in Java is the String. In fact, it's used so often that most people don't even think much about what a String is or how it works. But a String Object works like no other object in Java in two important ways; immutability and internment.

You Can't Touch This

Immutability means that an object can't be changed. Ever. You can't modify any of it's values in any way.

While this might not sound very useful, it actually allows Java to optimize how it handles Strings -- which is important since they're used so much in the language -- and brings a sense of security to using Strings. As we saw last week, when Objects are changeable, weird side effects can pop up when one variable changes an Object and all the other variables see the change and don't know what happened.

Strings sidestep this issue by never allowing you to change a String in place. In programming parlance, this means Strings never have side effects. So in this operation:

String a = "hello";
String b = a;

b = b.toUpperCase();

a and b are now pointing to two different String Objects. a is pointing to the original String of "hello" while b is pointing to a copy that's all uppercase of "HELLO". Even though a and b were pointing to the same String originally, the method toUpperCase() makes a copy and assigns the copy to b. This leaves a untouched. [1]

This property allows Strings to do another cool trick. They can Intern.

Lowly Paid College Students?

Interning is a method where you cache objects in memory to speed up creation of those objects. What that means is, all String literals are interned, and if you define the same literal, it will actually be the same Object. A little illustration:

String a = "hello";
String b = "hello";

It looks like a and b are pointing to two different Objects, but they're not. Here's why.

When Java sees the "hello" for variable a, it creates a new String Object with the value and assigns the reference to a. It then saves that Object in a table in memory, also called interning.

Then Java sees that b is getting a String with the value "hello". It checks it's intern table and sees it already has that Object created. Since Strings can't be changed, it doesn't hurt to just give that Object reference to b too, and so it does. Even though the programmer didn't explicitly say so, a and b are now pointing to the same Object in memory.

In fact, Java is so good at this, it'll assign to same Object to these variables too:

String c = "hel" +
              "lo";
String d = "h" + "e" + "l" + "l" + "o";

String is the only Class in Java that does interning in the JVM.

Next week we'll talk about how to use this knowledge to build better programs. (Hint: it has to do with memory management.)

[1]Strings aren't the only Objects that do this in Java. Can you think of any others?

Agile Project Management Training

In June, I'm going to be going to Columbus for Agile Project Management Training given by ASPE Tech. This is something that I've wanted to do for a while. I've done a lot of reading on Agile practices and implemented them myself for personal projects and everything just seems to click with me. I get those slap-in-the-forehead-type moments and wonder why I haven't been doing this all the time.

So now I'm going to do some Career Investment and try and get some training under my belt for the project management side of things. I'd like to eventually go to their ScrumMaster Certification, but that might have to wait until next year.

Next step, using it on the job.

Music Video Via a Mac

I don't usually post videos on my blog but ... wow.

Via 37Signals.

Song via the bird and the bee

Java Tips & Tricks: A Couple Pointers

A Couple Pointers

Many of you may have learned the following in Java 101, but I thought I'd repeat it just in case.

Most of the variables you'll use in Java are going to be Objects. So in the following code, we're creating three Objects:

BigDecimal number = new BigDecimal("9.5");
String welcome = "Hello";
Connection conn = DriverManager.getConnection(props);

The first case is using the standard constructor of a Class to create an Object, the second is creating a String via a String literal [1], and the third is creating an Object via a Factory (an Object that creates other Objects). All of these create an Object and puts it in the variable to the left.

Except I'm lying. Do you know where I'm lying?

Little Errand Boys

number, welcome, and conn aren't holding Objects, they're holding references to Objects. These are called pointers because they "point" to an Object and don't actually do anything except pass messages along to the Object they're pointing at. They're basically the Objects' errand boys. When you say something like:

number.toString();

You're basically saying "Hey, errand boy. Go ask your Object what it's string value is and then let me know." That becomes important when you consider that you can do this:

BigDecimal sameNumber = number;

sameNumber is now pointing to the exact same Object as number. That means that one Object has two errand boys. If I tell one errand boy to change the Object, then the other errand boy sees the change too, because it's the same Object! This can be confusing if you're not expecting it.

Cha-Cha-Cha-Changes

This has important repercussions when looking at methods calls. For instance:

public static void addItem(List listToAdd){
  listToAdd.add("Hello");
}

List group = new ArrayList();
System.out.println(group.isEmpty());  // Prints true

addItem(group);

System.out.println(group.isEmpty());  // Prints false

So even though group didn't add anything to the List Object, listToAdd did. Since they point to the same Object, both errand boys see the change. This is very important to remember when coding in Java.

Remember: Multiple variables can point to the same Object, so be careful changing them!

[1]Strings and arrays are the only Objects that can be created via a literal in our current version of Java.

DRM Sucks, Now with Proof

It seems sometimes people think I'm being overly critical of DRM from Apple or Amazon. I say that you're purchasing something you don't own and the minute the companies don't care anymore, you're screwed and all of your music or books or whatever won't be yours anymore. In that way, it's never yours to begin with and you're really just renting it for a couple years.

But people say, "Come on. These are big companies. They're not just going to leave everyone hanging that bought their music. I paid money for this and they can't just up and take it away."

Microsoft will. Do companies get any bigger than that?

Thanks to Alan Phillips for the link.

FirstClown is powered by WordPress
Entries (RSS) and Comments (RSS).