First off, I’d like to say that I’m writing these preliminary posts in a way that I’ll assume you have very little knowledge in programming. I want this content to provide anyone “walking in off the street” the knowledge to be able to write their first program with the Java programming language with as little pain as possible.
So, let’s get started with our first topic: The 5 basic concepts of any programming language. You might say, “Why are we talking about any programming language? I thought this was about Java”. Well, I’ve found that it’s important to remember that a lot of programming languages are very similar, and knowing what’s common between all programming languages will help you transition into any other programming language if you need to! For example, with the Java programming knowledge I had obtained, it took me less than a month to learn how to program in a language called Objective C (which is used for iPhone apps). That’s powerful stuff!
Before we start learning: if you’re someone who also enjoys learning by watching videos, then I have the perfect deal for you. You can get access to over 50 hours of video tutorials (plus exercise files, assignments, quizzes and a private Facebook community of like minded programmers) for free for 30 days. You can click here to learn more about the Java courses.
So here are the 5 basic concepts of any programming language:
I recognize that these words probably look foreign to you, but don’t worry, I’ll do my very best at taking the mystery out of them. Now, there’s a lot to say about each of these 5 concepts, so for today’s post I’ll only be talked about item #1, variables!
What is a variable?
Variables are the backbone of any program, and thus the backbone of any programming language. I like to start off by defining what we’re about to learn, so, Wiki defines a variable as follows:
Okay, well, that’s kind of cryptic. To me, a variable is simply a way to store some sort of information for later use, and we can retrieve this information by referring to a “word” that will describe this information.
For example, let’s say you come to my website www.howtoprogramwithjava.com and the first thing I want to do, is ask you what your name is (so that I can greet you in a nice way the next time you visit my website). I would put a little text box on the screen that asks you what your name is… that text box would represent a variable! Let’s say I called that text box ‘yourName’, that would be the symbolic name (or “word”) for your variable (as described from our wiki definition above).
So now, when you type your name into the text box, that information would be stored in a variable called ‘yourName’. I would then be able to come back and say “What value does the variable ‘yourName’ contain?”, and the program would tell me whatever it was your typed into that text box.
This concept is extremely powerful in programming and is used constantly. It is what makes Facebook and Twitter work, it’s what makes paying your bills via your online bank work, it’s what allows you to place a bid on eBay. Variables make the programming world go ’round.
Now, if we want to get more specific, when it comes to the Java programming language, variables have different types. Brace yourself here, as I’m going to try to confuse you by explaining an important concept in three sentences. If I were to be storing your name in a variable, that type would be a String. Or, let’s say I also wanted to store your age, that type would be stored as an Integer. Or let’s say I wanted to store how much money you make in a year, that type would be stored as a Double.
What the heck are String, Integer and Double?
Excellent question! In Java, the programming language wants to know what kind of information you are going to be storing in a variable. This is because Java is a strongly typed language. I could teach you about what the difference is between a strongly typed language and a weakly typed language, but that will likely bore you right now, so let’s just focus on what a type is in Java and why it’s important.
Typing in Java, allows the programming language to know with absolute certainty that the information being stored in a variable will be ‘a certain way’. So like I said, if you’re storing your age, you would use the Integer type… well that’s because in Java, an Integer means you have a number that won’t have any decimal places in it. It will be a whole number, like 5, or 20, or 60, or -60, or 4000, or -16000. All of those numbers would be considered an Integer in Java.
So what would happen if you tried to store something that wasn’t an Integer, into an Integer variable, say for instance the value “$35.38”? Well, quite simply, you would get an error in the program and you would have to fix it! “$35.38” has a dollar sign ($) in it, as well as a decimal place with two digits of accuracy. In Java, when you specify that a variable is of type Integer, you are simply not allowed to store anything except a whole number.
Specifying what kind of data that you are dealing with allows the programming language to use that data in interesting ways. Again, what I say “specifying what kind of data”, I’m just referring to the type of data.
Let’s dive into the power of assigning a type to your data.
What can you do with data types?
Let’s start with a simple example.
Your desire is to add two numbers together, let’s say the number 22 and the number 3. Java will behave differently depending on the type of the variable that’s storing this data.
Let me show you what I mean:
If you have defined your variables to be of type Integer, then adding 22 and 3 together will result in the Integer 25. Makes perfect sense right? Of course, this is simple Math.
But what happens if your variables are not Integers, but are Strings?
A String in Java is a different kind of data type and it behaves differently BECAUSE it is a different type of data.
When we refer to a String in Java (and in many other programming languages) we are treating the data like it’s just a plain old sentence in the English language. A String just represents words (or more specifically letters) all placed in a certain order. That’s all the English language (or any language) is, a series of characters/letters placed in a certain order to give meaning to what you’re writing down.
So now I ask you, what does it mean to add two sentences together? What does it mean to add two Strings together?
I’ll show you.
If you were to have two variables, each defined as Strings and they stored the data “22” and “3” (respectively), what would happen if we added them together?
We would get the String: “223”
This might be confusing at first, but it makes more sense when we use less “misleading” data.
Let’s assume that in our two String variables, we aren’t storing numbers, we’re storing words. So in variable 1 we store the String “Hello”, and in variable 2 we store the String “World”.
Now what happens in your mind if I tell you to add those two words together?
Hopefully your natural instinct is to say that the resulting String would be “Hello World”!
That’s all that’s happening with the Strings “22” and “3”… Java behaves differently because of the type of the variables.
To Java, the String “22” is the same type of data as the String “twenty-two”, they’re both characters arranged in a specific way.
Now I don’t want to go into too much detail about types, as this is better suited to programming basic concept #3 – Data Structures. So that’s all I will touch on for now, but no worries, it will all make sense in time!
Want Free Access to my Best Java Courses?
Alright, so if you’re read this far down the article, then you’re clearly interested in learning how to code. You can currently get access to my 2 best courses on Java for free for 30 days (then it’s just $10/month afterwards, or $97/year).
When you sign up you’ll receive login information to start learning about Java. If you decide you don’t like the courses, you can cancel at any time. If you decide you love the courses then you don’t have to do anything and you can stay subscribed for as long as you like for just $10/month or $97/year.
It’s like Netflix for coders!
Go ahead an click the button below to get started!
So, to sum up, we talked about what a variable is and how you can store information in a variable and then retrieve that information at some later point in time. The variable can have a name, and this name you give to the variable is usually named after the kind of content you’ll be storing in the variable, so if I’m storing your name in the variable, you’d name the variable ‘yourName’. You wouldn’t HAVE to give it that name, you could name the variable “holyCrapImProgramming”, but that wouldn’t make a whole lot of sense considering you are trying to store a person’s name. Makes sense right? Finally, variables have types, and these types are used to help us organize what can and cannot be stored in the variable. Hint: having a type will help to open up what kind of things we can do with the information inside the variable. Example: if you have two Integers (let’s say 50 and 32), you would be able to subtract one variable from the other (i.e 50 – 32 = 18), pretty straight forward right? But, if you had two variables that stored names (i.e. “Trevor” and “Geoff”) it wouldn’t make sense to subtract one from the other (i.e. “Trevor” – “Geoff”), because that just doesn’t mean anything! So, types are also a powerful thing, and they help us to make sense of what we CAN do with our variables and what we CANNOT do!
So I hope this information has been helpful to you, and I hope you realize what the benefits of learning a programming language are! The allure to learning a programming language is quite high in today’s corporate world, as most companies are hiring programmers with the skills to create web applications. The programming profession is one that provides excellent pay and job stability, and in the end, isn’t that what we’re all looking for?
To dive even deeper into the subject, you can watch this video I’ve created on the topic of Variables: