Basic Concepts
In a nutshell: Variables store information; data types define what kind of information can be stored.
## Understanding Variables
Variables are containers for storing data values. In Java, every variable must be declared with a specific data type, which tells the computer what kind of data the variable will hold.
## Common Data Types
- `int`: Stores whole numbers, like 7 or -42.
- `double`: Stores decimal numbers, like 3.14 or -0.01.
- `boolean`: Stores `true` or `false` values.
- `char`: Stores single characters, like 'A' or 'z'.
- `String`: Stores sequences of characters, like "Hello, Java!".
## Declaring and Using Variables
To declare a variable, write the data type, then the variable name, and assign a value with `=`:
```java
int age = 16;
double price = 9.99;
boolean isStudent = true;
char grade = 'A';
String name = "Alex";
```
## Changing Values
You can change a variable's value at any time:
```java
age = 17;
price = price - 2.00;
```
## Why Variables Matter
Variables let programs remember information and perform calculations. They're the building blocks for everything in programming!
## Real-World Analogy
Imagine variables as labeled jars in a kitchen. Each jar (variable) can hold a specific kind of ingredient (data type).
## Common Mistakes
- Forgetting to declare the data type.
- Trying to store a string in an `int` variable.
Examples
- Declaring an `int` called `score` and setting it to 100.
- Using a `boolean` named `isLoggedIn` to track user login status.
Key terms
- Variable
- A storage location identified by a name that holds data.
- Data Type
- Specifies the kind of data a variable can store, such as int, double, or boolean.