Java vs C# – Key Differences Explained with Examples
Java and C# are close relatives. Both are statically typed, object-oriented, garbage-collected languages that compile to an intermediate format and run on a managed runtime: the JVM for Java and the .NET CLR for C#. C# was created by Microsoft in 2000, partly in response to Java, and the two languages have kept borrowing ideas from each other ever since.
If you know one, you can read the other almost immediately. This tutorial highlights the differences that matter in practice, with code from both languages.
1. Java and C# at a Glance
| Aspect | Java | C# |
|---|---|---|
| Created | 1995, Sun Microsystems (now Oracle) | 2000, Microsoft (Anders Hejlsberg) |
| Runtime | JVM (HotSpot and others) | .NET CLR |
| Intermediate code | Bytecode (.class, .jar) | CIL / IL (.dll, .exe) |
| Platforms | Windows, macOS, Linux and more | Windows, macOS, Linux (.NET 5+) |
| Properties | Getter and setter methods; records | Built-in get/set properties |
| Querying collections | Stream API | LINQ |
| Asynchronous code | CompletableFuture, virtual threads | async/await with Task |
| Generics | Type erasure | Reified (types kept at runtime) |
| Custom value types | No (primitives only) | struct |
| Checked exceptions | Yes | No |
| Operator overloading | No | Yes |
| Main frameworks | Spring Boot, Jakarta EE, Quarkus | ASP.NET Core, Entity Framework |
2. Hello World in Both Languages
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Notice the naming styles: C# methods use PascalCase (WriteLine), while Java methods use camelCase (println). C# programmers usually put opening braces on a new line; Java programmers keep them on the same line.
3. Properties vs Getters and Setters
C# has properties built into the language. Java uses ordinary methods by convention (the JavaBeans style), or a record for immutable data.
public class Person
{
public string Name { get; set; } // auto-property
private int age;
public int Age
{
get => age;
set => age = value >= 0 ? value : throw new ArgumentException("age must be >= 0");
}
}
var p = new Person { Name = "Asha", Age = 30 };
Console.WriteLine($"{p.Name} is {p.Age}");
public class PersonBean {
// Java: private field + getter/setter methods (or a record for immutable data)
private String name;
private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) {
if (age < 0) throw new IllegalArgumentException("age must be >= 0");
this.age = age;
}
public static void main(String[] args) {
PersonBean p = new PersonBean();
p.setName("Asha");
p.setAge(30);
System.out.println(p.getName() + " is " + p.getAge());
}
}
Asha is 30
4. LINQ vs the Stream API
Both languages let you filter, transform and sort collections in a declarative style. C# calls this LINQ; Java calls it the Stream API.
var names = products
.Where(p => p.Category == "Electronics")
.Where(p => p.Price > 100)
.Select(p => p.Name)
.OrderBy(n => n)
.ToList();
import java.util.List;
public class StreamsVsLinq {
record Product(String name, String category, double price) { }
public static void main(String[] args) {
List<Product> products = List.of(
new Product("Laptop", "Electronics", 899.0),
new Product("Mouse", "Electronics", 25.0),
new Product("Desk", "Furniture", 240.0),
new Product("Monitor", "Electronics", 199.0));
List<String> names = products.stream()
.filter(p -> p.category().equals("Electronics"))
.filter(p -> p.price() > 100)
.map(Product::name)
.sorted()
.toList();
System.out.println("Electronics over 100: " + names);
}
}
Electronics over 100: [Laptop, Monitor]
LINQ also has a SQL-like query syntax (from p in products where … select …) and can be translated into database queries by Entity Framework. Java streams are always method calls.
5. Asynchronous Programming
C# made async/await famous: asynchronous code reads like normal sequential code. Java offers CompletableFuture for composing asynchronous tasks, and since Java 21, virtual threads, which let you write simple blocking code that scales like asynchronous code.
var a = FetchPriceAsync("tea");
var b = FetchPriceAsync("coffee");
string[] both = await Task.WhenAll(a, b);
Console.WriteLine("Prices: " + string.Join(", ", both));
import java.util.concurrent.CompletableFuture;
public class AsyncDemo {
static String fetchPrice(String item) {
try { Thread.sleep(50); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
return item + "=42.0";
}
public static void main(String[] args) {
// Java: CompletableFuture (C# would use async/await with Task)
CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> fetchPrice("tea"));
CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> fetchPrice("coffee"));
String both = a.thenCombine(b, (x, y) -> x + ", " + y).join();
System.out.println("Prices: " + both);
}
}
Prices: tea=42.0, coffee=42.0
6. Type System Differences
- Value types: C# lets you define your own
structs, stored inline without object overhead. Java has only built-in primitives as value types (Project Valhalla aims to add value classes). - Generics: C# generics are reified, so
List<int>stores realints andtypeof(T)works at runtime. Java generics are erased, require boxing for primitives, and cannot usenew T(). - Nullability: C# has nullable reference types (
string?) checked by the compiler. Java relies onOptionaland annotations. - Unsigned types: C# has
uint,ulongand so on; Java does not. - Operator overloading and extension methods: available in C#, not in Java.
7. Exceptions
Java distinguishes checked exceptions, which the compiler forces you to catch or declare with throws, from unchecked ones. C# has only unchecked exceptions. Java's approach makes failure cases visible in method signatures; C#'s approach keeps code shorter. Both have automatic resource clean-up: Java's try-with-resources and C#'s using.
8. Ecosystem and Tooling
| Area | Java | C# |
|---|---|---|
| Web back-end | Spring Boot, Quarkus, Micronaut, Jakarta EE | ASP.NET Core |
| Database access | JPA/Hibernate, JDBC, Spring Data | Entity Framework Core, Dapper |
| Build and packages | Maven, Gradle, Maven Central | dotnet CLI, MSBuild, NuGet |
| IDEs | IntelliJ IDEA, Eclipse, VS Code | Visual Studio, Rider, VS Code |
| Mobile | Android (with Kotlin) | .NET MAUI, Xamarin |
| Games | LibGDX, Minecraft modding | Unity (major engine) |
| Desktop | JavaFX, Swing | WPF, WinForms, MAUI |
| Big data | Kafka, Hadoop, Spark, Flink, Elasticsearch | Fewer native options |
9. Performance
Both run on mature JIT-compiling runtimes with advanced garbage collectors, and their performance is broadly similar. C#'s value types and Span<T> help reduce allocations in hot code; Java's HotSpot and GraalVM compilers and low-pause collectors such as ZGC shine in large, long-running server applications. Both support ahead-of-time compilation (GraalVM native image for Java, Native AOT for .NET).
10. Which Should You Choose?
- Choose Java for enterprise back-ends, Android, big-data platforms, and when your organisation already uses the JVM ecosystem.
- Choose C# for Unity games, Windows desktop applications, and organisations built around Microsoft Azure and .NET.
- For general web APIs and cloud services, both are excellent; pick the one your team knows.
11. Summary
- Java and C# are similar managed, object-oriented languages running on the JVM and .NET respectively.
- C# has properties, LINQ,
async/await, structs, reified generics and operator overloading. - Java has checked exceptions, a huge open-source ecosystem, and virtual threads for simple scalable concurrency.
- Both are cross-platform today, and skills transfer easily between them.