JAVA Notes

JAVA Notes

Added various java related references e.g. java 8 release notes, features of java 8 like optional, streams, lambda and some important concepts around it.

Updates on Java-9

1 Jshell

2 JPMS

3 JLink

4 HTTP/2

5 Process API Updates

6 Private Methods Inside Interfaces

7 Try With Resources Enhancement

8 Factory Methods to Create Unmodifiable Collections

9 Stream APIs enhancements

10 Diamond Operators Enhancement

11 Safe VarArgs Annotations

12 G1 Garbage Collector

 

Avoid finally while opening/closing file etc stuff

https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html

(Add the code like)

try(Reader reader = Helper.openReader("file1.txt))

{

} catch (IOException e)

{

System.out.println (e.getClass.getSimpleName() + "-" + e.getMessage())

}
public interface AutoCloseable
An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.

Notes Writing still in progress

Reference :

https://docs.oracle.com/javase/9/whatsnew/toc.htm#JSNEW-GUID-527735CF-44E1-4144-919B-E7D7CC9CDD4D

https://www.oracle.com/java/java9.html

https://www.journaldev.com/13121/java-9-features-with-examples

https://en.wikipedia.org/wiki/Java_version_history

Common Collections Methods

– size
– clear
– isEmpty
– add
– addAll
– contains – return true if contains element
– containsAll Return true if contains all members of another collection
– remove Remove Element
– removeAll – remove all elements contained in another collection
– retainAll Remove all elements not contained in another collection

Java 8 Collection Methods

– Lambda Expression – Pass code as arguments

Collection methods that leverage lambdas

– forEach

ArrayList<MyClass> list = new ArrayList<>();

MyClass v1 = new MyClass ("v1", "abc");
 MyClass v2 = new MyClass ("v2", "xyzzy");
 MyClass v3 = new MyClass ("v3", "abc");

list.add(v1);
 list.add(v2);
 list.add(v3);

list.removeIf(m->m.getValue().equals("abc"));

list.forEach(m->System.out.println(m.getLabel())); (Only v2)

Collection Interfaces

Collection Classes

Collection – Basic Collection Operations

List - Collection that maintains a particular order

Queue - Collection with the concept of order and specific c"head" element

Set - Collection that contains no duplicate values

SortedSet - A Set whose members are sorted

ArrayList (A list backed by resizable array)

LinkedList (A List and Queue backed y a doubly -linked list)

HashSet ( A Set implemented as a hash table)

TreeSet - A SortedSet implemented as a balanced binary tree

Maps Store key/valye pairs

– key used to identify/locate values
– Keys are unique
– Values can be duplicated
– Values can be null

Map – Basic Map Operations
SortedMap Map whose keys are sorted

HashMap Efficient general purpose Map Implementation

TreeMap Sorted Map Implemented as a self-balanced tree, Supports Comparable and Comparator Storting

Common Map Methods :

put - add key and value

putIfAbsent - Add key and value If key not contained or value null

get Return value for key, If key not found return null

getOrDefault - Return value for key, If key not found return the provided default value

values Return a Collection of the contained values

 

Map<String,String> map = new HashMap <>();

map.put("2222","ghi");
 map.put("3333","abc");
 map.put("1111","def");

String s1 = map.get("3333"); --> abc

String s2 = map.get("9999); --> null

String s3 = map.getOrDefault("9999","xyz");

It will return - "xyzzy"

map.forEach((k,v)->System.out.println(k+"|" + v));

-- Print all values .

map.replaceAll((k,v)->v.toUpperCase());

SortedMap
firstKey - Return first key

lastKey - Return last key

headMap - Return a map for all keys that are less than the specified key

tailMap - Return a map for all keys that are greater than or equal to the specified key

subMap - Return a map for all keys that are greater than or equal to the starting key and less than the ending key

SortedMap<String,String> map = new TreeMap<>();

map.put
 SortedMap<String,String> hMap = map.headMap("XXXX");

SortedMap<String,String> tMap = map.tailMap("XXXX");
Persist Java Objects
 Java has built-in ability to persist objects
 - Store from runtime into a byte stream
 - ReStore from byte stream into runtime

Most cases require very little programming
 - Leverages reflection

Serialzation

– Storing an object-graph to a byte stream
– Restoring an object-graph from a byte stream

Serialization Types :

Serializable

ObjectOuputStream

ObjectInputStream

Java Reflection:

Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.

Java Reflection is quite powerful and can be very useful. For instance, Java Reflection can be used to map properties in JSON files to getter / setter methods in Java objects, like Jackson, GSON, Boon etc. does. Or, Reflection can be used to map the column names of a JDBC ResultSet to getter / setter methods in a Java object.

http://tutorials.jenkov.com/java-reflection/index.html

Iterable

–> Collection

——-> List, Queue , Set

List

— > ArrayList
—> LinkedList
—> Vector –Stack

Queue –>PriorityQueue
— Queue
— ArrayDequeue

Set
–>HashSet
–>LinkedHashSet

SortedSet
–>>TreeSet

Methods of Collection Interface

add, addAll, remove, removeAll, retainAll ,size(),clear(),contains(),iterator(),toArray(),isEmpty(),equal(), hashcode()

Iterator

hasNext(),
next(),
remove()

Overall following classes :

ArrayList class
 LinkedList class
 List interface
 HashSet class
 LinkedHashSet class
 TreeSet class
 PriorityQueue class
 Map interface
 HashMap class
 LinkedHashMap class
 TreeMap class
 Hashtable class
 Sorting
 Comparable interface
 Comparator interface
 Properties class in Java

 

ArrayList<String> arrList = new ArrayList<String>();

list.add(“a”);
list.add(“b”);
list.add(“c”);
list.add(“d”);

Iterator itr = list.iterator();

while(itr.hasNext()){
{

System.out.println(itr.next());
}

 

Reference : https://www.javatpoint.com/difference-between-arraylist-and-linkedlist

What’s New in JDK 8

http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html

Java Platform, Standard Edition 8 is a major feature release. This document summarizes features and enhancements in Java SE 8 and in JDK 8, Oracle’s implementation of Java SE 8. Click the component name for a more detailed description of the enhancements for that component.

  • Java Programming Language
    • Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method argument, or code as data. Lambda expressions let you express instances of single-method interfaces (referred to as functional interfaces) more compactly.
    • Method references provide easy-to-read lambda expressions for methods that already have a name.
    • Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.
    • Repeating Annotations provide the ability to apply the same annotation type more than once to the same declaration or type use.
    • Type Annotations provide the ability to apply an annotation anywhere a type is used, not just on a declaration. Used with a pluggable type system, this feature enables improved type checking of your code.
    • Improved type inference.
    • Method parameter reflection.
  • Collections
    • Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into the Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations.
    • Performance Improvement for HashMaps with Key Collisions
  • Compact Profiles contain predefined subsets of the Java SE platform and enable applications that do not require the entire Platform to be deployed and run on small devices.
  • Security
    • Client-side TLS 1.2 enabled by default
    • New variant of AccessController.doPrivileged that enables code to assert a subset of its privileges, without preventing the full traversal of the stack to check for other permissions
    • Stronger algorithms for password-based encryption
    • SSL/TLS Server Name Indication (SNI) Extension support in JSSE Server
    • Support for AEAD algorithms: The SunJCE provider is enhanced to support AES/GCM/NoPadding cipher implementation as well as GCM algorithm parameters. And the SunJSSE provider is enhanced to support AEAD mode based cipher suites. See Oracle Providers Documentation, JEP 115.
    • KeyStore enhancements, including the new Domain KeyStore type java.security.DomainLoadStoreParameter, and the new command option -importpassword for the keytool utility
    • SHA-224 Message Digests
    • Enhanced Support for NSA Suite B Cryptography
    • Better Support for High Entropy Random Number Generation
    • New java.security.cert.PKIXRevocationChecker class for configuring revocation checking of X.509 certificates
    • 64-bit PKCS11 for Windows
    • New rcache Types in Kerberos 5 Replay Caching
    • Support for Kerberos 5 Protocol Transition and Constrained Delegation
    • Kerberos 5 weak encryption types disabled by default
    • Unbound SASL for the GSS-API/Kerberos 5 mechanism
    • SASL service for multiple host names
    • JNI bridge to native JGSS on Mac OS X
    • Support for stronger strength ephemeral DH keys in the SunJSSE provider
    • Support for server-side cipher suites preference customization in JSSE
  • JavaFX
    • The new Modena theme has been implemented in this release. For more information, see the blog at fxexperience.com.
    • The new SwingNode class enables developers to embed Swing content into JavaFX applications. See the SwingNode javadoc and Embedding Swing Content in JavaFX Applications.
    • The new UI Controls include the DatePicker and the TreeTableView controls.
    • The javafx.print package provides the public classes for the JavaFX Printing API. See the javadoc for more information.
    • The 3D Graphics features now include 3D shapes, camera, lights, subscene, material, picking, and antialiasing. The new Shape3D (BoxCylinderMeshView, and Spheresubclasses), SubSceneMaterialPickResultLightBase (AmbientLight and PointLight subclasses) , and SceneAntialiasing API classes have been added to the JavaFX 3D Graphics library. The Camera API class has also been updated in this release. See the corresponding class javadoc for javafx.scene.shape.Shape3Djavafx.scene.SubScenejavafx.scene.paint.Materialjavafx.scene.input.PickResultjavafx.scene.SceneAntialiasing, and the Getting Started with JavaFX 3D Graphics document.
    • The WebView class provides new features and improvements. Review Supported Features of HTML5 for more information about additional HTML5 features including Web Sockets, Web Workers, and Web Fonts.
    • Enhanced text support including bi-directional text and complex text scripts such as Thai and Hindi in controls, and multi-line, multi-style text in text nodes.
    • Support for Hi-DPI displays has been added in this release.
    • The CSS Styleable* classes became public API. See the javafx.css javadoc for more information.
    • The new ScheduledService class allows to automatically restart the service.
    • JavaFX is now available for ARM platforms. JDK for ARM includes the base, graphics and controls components of JavaFX.
  • Tools
    • The jjs command is provided to invoke the Nashorn engine.
    • The java command launches JavaFX applications.
    • The java man page has been reworked.
    • The jdeps command-line tool is provided for analyzing class files.
    • Java Management Extensions (JMX) provide remote access to diagnostic commands.
    • The jarsigner tool has an option for requesting a signed time stamp from a Time Stamping Authority (TSA).
    • Javac tool
      • The -parameters option of the javac command can be used to store formal parameter names and enable the Reflection API to retrieve formal parameter names.
      • The type rules for equality operators in the Java Language Specification (JLS) Section 15.21 are now correctly enforced by the javac command.
      • The javac tool now has support for checking the content of javadoc comments for issues that could lead to various problems, such as invalid HTML or accessibility issues, in the files that are generated when javadoc is run. The feature is enabled by the new -Xdoclint option. For more details, see the output from running “javac -X“. This feature is also available in the javadoc tool, and is enabled there by default.
      • The javac tool now provides the ability to generate native headers, as needed. This removes the need to run the javah tool as a separate step in the build pipeline. The feature is enabled in javac by using the new -h option, which is used to specify a directory in which the header files should be written. Header files will be generated for any class which has either native methods, or constant fields annotated with a new annotation of type java.lang.annotation.Native.
    • Javadoc tool
      • The javadoc tool supports the new DocTree API that enables you to traverse Javadoc comments as abstract syntax trees.
      • The javadoc tool supports the new Javadoc Access API that enables you to invoke the Javadoc tool directly from a Java application, without executing a new process. See the javadoc what’s new page for more information.
      • The javadoc tool now has support for checking the content of javadoc comments for issues that could lead to various problems, such as invalid HTML or accessibility issues, in the files that are generated when javadoc is run. The feature is enabled by default, and can also be controlled by the new -Xdoclint option. For more details, see the output from running “javadoc -X“. This feature is also available in the javac tool, although it is not enabled by default there.
  • Internationalization
    • Unicode Enhancements, including support for Unicode 6.2.0
    • Adoption of Unicode CLDR Data and the java.locale.providers System Property
    • New Calendar and Locale APIs
    • Ability to Install a Custom Resource Bundle as an Extension
  • Deployment
    • For sandbox applets and Java Web Start applications, URLPermission is now used to allow connections back to the server from which they were started. SocketPermission is no longer granted.
    • The Permissions attribute is required in the JAR file manifest of the main JAR file at all security levels.
  • Date-Time Package – a new set of packages that provide a comprehensive date-time model.
  • Scripting
    • The Rhino javascript engine has been replaced with the Nashorn Javascript Engine
  • Pack200
    • Pack200 Support for Constant Pool Entries and New Bytecodes Introduced by JSR 292
    • JDK8 support for class files changes specified by JSR-292, JSR-308 and JSR-335
  • IO and NIO
    • New SelectorProvider implementation for Solaris based on the Solaris event port mechanism. To use, run with the system property java.nio.channels.spi.Selectorset to the value sun.nio.ch.EventPortSelectorProvider.
    • Decrease in the size of the <JDK_HOME>/jre/lib/charsets.jar file
    • Performance improvement for the java.lang.String(byte[], *) constructor and the java.lang.String.getBytes() method.
  • java.lang and java.util Packages
    • Parallel Array Sorting
    • Standard Encoding and Decoding Base64
    • Unsigned Arithmetic Support
  • JDBC
    • The JDBC-ODBC Bridge has been removed.
    • JDBC 4.2 introduces new features.
  • Java DB
    • JDK 8 includes Java DB 10.10.
  • Networking
    • The class java.net.URLPermission has been added.
    • In the class java.net.HttpURLConnection, if a security manager is installed, calls that request to open a connection require permission.
  • Concurrency
    • Classes and interfaces have been added to the java.util.concurrent package.
    • Methods have been added to the java.util.concurrent.ConcurrentHashMap class to support aggregate operations based on the newly added streams facility and lambda expressions.
    • Classes have been added to the java.util.concurrent.atomic package to support scalable updatable variables.
    • Methods have been added to the java.util.concurrent.ForkJoinPool class to support a common pool.
    • The java.util.concurrent.locks.StampedLock class has been added to provide a capability-based lock with three modes for controlling read/write access.
  • Java XML – JAXP
  • HotSpot
    • Hardware intrinsics were added to use Advanced Encryption Standard (AES). The UseAESand UseAESIntrinsics flags are available to enable the hardware-based AES intrinsics for Intel hardware. The hardware must be 2010 or newer Westmere hardware. For example, to enable hardware AES, use the following flags:
      -XX:+UseAES -XX:+UseAESIntrinsics
      

      To disable hardware AES use the following flags:

      -XX:-UseAES -XX:-UseAESIntrinsics
      
    • Removal of PermGen.
    • Default Methods in the Java Programming Language are supported by the byte code instructions for method invocation.
  • Java Mission Control 5.3 Release Notes
    • JDK 8 includes Java Mission Control 5.3.

Nine New Developer Features in JDK 9

 https://www.oracle.com/corporate/features/jdk9-new-developer-features.html
Removes rt.jar and tools.jar from the Java runtime image.
Lambda Quick Start

Java 8 Optional In Depth
By Rambabu Posa | February 23, 2017 | Viewed : 119,589 times +2,847 pv/w

Java 8 has introduced a new class Optional in java.util package. It is used to represent a value is present or absent. The main advantage of this new construct is that No more too many null checks and NullPointerException. It avoids any runtime NullPointerExceptions and supports us in developing clean and neat Java APIs or Applications. Like Collections and arrays, it is also a Container to hold at most one value. Let us explore this new construct with some useful examples.

Advantages of Java 8 Optional:

Null checks are not required.
No more NullPointerException at run-time.
We can develop clean and neat APIs.
No more Boiler plate code

https://www.mkyong.com/java8/java-8-optional-in-depth/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s