Showing posts with label Java 8. Show all posts
Showing posts with label Java 8. Show all posts

Sunday, 21 February 2016

JDBC/ODBC alternative in Java 8


java8.jpg

The JDBC/ODBC driver is the type 1 driver it is also known as JDBC-ODBC bridge. It is ODBC driver to connect the database that convert the JDBC calls to ODBC function calls. Type 1 driver is fully platform dependent and use the native libraries of operating system. The ODBC driver must be installed in operating system. The Sun Microsystem provide JDBC-ODBC driver name as sun.jdbc.odbc.JdbcOdbcDriver but Oracle's JDBC-ODBC Bridge was removed in Java 8 for following reasons:-
Disadvantages
  1. Performance issue the calls have to go through the JDBC bridge to the ODBC driver so it is slower than other types of drivers.
  2. The ODBC driver must be installed on the client machine.

Also visit Findnerd to read and post tech blogs and can also Ask tech Query over Findnerd.

Monday, 18 May 2015

Lambda expressions - Java 8

Lambdas?

A lambda is nothing but a anonymous function which accepts zero or more arguments and produces

output. Lambda is basically composed with three components- argument section, an arrow and a

body.

    (argument ...) -> { body } // a valid pseudo code

Example 1 :

     x -> System.out.println(x);

Example 2 :

    (x,y) -> return x + y;

Example 3 :

    (int x, int y) -> { int z = x + y;
    return z;
    }

Let me explain the examples.
First example, shows that brackets are optional if there is only a single argument.
In second example, Type of the arguments are specified. Compiler checks the type if not

declared, it enable runtime typecasting for primitives. Developers can ignore the curly

brackets around a single line body.
Third example shows the argument types and the body of the function.

Lambda expressions can be used to define a inline implementation of a functional interface

i.e. an interface with a single method only. For example - Java developer are well familiar

with the example below -

    new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("Hello from thread");
        }
    }).start();

But it could be done more easily with the Lambda expressions as following -

    new Thread(
        () -> System.out.println("Hello from thread")
    ).start();

In the above example, we instantiate the Runable and define the body of run method.

It is very useful in inner class declaration. For example :

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("The button was clicked using old fashion code!");
        }
    });

It can be done as below.

    button.addActionListener( (e) -> {
            System.out.println("The button was clicked. From lambda expressions !");
    });

Here, we don't need to create a inner class to handle the button event.

Lambda expressions are very useful while working with Collections. For example -

    List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    list.forEach(n -> System.out.println(n));

@FunctionalInterface

Similar to the marker interface, there is a concept of functional interface. The interface

with only a single methods are known as functional interface. @FunctionalInterface is a new

interface added in Java 8 to indicate that we can simply use it in our API and take advantage

of Lambda expressions. For example -

    @FunctionalInterface
    public interface MyFunctionalInterface {
        public void doSomeWork();
    }
    public class MyFunctionalInterfaceTest {
        public static void execute(MyFunctionalInterface worker) {
            worker.doSomeWork();
        }
        public static void main(String [] args) {
            execute( () -> System.out.println("Worker invoked using Lambda expression") );
        }
    }

Have fun with Java 8 and Lambda expressions.

Happy coding.

You can see such more blogs at http://findnerd.com/NerdDigest/

Tuesday, 12 May 2015

Functional Interface - Java 8

The interface with only a single methods is known as functional interface. If an interface

marked as @FunctionalInterface it can only contains one abstract method. If we try to declare

more than one abstract method inside FunctionalInterface compiler will produce the error.

For example -

    @FunctionalInterface
    public interface MyDemoFunctionalInterface {
     public abstract void display();
    }

The above example is correct and will compile successfully, but the below example cannot be

compiled and will throw the error:

    @FunctionalInterface
    public interface MyDemoFunctionalInterface
    {
      public abstract void display();
      public abstract void show();
    }

Hope this will help you :)

You can also get such more articles http://findnerd.com/NerdDigest

Thursday, 7 May 2015

Java 8- Method and Constructor References

Method Referencing

Method reference is feature related to lambda expressions. Method references are more readable

form of Lambda expressions for already written methods. “::” operator is used to define method

reference. It provides a way to a method without executing it. In order to that a method

reference requires a target type context that consists of a compatible functional interface.

Method reference when evaluated creates an instance of the functional interface. The 2 types

of method references are:-

a). Static Method References.

    Syntax:-  ClassName :: methodName

Ex:-

    public interface Parent
    {
            String process(String input);
    }
    public class MethodReference
    {
            public static String toUpperStatic(String input)
            {
                    return input.toUpperCase();
             }
          public static void main(String... args)
            {
                 Parent parent = Parent::toUpperStatic;
         System.out.println("Static reference = "+parent .process("static"));
                 }
    }

b). Instance Method References Of Objects.

    Syntax:-   objRef :: methodName

Ex:-

    public interface Parent
        {
                String process(String input);
        }
        public class MethodReference
        {
                public String toUpperInstance(String input)
                {
                        return input.toUpperCase();
               }
              public static void main(String... args)
                {
                    MethodReference methodReference = new MethodReference();
                         Parent parent  = methodReference::toUpperInstance;
                 System.out.println("Instance reference  "+parent .process("instance"));
                     }
        }

Constructor Referencing

you can create references to the constructors. In Constructor references the method name is

"new" and the receiver is always the name of the class that is defining the constructor. You

can assign a constructor reference to any functional interface which has a method compatible

with the constructor.

    Syntax:-    ClassName :: new

Ex:-

    package com.evon;
        @FunctionalInterface
        public interface MyString
        {
                String strFunc(char[] chArray);
        }
        public class StringCreator
        {
              public static void main(String[] args)
            {
                        MyString mystr = String::new;
                        char[] charArray =

{'e','v','o','n','t','e','c','h','n','o','l','o','g','y'};
                        System.out.println(mystr.strFunc(charArray));
            }
        }

In above example we have assigned a String constructor reference to the functional interface

which we have created just now.

Hope this will help you :)

you can find such more blogs on http://findnerd.com/NerdDigest