-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpath-of.yaml
More file actions
43 lines (43 loc) · 1.38 KB
/
path-of.yaml
File metadata and controls
43 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
---
id: 60
slug: "path-of"
title: "Path.of() factory"
category: "io"
difficulty: "beginner"
jdkVersion: "11"
oldLabel: "Java 8"
modernLabel: "Java 11+"
oldApproach: "Paths.get()"
modernApproach: "Path.of()"
oldCode: |-
Path path = Paths.get("src", "main",
"java", "App.java");
modernCode: |-
var path = Path.of("src", "main",
"java", "App.java");
summary: "Use Path.of() — the modern factory method on the Path interface."
explanation: "Path.of() is a factory method added directly to the Path interface,\
\ replacing the separate Paths utility class. It's more discoverable and consistent\
\ with List.of(), Map.of(), etc."
whyModernWins:
- icon: "📐"
title: "Consistent API"
desc: "Follows the .of() factory pattern like List.of(), Set.of()."
- icon: "📖"
title: "Discoverable"
desc: "Found on the Path type itself, not a separate Paths class."
- icon: "🧹"
title: "One less class"
desc: "No need to import the Paths utility class."
support:
state: "available"
description: "Widely available since JDK 11 (Sept 2018)"
prev: "io/inputstream-transferto"
next: "io/try-with-resources-effectively-final"
related:
- "io/inputstream-transferto"
- "io/reading-files"
- "io/try-with-resources-effectively-final"
docs:
- title: "Path.of()"
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/nio/file/Path.html#of(java.lang.String,java.lang.String...)"