Thursday, 29 December 2016

Autoboxing and Unboxing in Java


Autoboxing and Unboxing feature are added from Java 5 onwards.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
    lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
·  Passed as a parameter to a method that expects an object of the corresponding wrapper class.
·  Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i: li)
        if (i % 2 == 0)
            sum += i;
        return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
    int sum = 0;
    for (Integer i : li)
        if (i.intValue() % 2 == 0)
            sum += i.intValue();
        return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
·  Passed as a parameter to a method that expects a value of the corresponding primitive type.
·  Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don't have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

Autoboxing and Unboxing

Autoboxing and Unboxing

Autoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

- See more at: http://learnerpoint.in/2016/12/29/autoboxing-unboxing-java/#sthash.I7v19ysf.dpufAutoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:

- See more at: http://learnerpoint.in/2016/12/29/autoboxing-unboxing-java/#sthash.I7v19ysf.dpufAutoboxing and Unboxing feature are added from Java 5 onwards.

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. Converting an object of a wrapper type to its corresponding primitive data type is called unboxing
Here is the simplest example of autoboxing:
Integer i=100;
Consider the following code:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(i);
Although we add the int values as primitive types, rather than Integer objects, to lst, the code compiles. Because lst is a list of Integer objects, not a list of int values. The compiler does not generate an error because it creates an Integer object from i and adds the object to lst. Thus, the compiler converts the previous code to the following at runtime:
List<Integer> lst = new ArrayList<>();
for (int i = 1; i < 50; i += 2)
lst.add(Integer.valueOf(i));
Converting a primitive value (an int, for example) into an object of the corresponding wrapper class (Integer) is called autoboxing. The Java compiler applies autoboxing when a primitive value is:
  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.
Consider the below piece of code:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i: li)
if (i % 2 == 0)
sum += i;
return sum;
}

The remainder (%) and unary plus (+=) operators do not apply to Integer objects, and still our code compiles without any errors. The compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:
public static int sumEven(List<Integer> li) {
int sum = 0;
for (Integer i : li)
if (i.intValue() % 2 == 0)
sum += i.intValue();
return sum;
}
Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing. The Java compiler applies unboxing when an object of a wrapper class is:
  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Benefits of Autoboxing / Unboxing

  • Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don’t have to perform Explicit typecasting.
The following table lists the primitive types and their corresponding wrapper classes, which are used by the Java compiler for autoboxing and unboxing:
dddddddddddddddddd

Thursday, 22 December 2016

What is the difference between doGet () and doPost ()?

While using doGet() or doPost() prefer using doPost() because it is secured and it can send much more information to the server.

GET or doGet()

GET is the simples HTTP method, and its main purpose is to ask a server to get a resource and send it back. The resource can be an HTML page, an image , a PDF etc.

  • The request parameters are transmitted as a query string appended to the request.
  • All the parameters get appended to the URL in the address bar.
    http://example.com/search?emp_name=sanjay 
  • Allows browser bookmarks but not appropriate for transmitting private or sensitive information. 
  • In an HTML you can specify as follows:
    <form name=”myForm” method=”GET” >
  • GET was originally intended for static resource retrieval.
  • GET is not appropriate when large amounts of input data are being transferred.



POST or doPost()

HTTP Post request is more powerful and used by the browser to make complex requests on the server. For example the user fills up a form and submit the data , which is then supposed to be inserted into a database.

The request parameters are passed with the body of the request.

More secured. In HTML you can specify as follows:
<form name=” myForm” method=”POST” >
POST was intended for form submits where the state of the model and database are expected to change.

Since POST sends information through a socket back to the server and it won’t show up in the URL address bar, it can send much more information to the server.



Unlike doGet(), it is not restricted to sending only textual data. It can also send binary data such as serialized Java objects.


Thursday, 15 December 2016

Difference between PATH and CLASSPATH in Java

Here are some of the common difference between PATH vs CLASSPATH in Java :



1) PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. CLASSPATH, an environment variable is used by Java to locate and load compile Java bytecodes stored in the .class file.

2) In order to set PATH in Java, you need to include JDK_HOME/bin directory in PATH environment variable while in order to set CLASSPATH in Java you need to include all those directories where you have put either your .class file or JAR file which is required by your Java application.

3) Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands.



4) PATH environment variable is used by operating system to find any binary or command typed in the shell, this is true for both Windows and Linux environment while CLASSPATH is only used by Java ClassLoaders to load class files.


These are some notable difference between PATH vs CLASSPATH in Java.
How to set PATH and CLASSPATH in Windows and Unix
Use command prompt in Windows or shell in Linux to set PATH and CLASSPATH . Both PATH and CLASSPATH are environment variable and can be set using 
set keyword in DOS and Windows and export command in Linux

Command to set PATH in Windows

set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin

Command to set PATH in UNIX/Linux

export PATH = ${PATH}:/opt/Java/JDK1.6.18/bin

In Linux use a colon(:) as a 
separator and in Windows use semi-colon(;) as a separator.

Command to set CLASSPATH in windows

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

Command to set CLASSPATH in Unix/Linux

export CLASSPATH= ${CLASSPATH}:/opt/Java/JDK1.6.18/lib






Saturday, 10 December 2016

What Are RESTful Web Services

Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability and modifiability that enable services to work best on the Web.
In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), typically links on the Web.
It revolves around resource where every component is a resource and a resource is accessed by a common interface using HTTP standard methods. In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.
In REST architecture, a REST Server simply provides access to resources and REST client accesses and presents the resources. Each resource is identified by URIs/ global IDs. REST uses various representations to represent a resource like text, JSON and XML.  JSON is the most popular format being used in web services.
The following principles encourage RESTful applications to be simple, lightweight, and fast:
·         Resource identification through URI: A RESTful web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing space for resource and service discovery.
·         Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE. PUT creates a new resource, DELETE is used to remove a resource. GET retrieves the current state of a resource. POST is used to update an existing resource.
·         Self-descriptive messages: Resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others.

·         Stateful interactions: Every interaction with a resource is stateless. It is responsibility of the client to pass its context to server and then server can store this context to process client's further request.  Stateful interactions are based on the concept of explicit state transfer. Several techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields. State can be embedded in response messages to point to valid future states of the interaction. 

Wednesday, 7 December 2016

Dependency Injection

Dependency Injection

Dependency Injection is a software design pattern that implements inversion of control for resolving dependencies. A dependency is an object that can be used. An injection is the passing of a dependency to a dependent object that would use it. The service is made part of the client's state. Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern.
Dependency injection allows a program design to follow the dependency inversion principle. The client delegates the responsibility of providing its dependencies to external code. The client is not allowed to call the injector code. It is the injecting code that constructs the services and calls the client to inject them. This means the client code does not need to know about the injecting code. The client does not need to know how to construct the services. The client does not need to know which actual services it is using. The client only needs to know about the intrinsic interfaces of the services because these define how the client may use the services. This separates the responsibilities of use and construction.
There are three common means for a client to accept a dependency injection: setter-, interface- and constructor-based injection.
·         Constructor Injection: the dependencies are provided through a class constructor.
·         Setter Injection: the client exposes a setter method that the injector uses to inject the dependency.
·      Interface Injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.

 


Advantages

·         Dependency injection allows a client the flexibility of being configurable.
·         Dependency injection can be used to externalize a system's configuration details into configuration files allowing the system to be reconfigured without recompilation. Separate configurations can be written for different situations that require different implementations of components. This includes, but is not limited to, testing.
·         Because dependency injection doesn't require any change in code behavior it can be applied to legacy code as a refactoring.
·         This ease of testing is often the first benefit noticed when using dependency injection.
·         Dependency injection allows a client to remove all knowledge of a concrete implementation that it needs to use. This helps isolate the client from the impact of design changes and defects. It promotes reusability, testability and maintainability.
·         Reduction of boilerplate code in the application objects, since all work to initialize or set up dependencies is handled by a provider component.
·         Dependency injection allows concurrent or independent development. Two developers can independently develop classes that use each other, while only needing to know the interface the classes will communicate through. 
·         Dependency Injection decreases coupling between a class and its dependency.

 

Disadvantages

·         Dependency injection can make code difficult to trace (read) because it separates behavior from construction. This means developers must refer to more files to follow how a system performs.
·         Dependency injection forces complexity to move out of classes and into the linkages between classes which might not always be desirable or easily managed.
·         Ironically, dependency injection can encourage dependence on a dependency injection framework.



Related Posts Plugin for WordPress, Blogger...