PY
JV

Python to Java

11 lessons

Progress0%
1Introduction2Variables & Types3Functions to Methods4Lists to Arrays5Dicts to Maps6Classes & OOP7Inheritance8Exception Handling9Modules to Packages10Ecosystem11Modern Java Features
All Mirror Courses
PY
JV
Variables & Types
MirrorLesson 2 of 11
Lesson 2

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.

Mirror Card
PY
From Python:

In Python, you're familiar with variables & types.

JV
In Java:

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:

JV
Java 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:

PY
Python (What you know)
# 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"
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python infers types dynamically; Java requires an explicit type at declaration

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python True/False vs Java true/false (lowercase in Java)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python str vs Java String — capital S, it is a class in Java

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Java has primitive types (int, double, boolean) and their object wrappers (Integer, Double, Boolean)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

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.

PY
Python
name = "Alice"
age = 30
JV
Java
String name = "Alice";
int age = 30;

2. Booleans

Python booleans are True and False (capitalized). Java uses true and false (lowercase).

PY
Python
active = True
found = False
JV
Java
boolean active = true;
boolean found = false;
Common Pitfall
Using True or False in Java is a compile error — they must be lowercase.

3. var — Type Inference

Java 10 introduced var for local variables, similar to Python's implicit typing.

PY
Python
message = "hello"  # Python always infers
JV
Java
var message = "hello";  // Java 10+ infers String
Rule of Thumb
Use var only for local variables when the type is obvious from the right-hand side.

4. Primitives vs Wrappers

Java has primitive types (int, double) for performance and object wrappers (Integer, Double) required by generics and collections.

PY
Python
# Python: everything is an object
nums = [1, 2, 3]  # list of int objects
JV
Java
// Java: primitive array
int[] arr = {1, 2, 3};
// Generic collection needs wrapper
List<Integer> list = new ArrayList<>();
Common Pitfall
Generics like List<int> are invalid in Java — you must use List<Integer>.

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
Common Pitfall
Don't assume Java works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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+)
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
PreviousNext