heartin's blog

Resource Oriented Architecture

REST follows resource oriented architecture as opposed to the popular service oriented architecture followed by SOAP web services.

Resource-Oriented Architecture has four important concepts:

  • Resources,

  • their names (URIs),

  • their representations and

  • the links between them.


A Quick Introduction to Java 8

Java is one of the most popular programming languages. However, recently many other programming languages have come and some of them even running on top of the Java Virtual Machine (JVM). Many of these languages support newer programming practices like functional programming and comes with a bunch of out of the box functionalities that abstract away most of the low level details from the programmer.


Collection Framework Related Improvements in Java 8

Java 8 has has made some improvements in collections framework such as a new forEach loop, better type inference etc. We will list out those features here.


The forEach method

The default forEach method of the Iterable interface performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. 

Signature of the methiod is: void forEach(Consumer<? super T> action)

Accessing Effectively Final Variables From Anonymous Inner Classes in Java 8

Before Java 8, you could only use final variables from a surrounding class in an anonymous inner class. In Java 8, within anonymous class (or lambda expressions) you can use effectively final variables: not declared as final but only assigned once. 


Important points about accessing variables from within an anonymous inner class (or lambda expression)

Summary of Different Syntaxes to Write Lambda Expressions in Java 8

Java 8 allows you to write lambda expressions in few varying syntaxes. Let us quickly see those here.


With no arguments

Runnable r = () -> System.out.println(“H”);


Only one argument: with or without parenthesis

ActionListener al1 = (event) -> System.out.println(“Button clicked”);

ActionListener al2 = event -> System.out.println(“Button clicked”);


Java 8 Lambda Expression Basic Syntax and Examples

Lambda expressions implement an interface with only one single abstract methodl. Such interfaces are called as functional interfaces. Since the interface has only one single method (function), passing across that interface implementation gives the impression of passing across that function. Already existing interfaces in Java such as Runnable, Comparator, ActionListener are already functional interfaces, as they have only one abstract method. We will see how we can use lambda expressions with those existing interfaces.


Default and Static methods in Java 8 Interfaces

Before Java 8, interfaces could only have abstract methods and constants. Java 8 allows you to have default as well as static methods inside an interface. This was mainly done for interface unlocking: Now you can add methods to an interface without needing the implementing classes to change.


Default methods

You can add an instance method to an interface through the keyword default.

They are also public similar to the abstract methods. 

Default methods are inherited by subclasses and can be overriden.

Method References and Constructor References in Java 8

Method references or constructor references can be used to refer to an existing method or constructor by name. Classes containing these methods can be regular classes without the need to implement or extend anything. However, the target type needs to be a functional interface, as the signature of the method is infered from the functional interface's abstract method.

General Purpose Functional Interfaces in Java 8

Java 8 introduces few general purpose functional interfaces based on the input and output to the single abstract method.


Most common ones are:

  1. Predicate<T> whose method

    1. takes in an argument of type T and

    2. rerturn a boolean.

  2. Consumer<T> whose method

    1. takes in an argument of type T and

    2. rerturn nothing (void).

  3. Function<T,R> whose method

Introdution to Functional Programming and Lambda Expressions

In functional programming, we think in terms of functions. While Object Oriented programming abstract over data, functional programming abstract over behavior. Instead of data, stateless functions are be passed across the code. 

Lambda expressions are Java's way of doing functional programming. Lambda expressions in computer science are anonymous functions: a function (or a subroutine) defined, and possibly called, without being bound to an identifier.


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!