Lesson 2 of 34

Variables and Data Types

8 min 20 XP

In Java you must state a variable's type when you create it. int is for whole numbers, double for decimals, boolean for true/false, char for a single character and String for text.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Using + with a String joins values together (concatenation). You can change a variable's value later, but not its type. A char uses single quotes ' ', a String uses double quotes " ".

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Mark a variable final and it can never change (a constant). Constant names are usually written in CAPITALS.

Challenge

+20 XP

Store "Erode" in a String variable city and 2026 in an int variable year, then print: City: Erode Year: 2026

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.