Variables, Constants, and Data Types

Start course
Overview
Difficulty
Intermediate
Duration
2h 7m
Students
173
Ratings
5/5
starstarstarstarstar
Description

This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.

Learning Objectives

  • Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
  • Learn about arithmetic, relational, and logical operators
  • Understand how to obtain input from the user of our programs

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the last lecture, we wrote our very first little application and printed a couple of simple strings to the console. In this lecture, we will learn some of the more important foundational building blocks for essentially all programs. When writing software at a very general level, you work with instructions and data, that's pretty much it. Variables, constants, and their data types are the primary mechanisms for storing and working with the data we are interested in. For what we're doing at the moment, it would be best to write out some code first and then to discuss it, providing context and terminology as we're going along. So, we'll continue to use our Section2-Projects project in IntelliJ and add a new Java class called VariableFun. So, over here in the source folder right here, I'll go to new, Java class, and we will call it VariableFun. And we have our public static void main, which is our skeleton program here. So, let's write some code and then we'll discuss it. So, int age = 19, String name = "Billy",  System.out.println(name + " is " + age). Here we go. So, let's give this a quick run to see what it does. Right-click the 'VariableFun' file and then go to Run 'VariableFun.main()'. And we will get some output in just a moment here.

There we go. So, we've got Billy is 19 right there, which is what we expect. So, you might have even been able to expect that even if you're not familiar with code because it seems pretty logical and pretty straightforward. So, what do all these new words and things mean? Well, when we write int age = 19, that consists of a variable declaration, this part right here and an assignment using the = that right there. A variable is like a named box of memory that's allocated to store something in it. We want to store the value 19. So, since that's a whole number or integer, we use the data type int. Integers don't have a decimal component to them. We also use the = which is called the assignment operator in Java. We are assigning the value 19 to the variable integer age. This is a little bit like putting the 19 into this box of memory that we've named age. We can also separate this into two different lines. So, let's see what that looks like. So, I'm going to separate the declaration right here, int age, age = 19, notice I don't put int in front of it again, right there. Okay, so I've got two different lines. So, we've separated this into the two different statements. The first is the variable declaration and then that's the initialization right there.

So, the declaration just basically declares the existence of the variable age that allocated enough memory to hold an integer. The declaration consists of the data type int followed by the name of the variable that you come up with. This name that you come up with has to start with a letter, an _ , or a $, and can contain integers but an integer can't be the first character in the variable's name. There are also some other rules about names but those are the major ones. By convention, a variable should start with a lowercase letter. Also, the formal term used for a variable's name is the term identifier. So, what if the identifier contained more than one word? We can't put a space in the identifier, that's not a valid part of a name. So, what can we do? The convention for variables is to use what is called camelCase or camelCasing. So, the first word is lowercase and additional words have their first letter capitalized. Let's see what this looks like by changing the variable age's identifier to something else. So, int myAge and then I'm going to change this to myAge = 19 because it doesn't know what age is now. There we go, myAge, awesome. So, the next statement right here is called the initialization where the variable is given its initial value. 

You might think, wait, isn't that called an assignment statement? And you'd be right, it is an assignment statement also, but the first time a variable is assigned a value it's called the initialization as well. You can see that we could have the declaration and initialization on a single line like we had before or we can split it up across two lines. Just note that if you split it up you have to declare a variable before you can assign it a value. Like I can't switch lines 5 and 6 right now, I can't flop them or it'll be an error. Now if we move on to the next statement, we have String name = "Billy". Strings are a little different than very simple data types like int in Java. We will discuss this classification difference in more detail in a later lecture but for now just know that the int variable directly holds the 19 and the String variable actually holds the address of the string object in memory. 

But as far as our usage of the variables, much of it's quite similar. We can print out both integers and string variables using println right here, for example. In the example we wrote  (name + " is " + myAge). And then I guess I have to change that to myAge now, don't I? Yes, I do. Okay. Because that would not compile. So, we've got the string literal, the spaces, and then we have another +, and then finally the age or myAge variable. In this context with strings involved, we call the  + the concatenation operator. We are essentially combining the string variable name with the string literal " is ", and finally an integer concatenated to the end. Note that the + has a big decision to make, figuratively speaking. One of its operands is a string on the left and the other is an integer on the right, right here. So, does it try to add them or does it try to concatenate them?

So, the answer is whenever there's a string involved the string will win. The + does not reflect the addition operator, it reflects concatenation. So, it essentially converts the energy to a string representation and then concatenates it to the " is " right here. Now, let's revisit and summarize our terms quickly. A variable is a named allocated chunk of memory that can hold some sort of data. The type of value a variable can hold is determined by the variable's data type. The variable's name that you come up with is called the identifier. When you write a data type followed by the variable's name that is a variable declaration.

When you have a variable name and assignment operator, the = and then some value it's being set to, this is called an assignment statement in general. And the first time a variable is assigned a value we call that the variable's initialization because you're giving it an initial value. So, variable, data type, identifier, declaration, assignment, and initialization. That's a lot of vocabulary to keep track of. But terminology is important in any field and computer science, data science, and software engineering are no exceptions. In order to communicate your thoughts and intentions more effectively, it's crucial to know terminology as well as how to apply your skills. Now there's another method I'd like you to take a look at that is slightly different than the println method. So, what happens if we just use the print method instead of println? So, let's see, we're going to change this println to a print. Now pay attention to what was printed out previously, just Billy is 19, the extra space and all this other fun stuff. Now what happens when we run this? VariableFun and then we run 'VariableFun.main()'. Now you'll notice that the extra space that was put in between there is no longer there. So, it's very interesting. So, println introduces what we call a new line character into the output stream and that pushes the insertion point farther to the next line.

Whereas the plain old print method does not, that's why we get this output and it's different. There's not that extra space. Another thing you might want to know is, is there any way we can make a new line or tab or some space like that appear but maybe still just use print? There is a way, you can use escape sequences. There are several but two of the most popular are \n and \t for new line and tab respectively. Even though these require two keystrokes to create, escape sequences are considered single characters. Let's update our print method to use the new line character at the end and see what happens. So, I've got the print method but I'd like to also add this new \n right there and see what happens when I run it. So, let's run and right click 'Run', and we now again get our extra space here. So, this pushes that one down to the next line, awesome. So, the escape sequence \n allows us to insert a new line character into the stream, and you could put more than one if you needed to. Now let's quickly add one more small statement to learn about symbolic constants. So, we're going to add this below the other int and above the string I suppose, final int SOME_NUM = 150.

All right so, with symbolic constants also just called constants, you can initialize them with a value but unlike regular variables, once you initialize them with a value, you can't change that value. Additionally, although not required is common to use so called  snake case or _case when dealing with constants, besides symbolic constants, which are basically just variables you cannot change the value of once initialized. We also have literal constants which are values like directly 150 or "Hello World" and things like that. You can't set 150 = 20, I can't say 150 = 20, that doesn't make sense. So those are literal constants and they make up the majority of these values that we're using here, 19, 150, Billy. And then this is a symbolic constant, we make a symbol and identifier for it to represent this value but since it has final in front of it, I can't change its value. So, before we move on to the next lecture, I have a little challenge for you. Using the same VariableFun Java file, I'd like you to add a completely new variable to hold the name of your hometown. So, you can come up with an identifier for the variable and set its value, also print it out below the print line statement that we have already. So, pause the video and come back when you're done or if you need some help. How did that go for you? Were you able to complete this challenge?

Let's work on it together. So below this String name = "Billy", I'll put String myHometown = "Dearborn". And then below our print out down here, I'm going to put system.out.println and I'll change the other one above it back to println, and take out this extra space. And we're going to just put, no " , ", we just want to put myHometown because that's a variable name. All we're doing is just printing it out. You could concatenate myHometown is in front of it if you'd like or do something like that. But this is what I'm doing. So, I'm going to right-click 'VariableFun' and we're going to go to Run 'VariableFun.main()'. And after Billy is 19, I see Dearborn, awesome. So, we have our hometown now printed after Billy is 19. Good work. Let's move on to the next lecture where we'll take a closer look at categories of data types. I'll see you there.

 

About the Author
Students
788
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics