Type Systems
Type Systems
Introduction
In this lesson, you'll learn about type systems in Java. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with type systems.
Java has its own approach to type systems, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
int x = 5;
String name = "Alice";
boolean active = true;
ArrayList<Integer> nums = new ArrayList<>();Comparing to C#
Here's how you might have written similar code in C#:
int x = 5;
string name = "Alice";
bool active = true;
List<int> nums = new();You may be used to different syntax or behavior.
string (lowercase keyword) in C# vs String (uppercase class) in Java
You may be used to different syntax or behavior.
bool in C# vs boolean in Java
You may be used to different syntax or behavior.
C# List<T> (System.Collections.Generic) vs Java ArrayList<T> (java.util)
You may be used to different syntax or behavior.
C# has value types (struct) with stack semantics; Java has primitives + autoboxing for generics
You may be used to different syntax or behavior.
var works in both languages for local type inference
Step-by-Step Breakdown
1. Primitive Type Name Differences
Most primitive types have the same name (int, double, float, char) but a few differ: bool vs boolean, and string vs String.
bool isReady = true;
string greeting = "Hi";boolean isReady = true;
String greeting = "Hi";2. Generics and Autoboxing
Java generics only work with reference types, not primitives. Autoboxing converts int → Integer automatically when needed.
List<int> ids = new List<int>();
ids.Add(1);// Must use Integer (wrapper), not int
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1); // autoboxed int -> Integer automatically3. Type Inference with var
Both C# and Java (10+) support var for local type inference. The behavior is identical — the type is fixed at compile time.
var map = new Dictionary<string, int>();
var items = new List<string>();var map = new HashMap<String, Integer>();
var items = new ArrayList<String>();4. C# Struct vs Java Primitives
C# lets you define value types with struct. Java has no user-defined value types — primitives are built-in only, and all user types are reference types.
struct Point { public int X; public int Y; }
// Copied by value, lives on stack// Java has no struct — use a class (reference type)
class Point { public int x; public int y; }
// Always a reference, lives on heap
// Java 16+ records are immutable value-like classes:
record Point(int x, int y) {}Common Mistakes
When coming from C#, developers often make these mistakes:
- string (lowercase keyword) in C# vs String (uppercase class) in Java
- bool in C# vs boolean in Java
- C# List<T> (System.Collections.Generic) vs Java ArrayList<T> (java.util)
Key Takeaways
- bool → boolean, string → String (capital S)
- Java generics require wrapper types (Integer, not int)
- var works in both for local inference
- No user-defined value types in Java; use classes or records