C#
JV

C# to Java

10 lessons

Progress0%
1Introduction2Type Systems3Properties & Getters4Generics5Collections6LINQ to Streams7Async Programming8Ecosystem9Modern Java Features10Concurrency
All Mirror Courses
C#
JV
Type Systems
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
C#
From C#:

In C#, you're familiar with type systems.

JV
In Java:

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:

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

C#
C# (What you know)
int x = 5;
string name = "Alice";
bool active = true;
List<int> nums = new();
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

string (lowercase keyword) in C# vs String (uppercase class) in Java

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

bool in C# vs boolean in Java

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# List<T> (System.Collections.Generic) vs Java ArrayList<T> (java.util)

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# has value types (struct) with stack semantics; Java has primitives + autoboxing for generics

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

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.

C#
C#
bool isReady = true;
string greeting = "Hi";
JV
Java
boolean isReady = true;
String greeting = "Hi";
Common Pitfall
In Java, String with a capital S is a class, not a keyword. Using lowercase 'string' is a compile error.

2. Generics and Autoboxing

Java generics only work with reference types, not primitives. Autoboxing converts int → Integer automatically when needed.

C#
C#
List<int> ids = new List<int>();
ids.Add(1);
JV
Java
// Must use Integer (wrapper), not int
ArrayList<Integer> ids = new ArrayList<>();
ids.add(1); // autoboxed int -> Integer automatically
Rule of Thumb
Use primitive types (int, double) for variables and arithmetic. Use wrapper types (Integer, Double) only when generics or null is needed.

3. 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.

C#
C#
var map = new Dictionary<string, int>();
var items = new List<string>();
JV
Java
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.

C#
C#
struct Point { public int X; public int Y; }
// Copied by value, lives on stack
JV
Java
// 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 Pitfall
Java records are immutable by design. They do not replace mutable structs — use a regular class for that.

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

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