Search This Blog

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

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.