Skip to content

More Variables and Casting

  • Lesson
  • 5 min read
  • 7 of 8 in this chapter

The power of variables

Before we jump into casting, let's talk about variables and other things we can do with them. We'll also go over how the Java compiler reads and executes the code we write.

Let's start with this variable.

Java
int x = 12;

And then, let's create a new variable that is set to x.

Java
int y = x;

Changing variables

We haven't talked about this, but we can change variables in Java and pretty easily. Let's set x to 8.

Text
x = 8;

Notice how we didn't retype the data type, int, when giving x a new value. This is because, in Java, we only need to declare the variable and its type once. After that, we can go ahead and do whatever we want.

So let's keep track of this. x is equal to 8. So what does that make y equal to, if it is equal to x before the change? (Try printing the variables to see their changes over time)

If you checked the variables, y is equal to 12. When we set y equal to x, we don't fully make it equal x, but the value of x.

Here's a quick practice with three variables. What do you think the values of x,y, and z will be at the end of this program?

Java
int x = 1;
int y = 2;
int z = 3;

x = y;
z = x;
y = 4;
z = y;
x = z;
y = x;
z = 2;
x = y;
z = y;

We won't put the answers here, but they should all be the same number.

Self Operations

In the previous lesson, we talked about Operators. We used all of them with numbers but not with variables and numbers. Here are some examples using variables:

Java
int x = 5*2;
int y = x + 7;
int z = x * y;

As you can see, we can mix and match our variables, operators, and even numbers.

However, what if we wanted to add 1 to a variable? Just 1.

If x is equal to 7, how do we make it equal to 8 without setting it to 8? You might think something similar to this:

Java
int x = 7;
x = x + 1;

And you aren't wrong. The compiler does the expression/calculation first before setting the variable, meaning that x+1 would be the same as 7+1.

However, there's a better way to do this.

We can rewrite the code like this:

Java
int x = 7;
x += 1;

The += means to add to itself. = x + 1 is the same as += 1. In fact, we can do this with all the operators:

Java
int x = 5;

x += 5;
x -= 9;
x *= 10;
x /= 2;
x %= 2;

System.out.println(x);

Your final value of x should be 1; These self operations become super helpful in the future. But for the simple adding one and subtracting one, we can do this:

Java
int x = 5;
x--;
x++;

(That subtracted one from x then added one back to x.)

Block variables from changing

What if we don't want to change a variable or make sure we can't change it later? For example, a person in the United States can get a driver's license is 16. This number doesn't change, and we don't want to change it. This is where final comes into play. We can use it like this:

Java
final int driverLicenseAge = 16;

If you try to change this variable, later on, you will get an error.

Casting

Alright, let's talk casting. Casting is how we convert a variable from one data type to another. For example, converting a double to an int. To cast, just put the data type you want in () before the number/value/variable you want to cast.

We can do so like this:

Java
double x = 7.6;

int y = (int) x;

System.out.println(x);
System.out.println(y);

And the output:

Text
7.6
7

As you can see, when casting a double to an int, it rounds down in the process. Now, let's try it the other way:

Java
int x = 7;

double y = (double) x;

System.out.println(x);
System.out.println(y);

Here's the output:

Text
7
7.0

In this instance, it just gave it 7 a single decimal place.

You may think that casting is useless, but it is beneficial in one thing: Rounding.

Rounding

We already know that casting a double to an int results in rounding down no matter what. 7.9 becomes 7. Even 7.999999 becomes 7. So how do we round to the nearest whole number?

We can do so like this:

Java
double x = 7.6;

int y = (int)(x+0.5);
System.out.println(y);

This prints out 8, which is the nearest whole number, rounding up.

If we used 7.1, it would become (int)(7.6), rounding down to 7, which is exactly what we want.

If you hadn't noticed, we did the math in a parenthesis (just like in math!) for the reason to calculate that first before casting. Without it, x would be cast to an int, and there would be an error because you can't add 0.5 to an int.

This was a long lesson, but we went over some core parts of programming. Once we get done with the basics is when we'll get into the fun stuff, so keep pushin' through..`rite