Storing and Using Information in Java

Now that you’ve seen how to write and run a Java program, let’s make it do something useful. In this chapter, you’ll learn how to store data, accept input from the user, and start working with different types of values. This is where your programs stop being one-sided and start becoming interactive.

What You’ll Learn

What Are Variables?

Think of variables like labeled boxes in memory that hold values.

what are variables

You can use these values throughout your program — update them, print them, or combine them.

Java’s Data Types

Primitives (store actual values):

Reference Types (store references to objects):

data types

Comments in Java

Comments help explain what your code is doing. Java ignores them when running the program.

comments in java

Use comments to make your code readable for future-you (or anyone else).

Getting Input from the User

We’ll use the Scanner class to let the user type information into the program.

getting input from the user

You only need to import Scanner once at the top of your file.

Operators

Arithmetic Operators

arithmetic operators

Comparison Operators

comparison operators

Practice Time!

Try this mini challenge:

Ask the user for two numbers.
Print the sum, the difference, the product, and the quotient.

Then:

Ask the user for their name and favorite programming language.
Print a greeting that includes both.

What’s Next

Now that your programs can store and use information, let’s learn how to make decisions. In the next chapter, we’ll explore conditions: if, else, and how to control your program’s flow based on logic.