Variables & Types
Variables & Types
Introduction
In this lesson, you'll learn about variables & types in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with variables & types.
Java has its own approach to variables & types, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Java: static typing — type is mandatory
String name = "Alice";
int age = 30;
boolean active = true;
double pi = 3.14;
// Java 10+: var for local type inference
var inferred = "Java 10+"; // inferred as String
// Java primitives vs wrapper types
int primitive = 42; // primitive (fast)
Integer boxed = 42; // wrapper object (needed for generics)Comparing to Python
Here's how you might have written similar code in Python:
# Python: dynamic typing — no type declarations
name = "Alice"
age = 30
active = True
pi = 3.14
# Type hints (optional, not enforced at runtime)
score: int = 95
label: str = "hello"You may be used to different syntax or behavior.
Python infers types dynamically; Java requires an explicit type at declaration
You may be used to different syntax or behavior.
Python True/False vs Java true/false (lowercase in Java)
You may be used to different syntax or behavior.
Python str vs Java String — capital S, it is a class in Java
You may be used to different syntax or behavior.
Java has primitive types (int, double, boolean) and their object wrappers (Integer, Double, Boolean)
You may be used to different syntax or behavior.
Python type hints are optional; Java types are mandatory and enforced at compile time
Step-by-Step Breakdown
1. Declaring Variables
Java requires the type before the variable name. Python just assigns.
name = "Alice"
age = 30String name = "Alice";
int age = 30;2. Booleans
Python booleans are True and False (capitalized). Java uses true and false (lowercase).
active = True
found = Falseboolean active = true;
boolean found = false;3. var — Type Inference
Java 10 introduced var for local variables, similar to Python's implicit typing.
message = "hello" # Python always infersvar message = "hello"; // Java 10+ infers String4. Primitives vs Wrappers
Java has primitive types (int, double) for performance and object wrappers (Integer, Double) required by generics and collections.
# Python: everything is an object
nums = [1, 2, 3] # list of int objects// Java: primitive array
int[] arr = {1, 2, 3};
// Generic collection needs wrapper
List<Integer> list = new ArrayList<>();Common Mistakes
When coming from Python, developers often make these mistakes:
- Python infers types dynamically; Java requires an explicit type at declaration
- Python True/False vs Java true/false (lowercase in Java)
- Python str vs Java String — capital S, it is a class in Java
Key Takeaways
- Types are mandatory in Java — String name, int age, boolean flag
- Java booleans are true/false (lowercase)
- var gives Python-like inference for local variables (Java 10+)