Skip to main content
  1. Posts/

A Couple Pointers

·375 words·2 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.

This is an article I wrote a while ago, but is still relevant to many languages still in use. Figured I’d repost it to this blog.


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, 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 its 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 our code 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!

Related

Don't fear the frustration

I’ve always been a little more tenacious than most people I know, willing to spend the time to flail before making the breakthrough that gets the results I’m looking for.1 I think part of that has to do with the fact that I do most of the flailing in private, at a computer where no one can point out my failures or see what I’m working on before it’s ready. But as I’ve taught other people programming over the years, I’m always surprised at how quickly they throw in the towel on problems that I know are solvable.

Ask Away: Judging Your Skills

I recently got an email from one of my tutoring students about how he had finished setting up an e-commerce site for his Dad’s salsa business, which was a pretty big accomplishment. But there were two things in the email that I took issue with. One was that he thought it was weird that another web dev shop had asked for $3,000 to set up a shopping cart in Spotify and the other was that he felt that using an off the shelf solution (BigCommerce) was a cop out and he should have been able to build it himself. I think everyone’s felt both those things at some point–I know I have–but I sent him this in response:

Security topics every web developer needs to know

·2344 words·12 mins
When it comes to web application security, there are a class of things that every web developer should know. Trust me, you need to learn these and you’ll use them on every project you do. Interviewers ask about this stuff, so learn about them and how to handle them in whatever language you’re using.