Pages

Friday, September 23, 2016

Primitive and Abstract Data Types in Java

Primitive data types are the most basic data types available within the Java language. There are 8 types of primitive data types : boolean, byte, char, short, int, long. float and double.

These primitive types have predefined operations in Java type system and no new operations can be defined on them.

Details of primitive data types are as follows:


CategoryTypesSize (bits)Minimum ValueMaximum ValuePrecisionExample
Integerbyte8-128127From +127 to -128byte b = 65;
char160216-1All Unicode characterschar c = 'A';
char c = 65;
short16-215215-1From +32,767 to -32,768short s = 65;
int32-231231-1From +2,147,483,647 to -2,147,483,648int i = 65;
long64-263263-1From +9,223,372,036,854,775,807 to -9,223,372,036,854,775,808long l = 65L;
Floating-pointfloat322-149(2-2-23)·2127From 3.402,823,5 E+38 to 1.4 E-45float f = 65f;
double642-1074(2-2-52)·21023From 1.797,693,134,862,315,7 E+308 to 4.9 E-324double d = 65.55;
Otherboolean1----false, trueboolean b = true;
void----------

< and > can be used to compare primitive data types. Comparison using these operators cannot be done for abstract data type like string , list, map.

Abstract data types:

The other type of data type is abstract data type. They are called abstract because their implementation is hidden. The combination of data along with its method is called abstract data type.
In general, there are many possible operations that could be defined for each ADT; however, they often fall into these categories:
  1. initialize
  2. add data
  3. access data
  4. remove data
Examples are: List, Stack, Queue, Set, Map, etc.

Monday, September 12, 2016

Object Creation in Javascript

In Javascript, we don't have classes. Instead we use functions. There are two ways to create custom objects.

1. Constructor function
2. Literal Notation

1. Creating an object using constructor function;
        In this method, we first create a function and then create an object using that function.

function Employee(firstName, lastName){
      this.firstName = firstName;
      this.lastName = lastName;

      this.getFullName = function(){
            return this.firstName + " " + this.lastName;
      }
}

var employee = new Employee("Hello","World");

Here, employee is the object and Employee is the constructor function.


2. Literal Notation:

 var employee = {
        firstName : "Hello",
        lastName : "World",
        getFullName : function () {
                      return this.firstName + " " + this.lastName;
        }
}

        document.write("FirstName = " + employee.firstName + "[br/]");
        document.write("LastName = " + employee.lastName + "[br/]");
        document.write("FullName = " + employee.getFullName() ;

In this method, we already have the object. Here, employee is the object name. So, we can access the properties using that object name. Objects created using this method are singletons i.e. any changes to one instance affects the entire script.

Friday, September 9, 2016

Remove duplicate values from array using Javascript

Filter method in JavaScript can be used to easily remove duplicate values from an array in JavaScript.

Syntax:
array1.filter(callbackfn[, thisArg])

Filter method takes two parameters described as below:
Parameter
Definition
array1
Required. An array object.
callbackfn
Required. A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
thisArg
Optional. An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

Return Value

A new array that contains all the values for which the callback function returns true. If the callback function returns false for all elements of array1, the length of the new array is 0.
This property of filter method can be used to remove duplicates from an array.
Code:
var myArray = ["a","b","c","a"];
var newArray = myArray.filter(function(vale,index,array){
  return array.indexOf(value)==index;
});
alert(newArray);

indexOf(value) returns the first index of value. If it is not equal to the current value, filter returns false, so the value is not included in newArray. Thus, newArray will have unique values only.