Java


1)The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

2) To debug Java XML Parsers set jaxp.debug system property to 1 or true. It will print lots of messages regrading which parser classes/properties JAXP is picking up etc.

3)  JTDS/SQL Server

While querying a IMAGE type column, if you are expecting getObject() to return byte[], pass useLOBs=false in the JDBC url.

4) static import

Use it if you want unqualified access to static member of a  type without inheriting from the type.

import static  x.y.z.StaticMemeber;

....
access StaticMember  here without qualifying it
....
real life example of this is importing the Matchers in junit.

import static org.hamcrest.CoreMatchers.is;
or:
import static org.hamcrest.CoreMatchers.*;

and then you can call is()  as if it is a function defined in same class where it is being used. 



5) FTPClient

I was using FTPClient to upload files to an FTP server and it was not working, though everything looked fine. I did not notice any exceptions in the logs also. After scratching my head for some time I realized that the issue was due to destination directory permissions. After I did chmod 777 on destination directory, it all started working.

6) Jackson has annotations for both custom serialization ( @JsonSerialize)  and deserialization  ( @JsonDeserialize).

 6) Nice article on GC
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

7) In the pom file for an project, I had a dependency for servlet-api.  The existing version was 2.4 and I need to use 3.0.1

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
        </dependency>

 So I just changed the version to 3.0.1  and recompiled. The maven could not download the dependency from maven repositories. I researched around but nothing worked. Finally one of my coworker told me that artifactId was  changed and for 3.0.1  the artifactId  was "javax.servlet-api". Once I made that change everything worked fine.

8.     "Stack" class  supports stack data structure supported by Java.

Stack<Character> stack = new Stack<>();

See there is no  <Character>  in "new Stack<>".


Java 8

9. A variable is "effectively final" if it is only assigned once.
10. A lambda expression allow simple way of defining an anonymous class with one method whose type is inferred. 
11. Good link on Java 8 - https://leanpub.com/whatsnewinjava8/read

Diamond Problem
If Java allows multiple inheritance, then diamond problem will arise.   If D extends B and C which extend a common class A, then for a method in A, which is overridden in B and C,  which implementation will D inherit if it does not overwrite it. (  In Java8 this scenario may arise due to multiple inheritance via interfaces and interfaces are allowed to contain method implementations using default or static keywords. This is solved by compiler throwing exception and forcing it to be overridden in D. ) 

Functional Interface
An interface with exactly one abstract method becomes Functional Interface. Runnable is a good example of Functional Interface.  Java8 introduced a few:

Consumer<T>:  Represents an operation that takes a type and returns nothing

Function<T,R>: represents a function which take type T and returns type R
Predicate<T>: represents a predicate( boolean valued function) of a single argument
Supplier<R>: Represents a supplier of result. Takes nothing but returns a result. 

There are many variations of these basic interfaces.

Stream API

At the lowest level, all streams are driven by a spliterator.

A spliterator is the parallel analogue of an Iterator; it describes a (possibly infinite) collection of elements, with support for sequentially advancing, bulk traversal, and splitting off some portion of the input into another spliterator which can be processed in parallel. 



reduction operations: properly constructed reduce operation is inherently parallelizable, so long as function used are associative and stateless.  avoid side effect in the reduce operations. 


Streams allow lazy evaluation. All interme


Aggregate Operations 

Reduction operations/terminal operations - min, max, sum, average etc.   these typically return one value by combining contents of stream. Some reduction ops return collection too.

General Purpose Reduction operations: reduce, collect 


*reduce(identity, accumulator ) : good for operation which typically return a single value, so there is no need to keep the results from each intermediate operation.

*collect(Supplier<R> supplier,BiConsumer<R,? super T> accumulator,BiConsumer<R,R> combiner) : For scenarios similar to " if you need to return a collection where value from each iteration is added to the collection."    Collector interface also represents  all these functions together( mutable reduction operation ) . Hence collect(Collector<? super T, A , R> collector ) is also another form of collect.  

Parallelism in streams : https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html

Below are few good links which provide examples on Stream functionality:

Java8-stream-tutorial-examples
Mutable Reductions

StringBuilder  - is unsynchronized version of StringBuffer. It has exact same interface as StringBuffer, just it is not thread-safe. 


Intermediate Operations:
map(Function) - > it generates new stream by applying a function on each element and this stream can be of totally different type.

toArray(Supplier) - can be used to get elements of stream into an array.
flatMap(function which converts complex structure(a single stream element) to stream of simpler elements) - It is used to flatten a stream of complex data structure elements. For example:
List<List<String>> listOfListOfString
List<String> listOfString=listOfListOfString.stream().flatMap(Collection::stream).collect(Collectors.toList())

peek(Function) - Intermediate function to apply some operation on the elements and returns a new stream.
filter(Predicate)

Short-Circuit Operations - allows computations on infinite streams in finite time.
limit()
skip()

Terminal Operations:

Terminal operations mark the stream as consumed, so no further operation can be performed.
collect() performs mutable fold operations (repackaging elements to some data structures and applying some additional logic, concatenating them, etc.) on data elements held in the Stream instance.
orElse(null) can be used with Optional object.
orElseThrow(NoSuchElementException::new) - for throwing exception





Java 8 Date Time API  - It is JSR-310 implementation.




References:
https://howtodoinjava.com/java/multi-threading/concurrency-vs-parallelism/
https://howtodoinjava.com/java/basics/java-hashcode-equals-methods/


Comments

Popular posts from this blog

SQL

Analytics

DBeaver