The 5 Basic Concepts of any Programming Language – Concept #3

by Trevor Page on July 2, 2012

Hi everyone,

Without further delay, let’s jump right into our 3rd basic concept of programming languages:

  1. Variables
  2. Control Structures
  3. Data Structures
  4. Syntax
  5. Tools

Data structures, what are they, why are they useful?  Well, let’s turn to a quick definition from wiki:

In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Okay, that definition is a little more down to earth.  But, there is a lot of ‘meat’ behind data structures.  Let me try to explain the point of data structures by giving you an example… Let’s use a list of contacts as the example!  You probably have a list of contacts somewhere in your life, whether it’s in one of your email programs or an address book in a kitchen drawer.  There are a bunch of contacts, and that list of contacts could grow (or shrink) at any given moment.  If you were to try and represent all those contacts as variables in a computer program, how would you do it?  Well, there’s a right way, and a wrong way.  For  the purposes of our example, let’s say we need to keep track of 10 contacts.

First, the WRONG WAY:

If we need to store 10 contacts, we would probably define 10 variables, right?

String contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10;
contact1 = "John Smith (john.smith@someCompany.com)";
contact2 = "Jane Smith (jane.smith@someCompany.com)";
contact3 = //... etc.

In the world of programming, this is just a terrible way of trying to store 10 different variables.  This is because of two main reasons:

  1. The sheer amount of text that you’ll need to write in your program
    • Sure, right now we only have 10 contacts, so it’s not too bad, but what if we had 1,000 contacts!  Imagine typing that out a thousand times!  Forget about it!
  2. The flexibility of code
    • If we need to add another contact, we wouldn’t be able to do it without manually editing our code. We would have to go into our code, physically write out contact11, and then try to store whatever information is needed into the new variable. This is just crazy talk!

So, what is the RIGHT WAY?

I’m glad you asked, it’s a data structure of course!

In this case, we have a list of contacts, and with Java there is a data structure called a List!  Okay, so what does it look like?  Here’s the code:

List contacts = new ArrayList();

Again, let’s not worry about all those symbols and confusing brackets and semi-colon, we’ll cover that later.  All I need you to understand right now, is that there is a way to store a bunch of contacts into something called a data structure (in the case of our example, a List).  Okay, so, what’s so great about a List?  Well, for one thing, you can add and remove things from a list with ease.  So if you started with 10 contacts, it’s a piece of cake to add another contact to the list, how you ask?  Just like this:

contacts.add("John Smith (john.smith@someCompany.com)");
contacts.add("Jane Smith (jane.smith@someCompany.com)");

Voila, we’ve added another contact to our contacts list!  So, you may be saying, the right way looks like just as much typing as the wrong way.  You’ve got a point, but the main difference is that with the first approach, you had to “create” 10 unique variables (i.e. contact1, contact2, contact3, contact4…), but with the second approach, we only created 1 variable (contacts).  Because we only had to create 1 variable, and because we can say contacts.add(someRandomContact), means that our code is much more flexible and dynamic.  When I say dynamic, I mean that the outcomes of the program can change depending on what variables you give to it.  That’s really the key to using data structures!   We want our code to be as dynamic as possible, we want it to be able to handle a bunch of situations without having to write more and more code as the days go by.

In essence, a data structure is just a way to get around having to create tons of variables.  Now, there are a bunch of other data structures in Java, but I’ll just quickly touch on the ones we use most often when programming.  The next one I’ll talk about is the HashMap.  This doesn’t sound as straightforward as our last data structure (the List), but it’s really just a fancy name for a fairly simple concept.  So, what is a hash map?  Here’s a visual representation:

“Honda” -> “Civic”, “Prelude”
“Toyota” -> “Corolla”, “Celica”, “Rav4″
“Ford” -> “Focus”, “Mustang”
“Audi” -> “R8″

What you see here, is the make of a car on the left, which points to different models of that make.  This is known in the programming world as a Key/Value pair.  The key in this case, is the Make of the car (Toyota, Honda, Ford, Audi), the value in the case, is the model (Civic, Corolla, Focus, R8).  That is how a HashMap works, and it’s just another data structure!  This concept is use all over the place on the web.  For example, if you have ever filled out a quote for automobile insurance online, you’ve probably had to choose the make and model of your car.  I bet you that information was presented on screen by using a HashMap (just like I showed you above)!  Because when you select “Toyota” from one drop down list, the second drop down list will need to change its contents to only show models of cars made by Toyota.  Then if you change the first drop down to Honda, that second drop down list will change again to show cars made by Honda.

So how about I show you what a HashMap looks like in Java code?

Map<String, List<String>> cars = new HashMap<String, List<String>>();
cars.put("Toyota", new ArrayList(Arrays.asList("Corolla", "Celica", "Rav4")));

Wow, there’s a lot of stuff going on in there, but don’t be intimidated, all you need to understand right now is the concept that this HashMap data structure stores key/value pairs.  In our case, a make is the key, and a List of models is our value.  Now, that’s kind of interesting, did you see what we did there?  I just said that we have a HashMap data structure, and inside the HashMap we are storing a List (which is another data structure).  This is done on a fairly regular basis in the programming world.  We can use one data structure inside of another!  So, if the point of data structures is to help us minimize the number of variables we need to create, then we are really saving ourselves the hassle of creating a lot of variables!

Okay, so let’s sum up everything we’ve talked about.  A data structure is a way of storing and organizing data in such a way that it can be used efficiently.  The point is to avoid creating crap loads of variables people!  Java allows us to do this by using Lists and HashMaps (as well as many other data structures, but these two are the most common).  These two data structures can grow and shrink without you having to worry about coding all of that ‘behind the scenes’ stuff.  When you add to a List, the new element is there and is usable (like magic), when you remove from the List, that element is now gone (poof!).  This saves us from creating and deleting variables in our code manually.

Alright, that about sums up what I need you to know for our next section: syntax.  In my opinion as a veteran programmer,  syntax is the one thing that will discourage you from learning to program.  It takes practice and patience to understand, but I promise you, it will become second nature in time.  So please be patient, and more importantly, be excited to learn about syntax in Java, because once you get the hang of it, you’ll be on your way to programming like a champ!  If you’re eager to learn right now, follow this to the next Java tutorial.

Plenty of Ways to Learn

  • COMMUNITY – Learning with a community is the best way to ensure accountability and support when you need help. Coders’ Campus is a brand new community that is dedicated to new programmers like you!
  • BLOGS – If you feel like you enjoy the way this information is laid out in blog format, then I invite you to click on the “next” button below.
  • BOOKS – If you feel like you are better at learning from a book, then I invite you to check out my ebook via http://javapdf.org. This eBook is organized, and packed full of easy to follow tutorials and videos.
  • VIDEOS – If you feel like you prefer learning by watching Videos, then check out the Java Video Tutorials website. It’s full of fun and easy to follow video tutorials. So far the students enrolled in this video course have rated it 9.5/10!
  • PODCASTS – Finally, if you prefer to learn by just listening, then there’s also a podcast available on iTunes via How to Program with Java Podcast.




{ 18 comments… read them below or add one }

Leave a Comment

Powered by sweet Captcha

{ 1 trackback }

Previous post:

Next post: