Wednesday 21 February 2018

Java 9 Features

Java 9 Features
Oracle has released Java 9 with rich set of new features.
 It includes various upgrades to the Java programming, JVM, Tools and libraries.
In this tutorial, we will discuss all the main features that are given below.
  • Platform Module System (Project Jigsaw)
  • Interface Private Methods
  • Try-With Resources
  • Anonymous Classes
  • @SafeVarargs Annotation
  • Collection Factory Methods
  • Process API Improvement
  • New Version-String Scheme
  • JShell: The Java Shell (REPL)
  • Process API Improvement
  • Control Panel
  • Stream API Improvement
  • Installer Enhancement for Microsoft windows and many more

Java Platform Module System (Project Jigsaw)

It is a new kind of Java programing component that can be used to collect Java code (classes and packages). The main goal of this project is to easily scale down application to small devices. In Java 9, JDK itself has divided into set of modules to make it more lightweight. It also allows us to develop modular applications.
For more information click here

Interface Private Methods

In Java 9, we can create private methods inside an interface. Interface allows us to declare private methods that help to share common code between non-abstractmethods.
Before Java 9, creating private methods inside an interface cause a compile time error. For more information click here

Try-With Resources

Java introduced try-with-resource feature in Java 7 that helps to close resource automatically after being used.
In other words, we can say that we don't need to close resources (file, connection, network etc) explicitly, try-with-resource close that automatically by using AutoClosable interface.
In Java 7, try-with-resources has a limitation that requires resource to declare locally within its block.
For more information click here

Anonymous Classes Improvement

Java 9 introduced a new feature that allows us to use diamond operator with anonymous classes. Using the diamond with anonymous classes was not allowed in Java 7.
In Java 9, as long as the inferred type is denotable, we can use the diamond operator when we create an anonymous inner class.
For more information click here

Java @SafeVarargs Annotation

It is an annotation which applies on a method or constructor that takes varargs parameters. It is used to ensure that the method does not perform unsafe operations on its varargs parameters.
It was included in Java 7 and can only be applied on
  • Final methods
  • Static methods
  • Constructors
For more information click here

Java Collection Factory Methods

Factory methods are special type of static methods that are used to create unmodifiable instances of collections. It means we can use these methods to create list, set and map of small number of elements.
It is unmodifiable, so adding new element will throw java.lang.UnsupportedOperationException
For more information click here

Java Process API Improvement

Java has improved its process API in Java 9 version that helps to manage and control operating system processes.
In earlier versions, it was complex to manage and control operating system processes by using Java programming. Now, new classes and interfaces are added to perform this task.
For more information click here

Java New Version-String Scheme

Java version-string is a format that contains version specific information. This version-string consists of major, minor, security and patch update releases.
In Java 9, a new version-string scheme is introduced. For more information click here.

JShell: The Java Shell (REPL)

It is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and run from the command line. It is benificial, if we want to test our business logic and get result immediately. For more information click here

Java 9 Control Panel

Java control panel is used to control Java applications that are embedded in browser. This control panel maintains the settings that manage Java application embedded in browser.
In Java 9, control panel was rewritten as a JavaFX application and the storage location has changed. For more information click here.

Java 9 Stream API Improvement

In Java 9, Stream API has improved and new methods are added to the Stream interface. TakeWhile, dropWhile and ofNullable, and one overloaded iterate method are added to perform operations on stream elements. For more information click here.

Installer Enhancement for Microsoft windows

Java 9 includes improved version of Microsoft Windows installer and added the following feature.
This installer allows us to enable and disable web deployement. We can enable web deployment by selecting Custom Setup, install and click on the checkbox from the welcome page of the installer.

Installer Enhancements for macOS

Java 9 included the following features to the macOS installer.
FeatureDescription
CPU Version AvailabilityIt provides notification on next CPU availability after uninstalling the current CPU version.
User ExperienceIt enhanced user experience while updating the JRE.

Add More Diagnostic Commands

Java 9 has added some new commands to improve the diagnose issues. These diagnostic command are used to diagnose Hotspot and JDK.
The Java jcmd utility can be used to send diagnostic command requests to a running Java Virtual Machine (JVM).

Remove Launch-Time JRE Version Selection

Java 9 has removed JRE (Java Runtime Environment) version selection at launch time. Now days, modern application has own active installer that further contains methods to manage the JRE. that?s why JRE version selection has been removed.

Remove the JVM TI hprof Agent

Java 9 has removed the hprof from the existed JDK. It was not intended to be a production tool. Some better featured of the hprof agent have been superseded for better alternatives.

Remove the Jhat Tool

Java has removed the jhat tool in its new release JDK 9. it was an experimental and unsupported tool added in JDK 6, now has outdated.

Validate JVM Command-Line Flag Arguments

Java validates arguments to all numeric JVM command-line flags to avoid failure. If arguments are invalid or out-of-range, it displays an appropriate error message.
Constraint check has been implemented for range and optional that require numeric value.

jlink: The Java Linker

jlink is a tool that can be used to assemble set of modules into a runtime image. It also allows us to assemble module's dependencies into the custom runtime image.
Link time is a phase between the compile and runtime, jlink works there for linking and assemble modules to runtime image.

Datagram Transport Layer Security (DTLS)

DTLS is a protocol which is used to construct TLS over datagram. JSSE (Java Secure Socket Extension) API support DTLS protocol and both versions 1.0 and 1.2.
The TLS protocol requires a TCP, So it can't be used to secure unreliable datagram traffic.

TLS Application-Layer Protocol Negotiation Extension

This extension allows the client and server in TLS connection to negotiate for application protocol. Client server communicate and inform to each other about supported application protocols. The application protocol negotiation accomplished within the TLS handshake.

OCSP Stapling for TLS

OCSP (Online Certificate Status Protocol) helps to the server in a TLS connection to check for a revoked X.509 certificate revocation. During TLS handshaking server contact an OCSP responder for the certificate. Server then staple the revocation information tot the certificate at client.

DRBG-Based SecureRandom Implementation

Java 9 includes the functionality of DRBG (Deterministic Random Bit Generator) mechanisms as specified in NIST SP 800-90Ar1 in the SecureRandom API.
The DRBG mechanisms use modern algorithms as strong as SHA-512 and AES-256. Each of these mechanisms can be configured with different security strengths and features to match user requirements.

Disable SHA-1 Certificates

The security configuration of the JDK has improved. It provides more flexible mechanism to disable X.509 certificate having SHA-1-based signatures.
The jdk.certpath.disabledAlgorithms security property is enhanced with several new constraints that allow greater control over the types of certificates that can be disabled.

Create PKCS12 Keystores by Default

The default keystore type has modified from JKS to PKCS12. The PKCS is an extensible, standard, and widely supported format for storing cryptographic keys. It improves confidentiality by storing private keys, trusted public key certificates etc. This feature also opens opportunities for interoperability with other systems such as Mozilla, Microsoft's Internet Explorer, and OpenSSL that support PKCS12.

SHA-3 Hash Algorithms

New Java version supports SHA-3 cryptographic hash functions.
The java.security.MessageDigest API supports various algorithms like: SHA3-224, SHA3-256, SHA3-384, and SHA3-512.
The following providers support SHA-3 algorithm enhancements:
  • SUN provider: SHA3-224, SHA3-256, SHA3-384, and SHA3-512
  • OracleUcrypto provider: SHA-3 digests supported by Solaris 12.0

Deprecate the Java Plug-in

Java Plug-in and applet technologies has deprecate in JDK 9. In future releases, these technologies will be removed.
Java Plug-in is require to run applet and JavaFX applications in web browser. It is recommended to rewrite applications as Java Web applications.

Enhanced Java Control Panel

Java control panel has improved, information is easier to locate, a search field is available and modal dialog boxes are no longer used. Note that the location of some options has changed from previous versions of the Java Control Panel.

Modular Java Application Packaging

Java new version integrates features from Project Jigsaw into the Java Packager, including module awareness and custom runtime creation.
We can use jlink tool to create smaller packages.

Simplified Doclet API

The old Doclet API is replaced with a new simplified API that leverages other standard, existing APIs. In Java 9, the standard doclet has been rewritten to use the new Doclet API.

Compiler Control

Now, we can control JVM compilation through compiler directive options. The level of control is runtime-manageable and method-specific. Compiler Control supersedes, and is backward compatible, with CompileCommand.

Segmented Code Cache

Code cache is divided into distinct segments. Each segment is a compiled code and improve performance and enable extensibility.

Unified JVM Logging

Java new version introduces a common logging system for all components of the JVM.

Remove GC Combinations Deprecated in JDK 8

Deprecated garbage collector combination has removed from JDK 9. Following are the garbage collector combination that are not supported in new version.
  • DefNew + CMS
  • ParNew + SerialOld
  • Incremental CMS

Unified GC Logging

Garbage collection logging is reimplemented by using the unified JVM logging framework.

Deprecate the Concurrent Mark Sweep (CMS) Garbage Collector

The Concurrent Mark Sweep (CMS) garbage collector is deprecated. It issue a warning message when requested on the command line. The Garbage-First (G1) garbage collector is intended to be a replacement for most uses of CMS.

Process API Updates

Process API has improved with methods and interfaces. This API handles the operating system processes.
The ProcessHandle class is used to get process's native process ID, arguments, command, start time, accumulated CPU time, user, parent process, and descendants.

Compact Strings

In new version, Java uses more space-efficient internal representation for strings. In previous versions, the String was stored in char array and takes two bytes for each character. Now, the new internal presentation of the string is a byte class.

Platform Logging API and Service

The java.util.logging API helps to log messages, together with a service interface for consumers. An application or library can provide implementation of this service to log messages to logging framework. It uses default implementation, if no implementation is provided.

More Concurrency Updates

More concurrency updates are added to JDK 9. These updates are improved CompletableFuture API and interoperable publish-subscribe framework.

XML Catalogs

Standard XML catalog API is added which supports the organization for the Advancement of OASIS (Structured Information Standards) XML Catalogs version 1.1. This API consists of catalog-resolver which can be used as an intrinsic with the JAXP processors.

Convenience Factory Methods for Collections

This feature makes easier to create instances of collection and maps with small number of elements. Some new methods are added to List, Set and Map to create immutable instances. For example:
Set<String> alphabet = Set.of(1, 2, 3);

Enhanced Deprecation

The @Deprecated annotation is improved to provide better information about the status and intended disposition of an API. Following new elements are added.
  • @Deprecated(forRemoval=true): It indicates that the API will be removed from the future release of Java.
  • @Deprecated(since="version"): It contains the Java SE version string that indicates deprecared API element for Java 9.

Spin-Wait Hints

It enables Java code to inform that a spin loop is executing. A spin loop repeatedly checks to see if a condition is true, such as when a lock can be acquired, after which some computation can be safely performed followed by the release of the lock. This API is purely a hint, and carries no semantic behavior requirements.

Filter Incoming Serialization Data

It helps to filter date of incoming stream of object-serialization data to improve both robustness and security. Object-serialization clients can validate their input more easily, and exported Remote Method Invocation (RMI) objects can validate invocation arguments more easily as well.
Serialization clients implement a filter interface that is set on an ObjectInputStream. For RMI, the object is exported through a RemoteServerRef that sets the filter on the MarshalInputStream to validate the invocation arguments as they are unmarshalled.

Stack-Walking API

Java included a stack-walking API that allows lazy access of information in stack. It also allows easy filtering of information.
This stack-walking API allows access to Class objects, if the stack walker is configured.

Parser API for Nashorn

Java added Parser API which allows use to Enable applications, in server-side framework, particular IDEs etc..
It can be used to parse ECMAScript code from a string, URL, or file with methods of Parser class. Methods of this class return an object of CompilationUnitTree class, which represents ECMAScript code as an abstract syntax tree.
Nashorn parser API is located into jdk.nashorn.api.tree package.

Implement Selected ECMAScript 6 Features in Nashorn

Java added some new features to Nashorn in the 6th edition of ECMA-62. Following are the Implemented features.
  • Template strings
  • let, const, and block scope
  • Iterators and for..of loops
  • Map, Set, WeakMap, and WeakSet
  • Symbols
  • Binary and octal literals

Prepare JavaFX UI Controls and CSS APIs for Modularization

Java included public APIs for CSS functionality and JavaFX UI controls. These functionalities previously available only through internal packages, but now can be accessible because of modular approach.
A new package javafx.scene.control.skin is included that consists of a set of classes to provide a default implementation of each UI (User Interface) control.

BeanInfo Annotations

The @beaninfo Javadoc tag is replaced with the annotation types JavaBean, BeanProperty, and SwingContainer.
We can use these attributed directly in the Bean class. It also allows auto removal for automatically created classes and set the corresponding feature attributes during BeanInfo generation at runtime.

TIFF Image I/O

TIFF (Tag Image File Format) is added for reading and writing as standard. It is located into the package javax.imageio. One more new package javax.imageio.plugins.tiff is added to provide classes that simplify the optional manipulation of TIFF metadata.

HiDPI Graphics on Windows and Linux

Automatically scales and sizes AWT and Swing components for High Dots Per Inch (HiDPI) displays on Windows and Linux.
The JDK already supports HiDPI "retina displays" on OS X.
Prior to this release, on Windows and Linux, Java applications were sized and rendered based on pixels, even on HiDPI displays that can have pixel densities two to three times as high as traditional displays. This led to GUI components and windows that were too small to read or use.

Platform-Specific Desktop Features

Some new methods are added to the java.awt.Desktop class. These methods provides the following features.
  • Show custom About and Preferences windows.
  • Handle requests to open or print a list of files.
  • Handle requests to open a URL.
  • Open the native help viewer application.
  • Set the default menu bar.
  • Enable or disable the application to be suddenly terminated.

Enable GTK 3 on Linux

Java new version allows Java graphical applications(JavaFX, Swing, or Abstract Window Toolkit) to use GTK version on Linux.
JDK uses GTK +2 by default, if it is not available, GTK+3 is used.
We can use specific version by setting property of jdk.gtk.version.

Unicode 8.0

Java supports Unicode 8.0 in its new Java 9 version, previously Unicode 6.2 was used.
Java 9 supports, Unicode 6.3, 7.0 and 8.0 standards that combined introduced 10,555 characters, 29 scripts, and 42 blocks.

CLDR Locale Data Enabled by Default

CLDR (Common Locale Data Repository's) represents the locale data provided by the Unicode CLDR project. It was first added in JDK 8 and now default in JDK 9.
We can enable behavior compatible with Java 8 by setting the property java.locale.providers to a value with COMPAT ahead of CLDR.

UTF-8 Properties Files

The UTF-8 is a convenient way to represent non-Latin characters. The new version of java loads properties files in UTF-8 encoding. In earlier versions, ISO-8859-1 encoding was used when loading property resource bundles.
  • Java removes the endorsed-standards override mechanism and the extension mechanism.
  • The rt.jar and tools.jar both has been removed from JRE.
  • JDK's internal APIs are inaccessible by default.



Sunday 18 February 2018

List Of Popular Open Source Java Build





List Of Popular Open Source Java Build Tools


In this article, I will be sharing some popular, open source java build tools  with its pros and cons.

Java Build Tools

1. Ant with ivy
2. Maven
3. Gradle

Apache Ant With Ivy

Ant is java library, which helps to drive the process defined in the build file.
Mainly Ant is used to build java applications. Ant is very flexible, it does not impose any rules like coding conventions, directory structure. Ivy is a subproject of Ant, which acts as a dependency manager.
Here you can refer some ant scripts: Ant Scripts

Apache Maven

Maven is more than a build tool. Maven even describes how software is built and helps in dependency management also. Maven is used mainly for java based projects.

Gradle

Gradle is built upon the concepts of ant and maven. Gradle uses groovy scripts for declaring project configuration. Gradle was designed for multi-project builds and supports incremental builds by determining which parts of the build are up-to-date.
Ant is mostly treated as legacy right now. Industry going forward with Gradle build tool. I personally feel, Ant and Maven still we can use, it mainly depends on the project. Sometimes we can use a combination of Ant and Gradle, Maven and Gradle, or even three together.

ANT Vs Maven:

Let me share with you some of the important differences between ANT and Maven.
1) ANT – we need to provide information about the project structure whereas Maven has a convention to place source code, compiled code, packages etc., So for Maven we no need to provide information about the project.
2) Maven has a life cycle of the build process, whereas ANT does not have.
3) Maven is a framework and Ant is just a tool.
4) Ant is mainly for build tool and Maven is mainly project management tool.
5) Ant is less preferred and Maven is more preferred than Ant.

Maven Vs Gradle :

Let me share with you some of important features or reasons, which Gradle is having and Maven does not have. These features promoting Gradle to choose over other build tools.
1) When come to performance, Gradle has features like Incremental build and Compiler Daemon, which increases dramatic speed and reduced build time.
2) Ant integration with Gradle works very well.
3) Google adopted Gradle as the default build tool for android projects. so Gradle has more support for better building Android projects.
4) Gradle releases a new version usually every 6 to 8 weeks and adds new features every release.
For more and detailed comparison between Maven and Gradle. Please refer thislink. 

Conclusion

In this article, we have learned about some of the popular open source java build tools. Selecting a build tool for your java projects depends on your choice. If you have any queries regarding the build tools, leave a comment below.

Saturday 3 February 2018

Angular interview question-part-1

Explain the life cycle hooks of Angular 2 application

Angular 2 component/directive has lifecycle events, managed by @angular/core. It creates the component, renders it, creates and renders its children, processes changes when its data-bound properties change, and then destroys it before removing its template from the DOM. Angular provides a set of lifecycle hooks(special events) which can be tapped into this lifecycle and perform operations when required. The constructor executes prior to all lifecycle events. Each interface has a single hook method prefixed with ng. For example, ngOnint interface has Oninit method that must be implemented in the component. 
Some of the events are applicable for both component/directives while few are specific to components.
  • ngOnChanges: Responds when angular sets its data-bound property which receives the current and previous object values.
  • ngOnInit: Initializes the component/directive after first ngOnChange triggers. This is most frequently used method to retrieve the data for the template from a back-end service.
  • ngDoCheck: Detect and act upon changes occuring outside Angular context. It is called when every change detection run.
  • ngOnDestroy: Cleanup just before Angular destroys the directive/component. Unsubscribe observables and detach event handlers to avoid memory leaks.
Component-specific hooks:
  • ngAfterContentInit: Component content has been initialized
  • ngAfterContentChecked: After Angular checks the bindings of the external content that it projected     into its view.
  • ngAfterViewInit: After Angular creates the component’s view.
  • ngAfterViewChecked: After Angular checks the bindings of the component’s view.

What are the advantages of using Angular 2 over Angular 1?

  1. Angular 2 is a platform not only a language:
  2. Better Speed and Performance: No $Scope in Angular 2, AOT
  3. Simpler Dependency Injection
  4. Modular, cross platform
  5. Benefits of ES6 and Typescript.
  6. Flexible Routing with Lazy Loading Features
  7. Easier to Learn

How routing works in Angular 2.

Routing is a mechanism which enables user to navigate between views/components. Angular 2 simplifies the routing and provide flexibility to configure and define at module level (Lazy loading). 
The angular application has single instance of the Router service and whenever URL changes, corresponding Route is matched from the routing configuration array. On successful match, it applies redirects and the router builds a tree of ActivatedRoute objects and contains the current state of the router. Before redirection, the router will check whether new state is permitted by running guards (CanActivate). Route Guards is simply an interface method that router runs to check the route authorization. After guard runs, it will resolve the route data and activate the router state by instantiation the required components into <router-outlet> </router-outlet>.
Further Reading:

What are Event Emitters and how it works in Angular 2?

Angular 2 doesn’t have bi-directional digest cycle, unlike angular 1. In angular 2, any change occurred in the component always gets propagated from the current component to all its children in hierarchy. If the change from one component needs to be reflected to any of its parent component in hierarchy, we can emit the event by using Event Emitter api.
In short, EventEmitter is class defined in @angular/core module which can be used by components and directives to emit custom events.
@output() somethingChanged = new EventEmitter();
We use somethingChanged.emit(value) method to emit the event. This is usually done in setter when the value is being changed in the class.
This event emit can be subscribed by any component of the module by using subscribe method.
myObj.somethingChanged.subscribe(val) => this.myLocalMethod(val));
Further Reading:

What is the use of codelyzer in angular 2 application.

All enterprise applications follows a set of coding conventions and guidelines to maintain code in better way. Codelyzer is an open source tool to run and check whether the pre-defined coding guidelines has been followed or not. Codelyzer does only static code analysis for angular and typescript project.
Codelyzer runs on top of tslint and its coding conventions are usually defined in tslint.json file. Codelyzer can be run via angular cli or npm directly. Editors like Visual Studio Code and Atom also supports codelyzer just by doing a basic settings.
To set up the codelyzer in Visual Studio code, we can go to File -> Preferences -> User Settings and add the path for tslint rules.
{
  "tslint.rulesDirectory": "./node_modules/codelyzer",
  "typescript.tsdk": "node_modules/typescript/lib"
}
To run from cli: ng lint
To run from npm: npm run lint
Further Reading:

What is lazy loading and How to enable lazy loading in angular 2?

Most of the enterprise application contains various modules for specific business cases. Bundling whole application code and loading will be huge performance impact at initial call. Lazy lading enables us to load only the module user is interacting and keep the rest to be loaded at runtime on demand.
Lazy loading speeds up the application initial load time by splitting the code into multiple bundles and loading them on demand.
Every Angular application must have one main module say AppModule. The code should be splitted into various child modules (NgModule) based on the application business case.
Plunkr Example: Link
  1. We don't require to import or declare lazily loading module in root module.
  2. Add the route to top level routing (app.routing.ts) and set loadChildren. loadChildren takes absolute path from root folder followed by #{ModuleName}. RouterModule.forRoot() takes routes array and configures the router.
  3. Import module specific routing in the child module.
  4. In the child module routing, specify path as empty string ' ', the empty path. RouterModule.forChild again takes routes array for the child module components to load and configure router for child.
  5. Then, export const routing: ModuleWithProviders = RouterModule.forChild(routes);

What are the security threats should we be aware of in angular 2 application?

Just like any other client side or web application, angular 2 application should also follow some of the basic guidelines to mitigate the security risks. Some of them are:
  1. Avoid using/injecting dynamic Html content to your component.
  2. If using external Html, that is coming from database or somewhere outside the application, sanitize it.
  3. Try not to put external urls in the application unless it is trusted. Avoid url re-direction unless it is trusted.
  4. Consider using AOT compilation or offline compilation.
  5. Try to prevent XSRF attack by restricting the api and use of the app for known or secure environment/browsers.
Further Reading:

How would you optimize the angular 2 application for better performance?

Well, optimization depends on the type and size of application and many other factors. But in general, I would consider the following points while optimizing the angular 2 app:
  1. Consider AOT compilation.
  2. Make sure the application is bundled, uglified, and tree shaking is done.
  3. Make sure the application doesn’t have un-necessary import statements.
  4. Make sure that any 3rd party library, which is not used, is removed from the application.
  5. Have all dependencies and dev-dependencies are clearly separated.
  6. I would consider lazy loading instead of fully bundled app if the app size is more.
Further Reading:

How would you define custom Typings to avoid editor warnings?

Well, in most of the cases, the 3rd party library comes with its own .d.ts file for its type definition. In some cases, we need to extend the existing type by providing some more properties to it or if we need to define additional types to avoid Typescript warning.
If we need to extend the type definition for external library, as a good practice, we should not touch the node_modules or existing typings folder. We can create a new folder, say “custom-typings” and keep all customized type definition in that.
To define typings for application (JavaScript/Typescript) objects, we should define interfaces and entity classes in models folder in the respective module of the application.
For those cases, we can define or extend the types by creating our own “.d.ts” file.
Further Reading:

What is shadow DOM? How is it helping Angular 2 to perform better?

Shadow DOM is a part of the HTML spec which allows developers to encapsulate their HTML markup, CSS styles and JavaScript. Shadow DOM, along with a few other technologies, gives developers the ability to build their own 1st class tags, web components and APIs just like the <audio> tag. Collectively, these new tags and APIs are referred to as Web Components. Shadow DOM provides better separation of concern along with lesser conflict of styles and scripts with other HTML DOM elements.
Since shadow DOM are static in nature, it’s a good candidate to be cached as it is not accessible to developer. The cached DOM would be rendered faster in the browser providing better performance. Moreover, shadow DOM can be managed comparatively well while detecting the change in angular 2 application and re-paint of view can be managed efficiently.
References/Further Reading:

What is AOT compilation?

AOT compilation stands for Ahead Of Time compilation, in which the angular compiler compiles the angular components and templates to native JavaScript and HTML during the build time. The compiled Html and JavaScript is deployed to the web server so that the compilation and render time can be saved by the browser.
Advantages
  1. Faster download: Since the app is already compiled, many of the angular compiler related libraries are not required to be bundled, the app bundle size get reduced. So, the app can be downloaded faster.
  2. Lesser No. of Http Requests: If the app is not bundled to support lazy loading (or whatever reasons), for each associated html and css, there is a separate request goes to the server. The pre-compiled application in-lines all templates and styles with components, so the number of Http requests to the server would be lesser.
  3. Faster Rendering: If the app is not AOT compiled, the compilation process happens in the browser once the application is fully loaded. This has a wait time for all necessary component to be downloaded, and then the time taken by the compiler to compile the app. With AOT compilation, this is optimized.
  4. Detect error at build time: Since compilation happens beforehand, many compile time error can be detected, providing a better degree of stability of application.
Disadvantages
  1. Works only with HTML and CSS, other file types need a previous build step
  2. No watch mode yet, must be done manually (bin/ngc-watch.js) and compiles all the files
  3. Need to maintain AOT version of bootstrap file (might not be required while using tools like cli)
  4. Needs cleanup step before compiling
References/Further Reading:

What are the core differences between Observables and Promises?

A nice answer taken from stack overflow: 
A Promise handles a single event when an async operation completes or fails.
Note: There are Promise libraries out there that support cancellation, but ES6 Promise doesn't so far.
An Observable is like a Stream (in many languages) and allows to pass zero or more events where the callback is called for each event. Often Observable is preferred over Promise because it provides the features of Promise and more. With Observable it doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same API in each case. Observable also has the advantage over Promise to be cancelable. If the result of an HTTP request to a server or some other expensive async operation isn't needed anymore, the Subscription of an Observable allows to cancel the subscription, while a Promise will eventually call the success or failed callback even when you don't need the notification or the result it provides anymore. Observable provides operators like map, forEach, reduce, ... similar to an array. There are also powerful operators like retry(), or replay(), ... that are often quite handy.
Promises vs Observables
  • Promises:
    1. returns a single value
    2. not cancellable
  • Observables:
  1. works with multiple values over time
  2. cancellable
  3. supports map, filter, reduce and similar operators
  4. proposed feature for ES 2016
  5. use Reactive Extensions (RxJS)
  6. an array whose items arrive asynchronously over time
References/Further Readings:

Explain local reference variables, ViewChild, and ContentChild.

Local template variables in angular2 is used to refer HTML elements and use their properties to access siblings or children.
Let’s consider you have an input field named username.
<input type="text" required ... />
This HTMLInputField can be made available to the template using # symbol with a variable name say username.
 <input type="text" #username required ... />
Now, this HTMLInputElement can be accessed from anywhere in the current template for example, checking validation and showing appropriate message based on the validation rule. But, username HTML reference is not accessible in the component/directive.
To access this in the component, angular 2 provides @ViewChild decorator which accepts the local reference variable.
@ViewChild('username') username: HTMLInputElement;
ViewChild element can be read after the view is initialized (ngAfterViewInit).
ContentChild is used to query the reference of the DOM within ng-content. Content Child are set before the ngAfterContentInit lifecycle hook.
For example:
// <code>app.component.ts</code>
<my-component>
    <p #contentRef>{{test}}</p>
</ my-component >
 
// MyComponent.component.ts
@Component({
    selector: ‘my-component',
    template: `
    <ng-content></ng-content>
    <div> ContentChild Example </div>
})
export class LifecycleComponent implements ngAfterContentInit{
                @ContentChild(‘contentRef’)   childContent: HTMLElement;
 
ngAfterContentInit() {
              this.log('ngAfterContentInit');
console.log(this.childContent);
    }
}
Further Reading:

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...