Ir al contenido principal

Entradas

Mostrando entradas de mayo, 2017

Java Fundamentals - Persisting Objects

Serializing Storing an objet-graph to a byte stream Deserializing Restoring an objet-graph from a byte stream Serializing an Object ObjectOutputStream ObjectOutputStream out = new ObjectOutputStream( Files.newOutputStream (Path.get( filename )) ); out.writeObj ( object ); Deserializing an Object ObjectInputStream ObjectInputStream ois = new ObjectInputStream( Files.newInputStream (Path.get( filename )) ); ois.readObj ( ); Creating class version compatibility In a propmt inside /bin from our JDK Type:>   serialer  CLASS_PATH(Ej. com.comp.class) or Type: serialer -show  To have a GUI Delcare the field serialVersionUID as private static final long serialVersionUID = (Result from previous step); As long as all our classes contains the same serialVersionUID number will be compatible among them. Compare version using Reflection Once you get the ObjectOutputStream you can start using reflection. ...

Java Fundamentals - Annotations

Annotations Act as a metadata Preceded by @ @Override Check that the method signature override an interface method otherwise the compiler will send an error. @Deprecated The compiler indicate that this method is deprecated @SuppressWarnings("deprecation") Avoid that the compiler indicate the use of a deprecated method @Retention To indicate the availability according to the RetentionPolicy Values Java Source - Exist only in source file / Discarted by compiler Class - (Default value) Compile into the class file / Discarted by runtime Runtime - Loaded into runtime / Accesible with reflection @Target Define the elements where the annotation can be used: Target(ElementType.CONSTRUCTOR) Target(  { ElementType.CONSTRUCTOR,  ElementType.METHOD, ElementType.TYPE }) Create own annotations Declare the attibutes as methods Setting is similar to fields Declare annotation @Target(ElementType.TYPE) @...

Java Fundamentals - Reflection

Runtime information getClass method;  get the class description Class<?> c = obj.getClass(); c.getSimpleName(); Class.forName passing fully qualified type name c.forName("quelified_full_name") Class<?> c =  Class.forName("com.company.Bank") typeName.class getModifiers Return the class modifiers: public, private, protected, final int modifiers = c.getModifiers(); if( Modifier.isPublic(modifiers) )   System.out.println("Is a public method") Type Members Types declared & members only (Public, protected, private) getDeclaredFields getDeclaredMethods getDeclaredConstructors Type's declared & inherited members (Public only) getFields -  Return Field[] for(Field f: arr)     System.out.println(f.getName() +" " + f.getType() ); getMethods - Return Methods[] To avoid methods from the Object class   Methods[] me = c.getMethods();   for(Meth...

Java Fundamentals - Threads

Threads ExecutorService Creates a thread pool and wait until all the elements had completed his job ExcecutorService es = Executors.newFixedThreadPool( number_of_threads_to_work_); submit add and execute the thread es.submit( thread_class ); Avoid to add more work to the ExecutorService es.shutdown() Synchonized To be used in a method to a piece of code to make not thread safe classes in safe classes to be used by threads public void synchonized add(number ){    // This method will be blocked to be used by one thread at the time } public void add(){   //   synchonized ( number ) {    // The code inside this block will be tread safe  // The code can inlcude synchonized methods    } } Blocking Queues LinkedBlockingQueue PriorityBlockingQueue