Skip to content

Variables

  • Lesson
  • 6 min read
  • 4 of 8 in this chapter

Variables??

Yup, variables. If you are familiar with basic algebra, you should know what variables. They are terms that have a value attached to them. A variable is broken up into three parts:

  • the name of the variable
  • the type of the variable
  • the value of the variable

Except in math, you already have the type defined: it's any number. It can be an integer or a number with decimals. In Java, we need to be clear on what type of value our variable is.

And that's where our Primitive Types come in. The Primitive Types are the barebones building blocks of all our code. It's the "first-level" of data in Java. We will be focusing on these primitive types:

  • String
  • char
  • boolean
  • int
  • double

String

You have already seen a String in action. Remember in the last lesson how we were printing Hello World to the console? The content in the quotes is our String. The quotes told the Java compiler that we were writing a string. To write a string (or any variable), we do the following:

  • Declare the type
  • Give the variable a name
  • Set it equal to a value that is the same type, using =
  • end out the line with a ;

In Java, we use ;, or semicolons, to identify when a line of code is done. This way, the compiler can move to the next line and knows when to do so.

A String is a value of several characters. It can be a word, sentence, even a singular character. It's a "string" of characters if that makes sense.

Here are some examples of Strings:

Java
String name = "Greg";
String bookName = "Diary of a Wimpy Kid";
String birthMonth = "March";

Make sure you always surround your Strings in ", otherwise Java won't think it's a string, regardless of the variable type.

As you see above, we made three String variables: name, bookName, and birthMonth. These are our variable names.

Variable names in Java are case-sensitive. This means that you can have both birthMonth and birthmonth be different variables.

However, you can't name variables that contain the following:

  • Any number by itself (it's okay if there are letters with it, but it is still discouraged)
  • public, static, private, void, int, boolean, etc. (these are reserved words in Java)
  • NO SPACES (all variable names in Java must be a single word)

Make sure that your variable names are specific but not too complicated. This will make programming and the usage of these variables much easier

You will most likely get an error if you misspell or write an incorrect variable, but it's still helpful to know the rules.

If you hadn't noticed yet, the recommended way to write variables is through camelCase. For names, you make the first letter of the first word lowercase and the rest uppercase. It is important to make sure your variable naming scheme follows a standard order.

  • I like Pie becomes iLikePie
  • My Birthday becomes myBirthday
  • My Most Favorite Movie Ever becomes myMostFavoriteMovieEver

char

Above, we talked about Strings. They are several characters and can even be one character. However, sometimes, Strings don't work for single characters. That is where char comes into place.

The char data type is used for single characters. Every character has a numerical value attached to it, dependent on where it is in the list identified by Java. You don't need to really know how that works, but know that it exists.

We write chars like the following:

Java
char ch = 'B';
char ch2 = 'z';

As you can see, these are very similar to how we wrote strings. The main differences are:

  • we used ', or single quotes, rather than the double quotes in Strings
  • they are single characters
  • we identified the variables as char types

And that's basically it for chars!

boolean

Do you know what booleans are?

See, that's a yes or no question. Regardless of your answer, you do know what booleans are. Booleans are variables that we can set to true or false. It's that easy! Only two possible values. Let's write some out:

Java
boolean doILikePizza = true;
boolean canIDrive = true;
boolean amIAnAdult = false;

We don't use any quotes since this isn't a char (single) or string (double). We write true or false.

Make sure you write true and false lowercase, as that's the only way it can be identified.

int

Now time for the numbers. You should already know what an integer is, but here's a quick refresher:

Integers are whole numbers that can be positive or negative and don't have decimals. That's it. So to write int variables, we can do the following:

Java
int age = 17;
int year = 2021;
int bankAccount = -1000;

Remember, no quotes or decimals at all. Just the integer number. For the AP Exam, you also need to know these:

Java
int minValue = Integer.MIN_VALUE;
int maxValue = Integer.MAX_VALUE;

All variables have a certain amount of data they can store. Integers, they can store any number from -2,147,483,648 to 2,147,483,647. Luckily, you will never need to remember this, as Integer.MIN_VALUE and Integer.MAX_VALUE are equal to values, respectively. We will use those values in a future chapter to learn how to choose the smallest or largest number from a list.

double

And finally, double. A double is a variable that can store decimals. In fact, it can store up to 15 digits of decimals. We write doubles like so:

Java
double shirtPrice = 4.99;
double pi = 3.14159265;
double debt = -89.70;
double age = 17;

As you can see, we can do a lot of different things with doubles. But did you notice the last double we made? double age = 17; That stores a value that looks like an integer but is a double.

When I wrote 17, the code interpreted it as 17.0. That doesn't really make much sense, which is why we would use int as the type for an age.

Make sure you use the correct data type with the correct type of variable, or it will make your code much more difficult.

In the next lesson, we will be going over more about printing to the console and learning more about how Java treats variables.