Search This Blog

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

Friday 20 January 2017

What's New in Java 8

Java Platform, Standard Edition 8 is a major feature release. Java 8 contains a large number of new features. Although all are important, the three most important one are:
  • Lambda expressions
  • The stream API in java.util.stream
  • Default interface methods

Lambda Expressions
The most important new Java 8 feature is the lambda expression.
Lambda expressions are very important as they add functional programming features to Java. Lambda expressions can simplify and reduce the amount of source code needed to create certain constructs, such as some types of anonymous classes. This is particularly helpful when implementing a number of commonly used event handlers.
To support lambda expressions Java has been expanded by the inclusion of a new operator (the –>) and a new syntax element.


To understand the benefits that lambda expressions bring, consider the following ActionEvent handler, which uses the traditional, anonymous class, approach:

myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
myLabel.setText("Button pressed.");
}
});

With JDK 8, this event handler can be written using a lambda expression

myButton.addActionListener(
(ae) -> myLabel.setText("Button pressed.")
);


As we observe code is shorter, that is more direct and to the point.



The Stream API
Another important feature of Java 8 is the new stream API, which is packaged in java.util.stream.
A stream represents a sequence of data. The key aspect of the stream API is its ability to perform pipeline operations that search, filter, map, or otherwise manipulate data.

Let’s assume we have a list that stores employee names, department, e-mail and phone numbers. Using the stream API, we can efficiently pipeline the operations that search for entries that match some criterion, for example department name, sort the matching items, and then extract only the e-mail addresses In many cases, such actions can be performed in parallel, thus providing a high level of efficiency. The stream API provides a powerful means of handling data in an efficient, yet easy to use way.

Default Methods
With the release of Java 8, it is now possible for an interface method to define a default implementation. This new capability is called the default method.

Default method enables us a means by which interfaces could be expanded without breaking pre-existing code.
In simple terms default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface.


When a non-abstract class implements an interface, it must implement all methods defined by that interface. If a new method is to an existing interface, then the addition of that method would break pre-existing code, because no implementation would be found for that new method in pre-existing classes. The default method solves this problem by supplying an implementation that will be used if no other implementation is explicitly provided. Thus, the addition of a default method will not cause pre-existing code to break. This enables interfaces to be gracefully evolved over time without negative consequences.


Friday 6 January 2017

Differences between Java EE and Java SE

Java technology is both a programming language and a platform. The Java programming language is a high-level object-oriented language that has a particular syntax and style. A Java platform is an environment in which Java programming language applications run.

The Java Programming Language Platforms
There are four platforms of the Java programming language:
·         Java Platform, Standard Edition (Java SE)
·         Java Platform, Enterprise Edition (Java EE)
·         Java Platform, Micro Edition (Java ME)
·         JavaFX

All Java platforms consist of a Java Virtual Machine (JVM) and an application programming interface (API). The Java Virtual Machine is a program, for a particular platform, that is used to run Java applications. An API is like a library that is used to create other software components or applications. Each Java platform provides a virtual machine and an API, and this allows applications written for that platform to run on any compatible system with all the advantages of the Java programming language.

Java SE
Java SE's API provides the core functionality of the Java programming language. Java SE defines the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.
In addition to the core API, the Java SE platform consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits commonly used in Java technology applications.

Java EE
The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.

Java ME
The Java ME platform provides an API and a small-footprint virtual machine for running Java programming language applications on small devices, like mobile phones. The API is a subset of the Java SE API, along with special class libraries useful for small device application development. Java ME applications are often clients of Java EE platform services.

JavaFX
JavaFX is a platform for creating rich internet applications using a lightweight user-interface API. JavaFX applications use hardware-accelerated graphics and media engines to take advantage of higher-performance clients and a modern look-and-feel as well as high-level APIs for connecting to networked data sources. JavaFX applications may be clients of Java EE platform services.

Java Programming Language Platforms
Java Programming Language Platforms

Wednesday 4 January 2017

Variable argument or varargs in Java

Variable argument or varargs in Java 

Variable argument or varargs in Java allows you to write more flexible methods which can accept as many argument as you need. variable arguments or varargs were added in Java 1.5 Variable arguments is a  useful for a developer. Some time we have a scenario that one method can take variable number of argument  and now with varargs from language makes it much easier.

Variable arguments before Java 1.5 
Prior to Java 1.5 if a Java programmer needs to send multiple arguments to a method, they mainly have two choices to :

1. Either overload the method.
2. Take an array or Collection and pass the no of argument wrapped in array or Collection like List, Set or Map.

The real challenge for a developer comes when the developer is not aware about the number of arguments to handle and how many overloaded methods will be created. This will result in long and clumsy code, which again can be error prone if the developer has missed including the method with specific number of argument in the code.
This is where in varargs comes into picture.

varargs or variable arguments makes it possible for the developer  to call one method with variable number of argument; means define only one method and call that method with zero or more argument.



Syntax:
             type … variable Name.

Ellipses stands for variable argument java treats variable argument as an array of same data type. 3 dots is used to denote variable argument in a method and if there are more than one parameter, varargs arguments must be last, as better listed below

Some points which should be taken care when use varargs:
  1.       Ellipse can be used once in method parameter list.
  2.       Ellipse with type must be used in parameter list at the end of the method



Example to illustrate Varargs
public class Example01 {
      public static int add(int ...var){
            int sum=0;
            for(int i:var)
                  sum+=i;
     
      return sum;
}
      public static void main(String[] args) {
            System.out.println("Sum of 1 and 2 is = " +add(1,2));
            System.out.println("Sum of 1, 2 and 3 is = " + add(1,2,3));
            System.out.println("Sum of 1, 2, 3 and 4 is = "+add(1,2,3,4));
      }
}
Output of the Code

Variable argument or varargs in Java
Variable argument or varargs in Java




Monday 2 January 2017

Understanding Object and Class

The below post explains Object and Class
What is an Object?

An object represents an entity, either physical, conceptual or software.
Ø  Physical – Car or a Person
Ø  Conceptual – Chemical Process
Ø  Software – Bill Number 101, Purchase Order SO01

 An object is an entity with a well-defined boundary and identity that encapsulates its state & behaviour:
Ø  Identity - a name
Ø  State - determined by the values of its attributes
Ø  Behaviour - determined by how the object acts or reacts to requests (messages) from other objects

Object Has State

 The state of an object is one of the possible conditions in which an object may exist
 The state of an object normally changes over time
 E.g.  Rahul is an object of class Employee. The Rahul object has state:
-        Name= Rahul
-        Employee Id=123
-        Hire date=02/02/2013

Object has Behaviour

 Behaviour determines how an object acts and reacts to requests from other objects.

 The visible behaviour of an object is modelled by the set of messages it can respond to (operations the object can perform)
 Employee Rahul’s Behaviour:
-        Submit TimeSheet()
-        Complete TaskAssigned()

 The behaviour may change the state of an object.

Object has Identity

 Each object has a unique identity, even if the state is identical to that of another object.
 E.g. Employee Rahul is from XX City. Even if there is another employee with the same name – Rahul working in same city or another city they both are distinct objects

What is a Class?

 A class is a description of a set of objects that share the same attributes, operations, relationships and semantics.
Ø  An object is an instance of class

 A class is an abstraction in that it
Ø  Emphasizes relevant characteristics
Ø  Suppresses other characteristics


Relationship between Classes & Objects

Ø  A class is an abstract definition of an object.
Ø  It defines the structure & behaviour of each object in the class
Ø  It serves as a template / blue print for creating objects

Attributes of a Class

Ø  An attribute is a named property of a class that describes a range of values that instances of the property may hold
Ø  A class may have any number of attributes or no attributes at all.
Ø  An attribute has a type, which tells us what kind of attribute it is.
Ø  Typically attributes are integer, boolean, varchar etc. These are called primitive types.
Ø  Primitive types can be specific for a certain programming language.

Operations of a Class

Ø  An operation is the implementation of a service that can be requested from any object of the class to affect behaviour
Ø  A class may have any number of operations or none at all
Ø  The operations in a class describe what class can do
Ø  The operation is described with a return-type, name, zero and more parameters. This is known as signature of an operation
Often, but not always, invoking an operation on an object changes the object’s data or state 

Original Post At : http://learnerpoint.in/2017/01/02/understanding-object-and-class/

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