Méthode factory Path.of()
Utilisez Path.of() — la méthode factory moderne dans l'interface Path.
Comparaison de Code
✕ Java 8
Path path = Paths.get("src", "main",
"java", "App.java");
✓ Java 11+
var path = Path.of("src", "main",
"java", "App.java");
Un problème avec ce code ? Dites-le nous.
Pourquoi la méthode moderne gagne
API cohérente
Suit le pattern factory .of() comme List.of(), Set.of().
Facile à découvrir
Se trouve dans le type Path lui-même, pas dans une classe Paths séparée.
Une classe de moins
Pas besoin d'importer la classe utilitaire Paths.
Ancienne Approche
Paths.get()
Approche Moderne
Path.of()
Depuis JDK
11
Difficulté
Débutant
Support JDK
Méthode factory Path.of()
Disponible
Disponible depuis JDK 11 (septembre 2018)
Comment ça fonctionne
Path.of() est une méthode factory ajoutée directement à l'interface Path, remplaçant la classe utilitaire Paths séparée. Elle est plus facile à découvrir et cohérente avec List.of(), Map.of(), etc.
Documentation Associée