Java String Pool

What will the following lines of code print?

1  String a = "abc";
2  String b = "abc";
3  System.out.println(a == b);
4  String c = new String("abc");
5  String d = new String("abc");
6  System.out.println(c == d);

In order to test for equality between two objects, you must use the equals method.  Otherwise, if you test the equality between two variables that point to objects, you’re comparing the value it stores; in this case the address that reference the objects.  As a result, both println statements should both return false, right?  Why does the first println statement (line 3) print true?

It turns out that variables a and b both point to the same object.  This is because both variables reference string literal objects.  A string literal is instead stored in a special area of the Java PermGen heap space called the String Pool.  The String Pool acts as a cache for string literals; If you create a string literal that already exists in the String Pool, Java is smart enough to use the existing reference.

On line 1, when I set the variable a to the literal “abc”, it will store that value in the String Pool.  When I do the same again for variable b, on line 2, it will find the same value in the String Pool and use the reference to the existing object.  Not only does this save memory, but the values of the variables a and b are both equal since they reference the same object.

On the other hand, if we create a String using the new operator (line 4), we will first find the “abc” reference that exists in the String Pool.  This reference will be passed into the String constructor to instantiate a completely new object that now lives in the Java heap space.  After we create two instances of the String object using the new operator (line 5), we have two completely separate instances of the String object.  Therefore the variables c and d reference two completely separate variables.

As you can see, it almost always never makes sense to use the new String(String) constructor.  If for whatever reason you must use this constructor, you may use the String.intern() method to place the object in the String Pool. This method will then return the  reference to the object that lives in the String Pool.

Given what you have just learned, what will the following lines of code print?

System.out.println(c.intern() == d.intern());
System.out.println("a" + "bc" == "ab" + "c");
System.out.println("abcd" == "abc" + "d");

PHP Frameworks

I’ve been coding in PHP off and on for years now.  Each project I’ve done thus far has always been by hand.  Doing the architecture of your site by hand is the easiest way to introduce bugs and end up with sloppy code.   For the first time I’ve started a project where I used a framework in PHP.  My framework? Codeigniter.  I like it’s simplicity, MFC design and how it’s not like Rails.  I thought Rails was a bit too complicated and involved hacking the internal code way too much to get anything done, but that’s a whole other rant.

Anyway, the project I’m working on?  Just a twitter clone.  Why?  For the sake of learning, that’s all.  It’s actually for a project in school.  The idea is to implement this idea in something that I’m already familiar with (PHP) and then implement it again in another language I’m not familiar with at all.  After I’m doing with the first part I want to implement it in Python using the Django framework.  Python has always been one of those languages I’ve been meaning to learn.  In fact it seems I try to learn it every summer, but I just don’t seem to have the dedication to sit in doors learning it.

Regardless, I hope to use this knowledge in my professional life.  The last PHP project I worked on was stressful just because of some of the design decisisons I made (Doing a half-assed MVC implementation…didn’t do the best job of dividing up the view and controller).  However, I’ve learned from those mistakes and I might be taking up a new project at work where I’ll probably be using this framework.

Report back from SHDH22

Since moving to Camarillo for college in August, I missed both SHDH20 and SHDH21 due to the increased distance between the Bay Area and where I live. However, since I’m back in San Luis Obispo for winter break, I was able to attend SHDH22. Instead of spending my time socializing and learning, I actually came with an agenda of things to do.

A few months back I registered the domain http://picturesofpeopletakingpictures.net because my friends thought it would be funny. (Alcohol was also involved and it seemed like a good idea at the time). I still don’t know what I want to do with it, but I put wrote up a small script using Rails that queries flickr and displays a random picture tagged “picturesofpeopletakingpictures.” It turned out to actually be a huge pain in the ass because the flickr gem I used had their own API key built in. Once I hacked together a fix for using my own, I got it working pretty quickly. When I uploaded it to my web server, nothing worked. Duh I forgot to freeze the flickr gem! Nope that wasn’t the problem. Apparently you have to use a folder structure like ~/flickr-ver_num/lib/flickr.rb. The library had ~/flickr-ver_num/flickr.rb. After struggling with that for at least 45 minutes, I had it up and running. Anyways, if anyone has suggestions of something better, leave a comment.

I started on a project that could turn into a DevHouse competition. More on this later if it actually happens.

Lastly, I started looking at the Android Java Framework by Google for Cell Phones. I mean why not? Google has some pretty good documentation and tutorials that I was able to follow and I end up writing a Hello World program!

Ruby Iterators

I’m sure like most people, I started learning Rails by reading about….Rails. It would seem logical to learn Rails by reading about Rails, right? I think a better way of learning about Rails is to first master Ruby and THEN learn the Rails framework for Ruby. This approach has helped me debug some of those error messages that seemed so cryptic at first. Recently I picked up “Programming Ruby” by The Pragmatic Programmers. Reading it I found out that some of methods that I thought were part of the Rails framework actually belong to Ruby.

One of the things I wanted to focus on are the differences between three different types of iterators in Ruby that are commonly used in Rails: find, each, and collect. Let’s first start by asking what is an iterator? An iterator goes through each element in an array or hash table and let’s us perform an action using the individual element. Let’s check out the differences between these iterators.

find

Find is a bit misleading. The classical definition of a find method would be to return the index of the element (or perhaps the entire element itself) that matches a string. The find iterator method in ruby will instead compare each element using some comparison operator (<, >, ==, etc.) and based off of the boolean result (true or false), it will return the first matching value. For example say we have an array called a:

a = [ 1, 2, 3, 4, 5 ]

Using this array we want to find the first even value. Our code would look something like this:

a.find { |n| n % 2 == 0 }

Since 2 is the first even number, that would be the result.

each

Each is the simplest iterator method of the three. It simply traverses all elements in the array and returns each one individually. So if we want to print all lements of the array a, it would look something like this:

a.each { |n| print n }

The result would look like 12345

collect

Collect, or also known as map, is pretty much the same as each, except that collect returns an array of values. For this example, we’ll increment each element in array a and save the new element in b.

b = a.collect { |n| n + 1 }

This returns the array [2, 3, 4, 5, 6]

I hope this gives some insight on how Rails uses these methods.

In need of WD-40

I had some free time today, so I decided to start on a proof-of-concept for my OpenID + Microformats idea. Holy $^#@ am I rusty with PHP. It’s now been about a year since I last done any sort of programming with it and I find myself having to reference the books I was reading when I first began learning PHP 2 years ago. I really need to start fitting time into my schedule for working on programming projects because I’m finding myself having to look up information that has been lost over time.

On another note, I REALLY need to sit down and learn regular expressions. I’m tired of having to google search this sort of thing. I heard O’Reilly’s book on Regular Expressions is a good resource; perhaps I should pick it up. I’m going to have to learn them anyways, the sooner the better I suppose.

Afterthought: Perhaps instead of re-learning everything, I should just learn the PEAR library instead and fill in the gaps along the way. Anyways, I guess I know what books I’ll be ordering soon.

OpenID + uF epiphany

UPDATE: After an email discussion with Adam Darowski, I revised this post to make it a bit more user-friendly.

I think I might be going about this idea of consolidating your online identity with the OpenID simple registration extension and microformats wrong. I found this microformat library for PHP; It works by by retrieving the microformatted data (in this case hCard) by simply feeding it a URL and it then saves the hCard data for future manipulation. I’m starting to realize this same practice may be another way to consolidate your online identity with OpenID.

Before we get ahead of ourselves, let’s see how the applicable portion of the OpenID protocol works. The first step is to supply our OpenID URL to the consumer site and hit “submit.” For example, I would enter “nickpeters.net” as my OpenID URL to the consumer site ma.gnolia. What is a consumer site exactly? According to the OpenID Specification, the consumer is:

A web service that wants proof that the End User owns the Claimed Identifier.

Pretty much it means a site that wants to find out whether or not we are who we say we are by first asking for our OpenID URL.

After we hit the “Submit” button, an internal script on the consumer site will redirect itself to the given URL and look for two link tags in the head of the given HTML document. For the sake of simplicity, we’re only going to discuss the one of them: rel=”openid.server.” The href value of this link tag is going to be the script that actually authenticates you by asking you for the username and password you use on all OpenID-enabled sites. This practice is useful because you can offload the authentication process to a site like Vox or livejournal instead of setting up a server on your own server. This means I can enter the URL for the livejournal authentication script or a script hosted on my site. It also means your username and password isn’t sent to the consumer site and is only sent to the specified script.

That should be enough information for now, so let’s see how hCards can fit into this. Let’s say we log in to our OpenID consumer site (ma.gnolia) and go through the login process as normal. While the consumer site looks for the and OpenID.server URL in the head of our page, it would also look for some sort of clue as to the location of your hCard information. Perhaps this “clue” could follow in the steps of the existing OpenID protocol and also be a link tag, such as rel=”hcard” in the head of the document. From there, the consumer-site script can retrieve the hCard location by looking at the href value, properly redirect itself, and retrieve your information (Perhaps even using the hKit library).

The pros of this is that you can add as much or as little information as you want about yourself in your hCard. The simple registration data fields are very limited and there maybe some information in there that you don’t want to be shared (such as email address).

The cons is that it requires more administrative overhead. You’ll have to edit the header information of your site. You may not even be able to do this on all OpenID identifiers (such as ones on livejournal) because you don’t have permission to edit the header information.

Right now there are discussions going on in the microformat community about authoritative hCards that could possibly guide this idea in the right direction. I will continue to think about this and would appreciate any ideas from others.

Dev House 13

This past weekend I experienced my first Super Happy Dev-House convention/get together. I must say that never before have I met so many people that put me to shame. With this said however, I have learned more about online technologies from this than I have from any online tutorial or book. One of the biggest eye-openers for me was that I felt I was investing too much of my time learning PHP when instead I should be concentrating on learning Javascript-based technologies (most notably AJAX) and maybe Ruby/Ruby on Rails. Being around so many developers at once opened my eyes as to what is possible and how to get there.

With everyone working on their projects, it gave me the opportunity to begin filling in the gaps of my knowledge. I began researching new (to me at least) concepts, ideas, etc. Some examples are microformats, JQuery, and JSON. On top of that, I began going through AJAX tutorials, something I should’ve started a long time ago.

One of the unique things about being around this many people with similar interests are the conversations had throughout the night. Most of my conversations focused on web technologies: something I never have the opportunity to talk about at home. Topics included Ruby on Rails, the manageability of PHP in large projects, peoples’ current projects (including personal and business related). Most of all, I was able to network with people in the industry. There were a variety of people including CTOs, bloggers, and even someone from google. Hollywood has celebrities, but the bay area has eCelebrities.

This event has inspired me to form this sort of community back at home, but will it really be worth it? If I plan on moving in January (oh please make it happen), my efforts will be wasted in San Luis Obispo. I feel I should instead wait till I move to Camarillo for this sort of thing.

Anyways, can’t wait for Dev House 14!