0/30 Days Completed 0 Learning Days
Day 2 of 30 · Week 1 — Java Foundations
Variables and data types
1 Learn
A variable is a named box that stores a value. Before using a variable, you say what type of value it holds.
Common types: int (whole numbers), double (decimal numbers), char (one character in single quotes), boolean (true or false) and String (text in double quotes).
The name of a variable is an identifier. It can use letters, digits, _ and $, but cannot start with a digit or be a keyword like class.
2 See the Code
Java
public class Student {
public static void main(String[] args) {
String name = "Ananya";
int age = 19;
double marks = 86.5;
char grade = 'A';
boolean passed = true;
System.out.println(name + " is " + age);
System.out.println("Marks: " + marks + ", Grade: " + grade);
System.out.println("Passed: " + passed);
}
}- Each line creates a variable with a type, a name and a value.
- The + sign joins text and values together when printing.
- char uses single quotes 'A'; String uses double quotes "Ananya".
Output
Ananya is 19 Marks: 86.5, Grade: A Passed: true
3 Practice
- Create variables for a book: title (String), pages (int), price (double) and available (boolean). Print them.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. Which type stores 3.75?
2. Which is a valid variable name?
3. What type is 'Y'?
Step 5
Answer and check the Quick Check above to unlock this.