3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C# vs Java:コード対応表

3
Last updated at Posted at 2025-07-02

そっくり!やったね!!

機能 C# Java
クラス定義 public class Person { } public class Person { }
継承 class Student : Person { } class Student extends Person { }
インターフェース実装 class Car : IVehicle { } class Car implements Vehicle { }
コンストラクタ public Person(string name) { } public Person(String name) { }
プロパティ public string Name { get; set; } private String name; public String getName() { return name; }
文字列連結 "Hello " + name "Hello " + name
文字列補間 $"Name: {name}" String.format("Name: %s", name)
リスト定義 var list = new List<string>(); List<String> list = new ArrayList<>();
辞書(マップ) var dict = new Dictionary<int, string>(); Map<Integer, String> map = new HashMap<>();
foreachループ foreach (var item in list) for (String item : list)
例外処理 try { } catch (Exception e) { } try { } catch (Exception e) { }
nullチェック(簡易) if (value is not null) if (value != null)
null合体演算子 var name = input ?? "default"; String name = input != null ? input : "default";
ラムダ式 list.Where(x => x.Age > 20) list.stream().filter(x -> x.getAge() > 20)
非同期処理 async Task DoAsync() CompletableFuture<Void> doAsync()
属性/アノテーション [Serializable] @Serializable(例:独自定義)
エントリポイント static void Main(string[] args) public static void main(String[] args)
列挙型(Enum) enum Color { Red, Green, Blue } enum Color { RED, GREEN, BLUE }
ジェネリクス List<T> List<T>
名前空間 / パッケージ namespace MyApp package myapp;
using / import using System.Text; import java.util.*;
LINQ操作 list.Select(x => x.Name) list.stream().map(x -> x.getName())

🌐 Web開発フレームワーク(参考)

機能 ASP.NET Core (C#) Spring Boot (Java)
ルーティング [HttpGet("/hello")] @GetMapping("/hello")
コントローラー public class HelloController : ControllerBase @RestController public class HelloController
DI(依存性注入) services.AddScoped<IMyService, MyService>() @Autowired private MyService myService;
コンフィグ管理 appsettings.json application.yml / .properties

📝 サンプル比較:シンプルなクラス

🔷 C#

public class Person
{
    public string Name { get; set; }

    public Person(string name)
    {
        Name = name;
    }

    public void Greet() => Console.WriteLine($"Hello, {Name}!");
}

🔶 Java

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public void greet() {
        System.out.println("Hello, " + name + "!");
    }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

🔚 まとめ

  • 文法の違いはあれど、設計パターンやOOPの基本は共通
  • C#特有の「プロパティ」「LINQ」「非同期構文」などは、Javaではやや手間がかかる
  • Javaは明示的・保守性重視な記述が多いのが特徴

【C#エンジニア向け】Javaとの共通点・相違点・最速でキャッチアップする方法

3
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?