📚 Cheatsheet

Une collection organisée de snippets de code pour accélérer votre développement. Parcourez, recherchez et copiez en un clic.

Snippets 5

Retour
Classe générique
Intermédiaire
public class Boite<T> {
   private T t;

   public void ajouter(T t) {
      this.t = t;
   }

   public T obtenir() {
      return t;
   }
}
Classe Record (Java 16+)
Intermédiaire
public record Utilisateur(int id, String nom) {}
// Génère automatiquement constructeur, getters, equals, hashCode, toString
Créer une annotation personnalisée
Avancé
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MaAnnotation {
    String valeur();
    int nombre() default 0;
}
Énumération (Enum)
Facile
public enum Jour {
    LUNDI, MARDI, MERCREDI, JEUDI, VENDREDI, SAMEDI, DIMANCHE
}
Pattern Matching pour instanceof (Java 16+)
Intermédiaire
Object obj = "hello";
if (obj instanceof String s) {
    // s est déjà casté en String
    System.out.println(s.toUpperCase());
}