Assembly Mots clés Types primitifs boolean byte char double float int long short Visibilité private protected public Constantes true false null void Conditions if else switch case default break return Boucles continue do for while Déclarations abstract extends implements interface new package transient volatile Inutilisés const goto Exceptions try catch finally throw throws
Assembly Mots clés Types primitifs boolean byte char double float int long short Visibilité private protected public Constantes true false null void Conditions if else switch case default break return Boucles continue do for while Déclarations abstract extends implements interface new package synchronized transient volatile Inutilisés const goto Exceptions try catch finally throw throws
Assembly final • Variable : non modifiable – type primitif : constante – référence : référence constante, contenu modifiable – peut être initialisé plus tard (mais doit l’être avant utilisation) • Méthode : non surchargeable – Améliore performances
Assembly final • Variable : non modifiable – type primitif : constante – référence : référence constante, contenu modifiable – peut être initialisé plus tard (mais doit l’être avant utilisation) • Méthode : non surchargeable – Améliore performances – private -> final (implicitement) • Classe : non héritable • Argument : read-only
Assembly for each • Syntaxe : • Code compilé : • Possible d’itérer sur des collections customs : il suffit d’implémenter Iterable<T> for(T t : collection) { } Iterator<T> iterator = collection.getIterator(); while(iterator.hasNext()) { T t = iterator.next(); }
Assembly static • Variable : classe au lieu d’instance • Méthode : classe au lieu d’instance • Classe interne : indépendante de la classe conteneur • Bloc static : class MyClass { static { //Exécuté quand la classe est chargée sur la JVM } }
Assembly Threading • Thread : classe dont il faut hériter • Runnable : interface contenant la méthode run() MonThread t = new MonThread() ; t.start(); MonRunnable r = new MonRunnable() ; new Thread(r).start();
Assembly Generics • Utilisation classique : • Mieux : • Encore mieux : • Pour des classes : public MyClass<T> extends List<T> implements Cloneable<T> public String getRandom(List<String> list) {} public <T> T getRandom(List<T> list) {} public <T> T getRandom(List<? extends T> list) {}
Assembly Generics • Uniquement pour la compilation : type erasure • Héritage de generics : • Wildcard : <?> List<? extends MyClass> static <T extends Comparable<? super T>> void sort(List<T> list) Map<?, ?> A extends B List<A> vs List<B> ? List<A> vs List<? extends A> ? List<A> vs List<? extends B> ?
Assembly Réfléxivité : Class • Récupérer une classe • Méthodes sur la classe Class c = o.getClass(); Class c = Class.forName(className); Field getField(String name); Method getMethod(String name); Constructor getConstructor(Class[] parameters); Class[] getInterfaces Class getSuperclass(); Package getPackage();
Assembly Réfléxivité : Field et Method • Field • Method String getName(); Class getType(); int getModifier(); String getName(); Class getReturnType(); Class[] getParameterTypes(); int getModifiers(); Class[] getExceptionTypes();
Assembly Réfléxivité : exemples • Créer un objet • Accéder à un champ privé Class clazz = Class.forName("com.applidium.project.MyObject"); MyObject = clazz.newInstance(); Class clazz = object.getClass(); Field field = clazz.getDeclaredField("type"); field.setAccessible(true); field.get(object); field.set(object, null);
Assembly Pour aller plus loin Boxed Types Number Boolean Byte Character Double Float Integer Long Short String StringBuffer StringBuilder StringUtils Set AbstractSet TreeSet HashSet LinkedHashSet Collection AbstractCollection List Set List LinkedList AbstractList ArrayList Vector Serializable Cloneable Map SortedMap AbstractMap EnumMap HashMap TreeMap LinkedHashMap Comparable Comparator Object