Monday, 10 March 2014

Why do we have new java.time package introduced in java 8 ?


Why do we have new java.time package introduced in java 8 ?

Java 8 : The new Date and Time API was developed to overcome numerous problems with Java's previous date and time APIs.

 As a result, it has been architected around a number of important design principles:

Immutability and thread safety: All of the Date and Time API's core classes are immutable, which ensures that you don't have to worry about threading issues caused by lack of thread synchronization. Immutable objects are simple to construct, use, and test, they make for good hash keys, etc.

Fluency: Date and Time presents a fluent interface, which should make its methods more readable and easier to learn, especially when chained together. Fluent factory methods (e.g., now(), from(), and of-prefixed methods) are used as an alternative to constructors. You'll also be able to use with-prefixed methods if you need to return a copy of the current instance with additional information.

Clarity: Each method in the Date and Time API is well-defined and clear about what it accomplishes. Additionally, Date and Time rejects null arguments early. Unless otherwise noted, passing a null argument to a method in any class or interface will cause a NullPointerException to be thrown. Validation methods that take object arguments and return Boolean values are an exception: they generally return false when null is passed.

Extensibility: The Strategy design pattern is used throughout the API to allow for extension while avoiding confusion. For example,  you could even introduce your own calendar.


Difference between Nested and Inner class ?


Difference between Nested and Inner class ?

A nested class is a class defined inside the definition (body) of another enclosing class.  A nested class is treated as a member of the enclosing class. 

A nested class can be of two types
- static
a) A static nested class is not very tightly integrated with the enclosing class and it lives its own independent life as well i.e., a static nested class can be instantiated like any other class from just about anywhere.
b) They are defined inside the definition of an enclosing class just to get a logical grouping of the classes, which in turn increases readability and provides better packaging convenience.
c) A static nested class can't directly access the members of the enclosing class. Like any other top-level class it needs to access the members of the enclosing class via object references only.

-  non-static
d) A non-static nested class is called an inner class and it's tightly integrated with the enclosing class unlike a static nested class.
e) An inner class instance can't exist independent to the enclosing class instance. An inner class instance always exist within the instance of the enclosing class.
f) Since, it's always associated with the enclosing class instance and it has direct access to all the members of the enclosing class - even the private members.