Get startedGet started for free

Starting with variables

1. Starting with variables

So far, we directly printed values directly to the screen. But what if we want to store data and use it later? That's where variables come in!

2. Variables are boxes

We can think of a variable as a labeled box we use when moving.

3. Boxes store different information

Just like we might pack books into a box labeled "Books" and kitchen supplies into another box labeled "Kitchen," variables allow us to store different kinds of information in our code.

4. Variables have type, name, and value

Every variable in Java has a type, a name, and a value.

5. Assigning variables - String keyword

Let's see how this works in Java. To store text, we use the keyword `String`,

6. Assigning variables - name

followed by the name of the variable,

7. Assigning variables - syntax

an equal sign,

8. Assigning variables - text

and the text we want to store inside double quotation marks. If we want to store `“Hello, Java!”`, we create a variable of type `String` called `message`, assign that text to it, and end the line with a semicolon.

9. Assigning variables - whole numbers

Numbers work similarly, but instead of String, we use int for whole numbers. If we want to store the number 29, we define a variable called age and set it equal to 29.

10. Equal sign doesn't mean equal

The equal sign in Java doesn't mean "equal" in a mathematical sense - it's an assignment operator. It takes the value on the right and stores it inside the variable on the left, just like putting books into a box labeled "Books."

11. Variables have a type

Java is strict about how we name and declare variables. First, we always need to specify the type of data a variable will hold. If we want to store text, we must declare it as a String. If we want to store a number, we must declare it as an integer or another numeric type. We'll learn more about the other types in the next chapter.

12. Naming convention

Variable names must start with a letter and cannot contain spaces. The recommended naming convention is CamelCase where the first word starts with a lowercase letter, and each following word begins with a capital letter.

13. Using variables

Once we assign a value to a variable, we can use it anywhere in our program. Instead of typing out a number or a phrase multiple times, we can simply use the variable name, and Java will retrieve the stored value. For example, if we pass our message variable to System.out.println, Java will print whatever text is inside.

14. Summary

Variables store values. They are declared with a type and assigned values using an equals sign. Variable names must start with a letter, follow camel case, and cannot contain spaces. Once we store a value, we can use the variable name instead of writing out the value directly.

15. Let's practice!

Let's practice by creating and using variables in the next exercise!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.