- For the package
package com.sun2;public enum Seasons {SUMMER, FALL, WINTER, SPRING}
Valid import statements are:
import com.sun2.Seasons; // the class import static com.sun2.Seasons.*; //all enum valuesimport static com.sun2.Seasons.FALL; //only one enum value
- An interface can extend many interfaces
- Interfaces can have variables, overrides and overloads
- An enum can have methods and can override those methods for a specific enum constant
- var-args can be used to pass arrays, var args can be of 0 or more lengths
- A var-args argument must be a method’s last argument
- An override cannot change the return type. Override can use covariant as a return type
- Overloading does not depend on the return type
- From J2se 5.0, a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass
- When a subclass constructor does not explicitly invoke a superclass constructor, the no-argument constructor of the superclass is automatically called from the subclass constructor. If the super class does not have a no-argument constructor, a compile-time error is displayed
- When subclasses override superclass methods, these superclass methods can be accesses using the super keyword (from subclass methods)
- From java 1.5, enums can be used in a switch
- To enable asserts in java program use -ea switch.
- assert has two forms: assert Expression1 ;, assert Expression1 : Expression2 ;. In the first form only AssertionErorr is displayed, in the second form you can add additional message(Expression2) to the AssertionError. Assert when finds its arguments to be false, it throws AssertionError
- Good places to use assertions: Internal Invariants, Control-Flow Invariants, Preconditions, Postconditions, and Class Invariants
- bad use of assertions: argument checking in public methods, to do any work that your application requires for correct operation
- Assertions should be free of side effects: evaluating the expression should not affect any state that is visible after the evaluation is complete
- An overriding method does not need to throw the overridden method’s exception
- Nesting try/catches are ok and normal flow rules apply in case of nesting.
- Accessing uninitialized instance/class wrapper variables (Integer, Short) will cause NullPointerException
- NumberFormatException extends IllegalArgumentException
- JVM when calling a method, to make data types compatible, will widen the parameters before boxing.
- java.io.Console class: Only a single instance exists for a given JVM, All of the methods that read user input are synchronized
- Among File, FileReader, BufferedReader only BufferedReader has a readLine method
- A class’s superclasses don’t have to implement Serializable in order to be serialized
- if a superclass doesn’t implement Serializable then it’s constructor will run during deserialization.
- A transient variable’s state is lost during serialization
- A volatile variable’s state is not lost during serialization
- If Dog has-a Collar, then Collar must implement java.io.Serializable in order to serialize an instance of Dog.
- If Dog extends Animal and Animal implements java.io.Serializable but Dog does NOT implement java.io.Serializable, you can serialize an instance of Dog.
- Serialization rules: # Rule #1: The object to be persisted must implement the Serializable interface or inherit that implementation from its object hierarchy,# Rule #2: The object to be persisted must mark all nonserializable fields transient
- To make thread instance unserializable: transient private Thread animator;
- java.lang.Object does not implement Serializable. AWT and Swing GUI components, strings, and arrays — are serializable
- Serialization Details
- NumberFormat, Calendar, and DateFormat are abstract. Use the getInstance method to get instances.
- For java.util.Scanner, the default separator is a blank NOT a comma
- Multiple threads can be created using the same Runnable object, but a given thread can be started only once
- Two invocations of synchronized methods on the same object to interleave – is not possible. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object. When a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
- In object oriented design, low coupling is more desirable than high coupling. High cohesion is more desirable than low cohesion. Coupling: class interdependence. Cohesion: Class activities should be well focused.
- Polymorphism does NOT apply to static methods
- Polymorphic calls and co-variant returns can be mixed
- no modifier (package private) is weaker than protected. The order is: public, protected, package private, private
- Kind of’ translates to extends, ‘contract’ translates to implements, and ‘composed’ translates to a has-a implementation.
- TreeMap: headMap() method returns keys that are less than the argument, and tailMap() returns keys that are greater than(>=) the argument.
From: http://sitestree.com/?p=4888
Categories:Java Short Notes, SCJP
Tags:
Post Data:2011-10-20 04:13:02
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada