Monday 24 May 2021

Lambda Expression in Java 8

Lambda Expression













Lambda calculus was the big change in Mathematical world which came in 1930s.
Because of this programmers also started using it.

LISP is the 1st lang where lambda expressions came first.
Apart form LISP, C, CPP, PYTHON, SCALA, RUBBY, Objective C

Advantages

  1. To enable functional programming in java
  2. To write more readable, maintainable and concise code
  3. To use APIs very easily and effectively.
  4. To enable parallel processing
Lambda Expressionlambda expressions are a means to create anonymous classes of functional interfaces easily
     1. No Name
     2. No Return Type
     3. No Access Modifier

NOTE : Only use in case of functional interface.


Functional Interface : An interface having only one abstract method is known as Functional Interface.
Example : Runnable , Callable etc.

There are some rules to write lambda expression from which two main important rules are
1. If body is having only one statement then { } braces are optional
2. If passing parameter as an argument then argument type is optional

Example.

public void display(String str){
    System.out.println("Hello "+str);
}
Using lambda
(str)->System.out.println("Hello "+str);
or
str->System.out.println("Hello "+str);
int getValue(){
  return 5;
}
Using lambda
() -> 5
// concatenating two strings, it has 2 string parameters and they are concatenated in lambda body
(String s1, String s2) -> s1+s2;


Below are some example codes using lambda

1. Calculator program
























2. Threading



Scenarios

  1. (int x, int y) -> x+y; or (x, y) -> x + y; which one of these is a valid lambda expression?

    Both of them are valid lambda expressions if used in a correct context.


  2. (int x, y) -> x + y; is this a valid lambda expression?

    You can't have lambda expression where type for only one of the parameter is explicitly declared so this lambda expression is invalid.

Read More »

Monday 17 May 2021

10.0 Collection Framework in Java

Collection Framework

Why there is need of collection framework ?

 Limitation of Array : 

1. Fixed in Size - If we give less size then we can't expand it at run time, If we give more space then unused space will be of no use.

2. They are Homogeneous in type, we can only store similar type of data into an Array.

3. There is underlying data structure behind this as a result of there is no additional supporting methods are available.

Collections are grow able in nature.

Note : Arrays are highly recommended  because of its high performance but memory wise collections are good.


What is Collection ?

Group of individual object as a  single entity.

Java provides several classes and interfaces to achieve this also know as Collection Framework.


There are 9 key Interface




Read More »

Sunday 21 February 2021

1.4 Java Web Services


 

JAX-RS Annotation

@Path

The @Path annotation is used to specify the URI through which a resource and an API can be accessed. Resource in this case is the REST Web service itself. @Path("/").


@Path("/message")

public class MessageReceiveService {


@Path("/birthday")

    public Response printBdayMessage(){


@GET - If service method is returning data based on client request.

@POST - If you want to store the data

@PUT -If you want to modify the data

@DELETE - If you want to delete 


@QueryParam

public String hello(@QueryParam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

http://wwww.localhost:8080/appname/skeleton/helloService/hello?name1=bikesh&age1=32


@PathParam

 @Path("/helloService")

 public class HelloService{

@Path("sayHello/{name1}/{age}")

public String sayHello(@Pathparam("name1") String name, @QueryParam("age1") int age){

return "Hello ..."+name+" age="+age;

}

 }

 http://localhost:8080/webAppname/rest/helloService/sayHello/venkat/32


@MatrixParam

 It is similar to QueryParam but in URL variables will be seperated by semicoloun.

 

@FormParam

    @POST

    @Path("/add")

    public Response addUser(

        @FormParam("name") String name,

        @FormParam("age") int age) {


        return Response.status(200)

            .entity("addUser is called, name : " + name + ", age : " + age)

            .build();


    }

@Consumes annotation specifies the list of media types consumed by a particular API or class

Examples:

@Consumes("application/json")

@Consumes("application/xml")


@Produces annotation specifies the list of the media types produced by a particular API or class

Examples:

@Produces("application/json")

@Produces("application/xml")

Read More »

Wednesday 17 February 2021

1.3 Web Services - JAX-WS


 

JAX-WS - It is a specification (API - contain set of interfaces)

It is having some Annotations

NOTE : Recommended approach is create interface and then implementation service class

In interfaces and implementation all three annotations are mandatory

1. @WebService

    This is mandatory annotation in I+C

    Example








Three optional properties are there

serviceName - default value will be - ServiceClassName (It should be unique)

targetNamespace - default value will be - package name in reverse order (It should be unique)

endpointInterface - default value will be - Fully qualified name of interface

2. @WebMethod (Optional)

If you want to give different name for method in wsdl then you can use it otherwise original method name will appear in wsdl file.

3. @WebParam (Optional)

If you want to give different nave for variables in wsdl then you can use it otherwise original variable name will appear in wsdl file.




 CLIENT APPLICATION FOR APACHE CFX



Read More »

Tuesday 16 February 2021

1.2 Web Services - JAX RPC API - AXIS1 Implementation


NOTE : It doesn't support collection type methods
 

STEPS TO CREATE WEB SERVICE IN ECLIPSE





STEPS TO CREATE CLIENT



Read More »

Monday 15 February 2021

1.1 WEB SEVICES - SUMMARY


 

What is Web Services - Sharing information between two application on internet is know as web services.

There are other options to share information

1. Socket Programming (Both App should be in java)

2. RMI - Remote Method Invocation (Both App should be in java)

3. EJB - Session beans and entity beans (Both App should be in java)

4. RPC - Remote Procedure Call (Both App should be in CPP)

5. CORBA - Common Object Request Broker Architecture (Multi language app supported)

6. DCOM  - Distributed communication  (Both App should be in .NET)


NOTE : CORBA is the 1st Specification which supported different language app com. support.

But Every implementation provider given their own specification because of which in some cases they fail to share information.


In technical terms Web Services are nothing but specification(Set of interfaces) i.e set of rules.

WSDL - Web Service Definition language

It contains 

1. Service Name

2. Operations

3. Parameters

4. Data Type

5. End point URL

NOTE : If you want to make your web service public you can register it to UDDI

UDDI stands for Universal Description, Discovery, and Integration.


WEB SERVICE ARCHITECTURE


1. WSDL generation tool will generate WSDL document from java service class

2. WSDL registration to UDDI registry to make it available publicly

3. WSDL document received by client

4. Stub class generation tool will create stub classes in respective programming language

5. Client will use stub classes to pass the parameters into request

6. XML request will form using stub classes

7. HTTP protocol will take SOAP request to service API

8. Soap request will be read by skeleton classes 

9. Service operation will get perform 

10. Soap response will sent back to client.

11. Stub classes will read the response from soap response

12. Reading soap response

13. response read

14. response will be given to client


synchronous response returns to the client in the same HTTP connection as the request. With asynchronous responses, a client can send multiple requests and receive the responses in subsequent connections.
















Read More »