Sunday, August 3, 2014

JAVA Array: A Closure Look

Without any doubt, array is one of the most used data structure in all programming language, including Java. Pick up any programming language be it functional, object oriented, imperative or even scripting language like Python, Bash and Perl, you will always find array. That's why it's important for any programmer to have good understanding of  arry data structure. Array is used to store elements in contiguous memory location and many C, C++ programmer can take advantage of pointer to work with array. In Java, there is no pointers and arrays are also little bit different. They are object, they have length field which denotes how many elements array can store. Arrays are created in special memory area called heap memory in JVM, which is also created when you start the JVM. What remains same is that you can access the array element in constant time using their index, this works almost similarly in both C, C++ and Java, they start with 0 and ends at length -1, but Java array has an extra caveat that array index access are subject to bound check in Java. In C, it's possible for a program to access an invalid index, mostly index higher than size of array. In Java such attempts will result in ArrayIndexOutOfBoundsException, this is done to protect  external memory  access from JVM due to malicious programs.
10 Points about Array in Java
In order to learn and remember some important details about array data structure in Java, I am sharing these points. These are the things which I believe every Java developer should know about array. You may know how to iterate through array using enhanced for loop or how to sort array using Arrays.sort() method, but if you don't know fundamentals, it's very unlikely you would be able write clean and robust code. Even if you know everything about array in Java, this list may help you to revise some useful details.

1. First and foremost, array is object in Java. They are not primitive like int, short, or long, but they are also not full featured object with lot of methods, but because they are object, they implicitly extend Object and that's why you can call any method of java.lang.Object using array reference e.g. toString().

2. Another most important thing to know about array in Java is that once created you can not change size of array. Curious developers may ask that then how do we have dynamic collection like ArrayList in Java, which can resize itself when it get full? Well it's not the resize you think, where you can simply increase size of array to accommodate additional elements. In order to increase size, you have to create a new array and copy contents from old array to new array. Though there are fast methods exists to copy elements from one array to another, it still an expensive operation and can slow down performance of your Java application. That's why initializing array or collection with proper size is still one of the best practices to follow.

3. Third thing to know about array is it's length property, which tells you the size of array or how many elements it can hold. It's often a cause of confusion as well because String has a similar length() method, yes that's a method and array length is property, so no more parenthesis. One more thing which increases this confusion is size() method of ArrayList, which also returns how many elements ArrayList can hold. Here is a sample code snippet to find out length of array in Java.

 int[] arrayOfInts = new int[] { 101, 102, 103, 104, 105 };
System.out.println("length of arrayOfInts is : " + arrayOfInts.length);  // print 5

You can use length of array while looping though array in Java to avoid accessing invalid indexes, as shown in next example.

4. Array index starts from zero, so index of first element is 0 and index of last element is length -1. This property is used to iterate over all elements in a for loop.

 String[] cities = new String[]{"London", "Paris", "NewYork", "HongKong", "Tokyo"};
     
       for(int i=0;  i<cities.length;  i++){
           String city = cities[i];
           System.out.println("Current city is @ " + city);
       }

Output :
Current city is @ London
Current city is @ Paris
Current city is @ NewYork
Current city is @ HongKong
Current city is @ Tokyo

You can see that we are starting loop from 0 (first element) and ending it less than length e.g. length -1 (last element index). If you try to access array[length], you will get ArrayIndexOutOfBoundsException because last index is length-1.

5. As I said before that arrays are treated as objects by the Java Virtual Machine.  The type of an array is "[elementtype", where elementtype is is type of the elements.  For example, a (1-dimensional) array of integers has type "[I", similarly one dimensional short array has type "[S", and one dimensional float array has type "[F". For two dimensional arrays , you get two "[[" e.g. two dimensional int array has type "[[I". You can check this by yourself, when you print array in Java. It prints its element type and hashcode as shown below.

public class PrintArrayTypes{

    public static void main(String args[]) {

        // type of one dimensional array in Java
        int[] arrayOfInts = new int[] { 101, 102, 103, 104, 105 };
        System.out.println(arrayOfInts);

        short[] arrayOfShorts = new short[] { 20, 30, 40, 50, 60 };
        System.out.println(arrayOfShorts);

        float[] arrayOfFloats = new float[] { 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
        System.out.println(arrayOfFloats);

        // type of two dimensional array in Java
        int[][] arrayOfArrayOfInts = { { 1, 2, 3 }, { 10, 20, 30 },
                { 100, 200, 300 } };
        System.out.println(arrayOfArrayOfInts);

        double[][] arrayOfArrayOfDoubles = { { 2.0, 3.0 }, { 4.0, 5.0 } };
        System.out.println(arrayOfArrayOfDoubles);

    }

}

Output
[I@1693b52b
[S@3b5b25a1
[F@5d038b78
[[I@7b9a29
[[D@32c5f9fe


6. If you have been following my last example, then you will notice that printing array using toString() will not result in anything useful except that element-type. Ideally, we would like to see elements of array in the order they exists. Can we override toString() method of array class, no that's not an option, but don't worry we have got a utility class java.util.Arrays which contains several methods to help with different types of arrays. We can use toString() and deepToString() method of Arrays class to print elements of array for one and multi-dimensional array in Java, as shown here.

7. Similar to toString(), equals() method of array is also no use. In most cases we would like to compare elements of array and their order to another array and their elements, but equals() method of array doesn't do that, instead it does reference comparison and returns true only if both variables are pointing to same array object, as shown in below example. But don't worry Arrays class got equals() and deepEquals() method to compare elements of one dimensional and multidimensional arrays in Java. You can infer same understanding by following below example :

public class ArrayEquality{

    public static void main(String args[]) {

       String[] cities = new String[]{"London", "Paris", "NewYork", "HongKong", "Tokyo"};
       String[] metros = new String[]{"London", "Paris", "NewYork", "HongKong", "Tokyo"};
       String[] capitals = cities;
       
       // comparing array using == operator
       System.out.println("cities == metros : " + (cities == metros));
       System.out.println("cities == capitals : " + (cities == capitals));
       
       
       // comparing array using equals() method
       System.out.println("cities.equals(metros) : " + cities.equals(metros));
       System.out.println("cities.equals(capitals) : " + cities.equals(capitals));
     

       // comparing array using Arrays.equals() method
       System.out.println("Arrays.equals(cities, metros) : " + Arrays.equals(cities, metros));
       System.out.println("Arrays.equals(cities, capitals) : " + Arrays.equals(cities, capitals));
       
    }

}

Output :
cities == metros : false
cities == capitals : true
cities.equals(metros) : false
cities.equals(capitals) : true
Arrays.equals(cities, metros) : true
Arrays.equals(cities, capitals) : true

You can see that first statement is false even though elements and their orders are same, because "==" operator only returns true if both variable is pointing to same array, which is the case in second equality check. Similarly equals() method also mimic the behaviour of == operator because array doesn't override Object's equals() method whose default behaviour is to decide equality based upon same reference. Arrays.equals() is the right method to check if two arrays are equal in Java or  not. You should always use that for this purpose. 


8. For checking equality of two multi-dimensional array Java programmer should always use deepEquals() method, because even Arrays.equals() method doesn't perform deep comparison. It only does shallow equality check. Here is an example to check equality of multi-dimensional array in Java :
public class MultiDimensionalArray{

    public static void main(String args[]) {

       int[][] a1 = { {2,4}, {4,6}, {8,10} };
       int[][] a2 = { {12,14}, {14,16}, {18,20} };
       int[][] a3 = { {2,4}, {4, 6}, {8,10} };
       
       // checking if two multi-dimensional array of same length but different element equal or not
       boolean result = Arrays.deepEquals(a1, a2);
       System.out.println("Does two dimensional array a1 and a2 are equal : " + result);
       
       
       // checking if two multi-dimensional array of same length, elements equal or not
       result = Arrays.deepEquals(a1, a3);
       System.out.println("Does two dimensional array a1 and a3 are equal : " + result);
       
    }

}

Output :
Does two dimensional array a1 and a2 are equal : false
Does two dimensional array a1 and a3 are equal : true

9. While working in Java, you will need to convert between static array to dynamic array or ArrayList to array multiple times. It's good to know how you can do that quickly and this tip will help you a lot.
10.  There are several ways to initialize arrays in Java. You can either create them without initializing, in that case all buckets will hold default value of element type e.g. if you create an empty array and don't initialize it then all bucket will hold zero, because that's a default value of integer variable in Java. Similarly boolean array by default initialized with false values and String arrays are initialized with null values. If you know the values in advance you can initialize array at the time of creation itself as shown below:

 int[] numbers = {12345}; // valid
 int multipleOfThree[] = {3, 6, 9, 12, 15}; // valid 
 int[] even = new int[]{2, 4, 6, 8, 10}; // valid
11. One bonus tip is that Array is quite different than ArrayList in the sense that later is a dynamic array, it can resize itself when need. On the other hand, you can not change the size of Array once created. Apart from this very fact, there are several other difference between these two classes e.g. ArrayList is part of Java Collection framework but Array is not.

That's all on this list of some important points about array data structure in Java. Use array to hold same type of elements e.g. integers, strings or object, but you can not mix them e.g. Java array can not hold both integer and string at same time. At  compile time it's error but for objects if compiler will allow it will throw ArrayStoreException at runtime. On same note, array is also one of the fasted data-structure for accessing elements if you know the index. Several higher level data structures like HashMap and HashSet are built on top of array because of it's O(1) get performance.

The UNIX System Architecture

The UNIX operating system is a set of programs that act as a link between the computer and the user.

The computer programs that allocate the system resources and coordinate all the details of the computer's internals is called the operating system or kernel.

Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.
  • Unix was originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna.
  • There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are few examples. Linux is also a flavor of Unix which is freely available.
  • Several people can use a UNIX computer at the same time; hence UNIX is called a multiuser system.
  • A user can also run multiple programs at the same time; hence UNIX is called multitasking.

Unix Architecture:

 The main concept that unites all versions of UNIX is the following four basics:

  • Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of the tasks like memory management, tash scheduling and file management.
  • Shell: The shell is the utility that processes your requests. When you type in a command at your terminal, the shell interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the Unix variants.
  • Commands and Utilities: There are various command and utilities which you would use in your day to day activities. cp, mv, cat and grep etc. are few examples of commands and utilities. There are over 250 standard commands plus numerous others provided through 3rd party software. All the commands come along with various optional options.
  • Files and Directories: All data in UNIX is organized into files. All files are organized into directories. These directories are organized into a tree-like structure called the filesystem.

 

Saturday, August 2, 2014

The Traceability Matrix

Sounds like a big word; it is actually a way to trace that all the requirements have been mapped through the development cycle. For example, that the various sections of design have covered all the requirements, test plans and cases cover all the requirements and so on. The traceability matrix document meets the following need: Provide a traceability analysis or matrix which links requirements, design specifications, hazards, and validation. Traceability among these activities and documents is essential. This document acts as a map, providing the links necessary for determining where information is located.
The client who had ordered for the product specifies his requirements to the development Team and the process of Software Development gets started. In addition to the requirements specified by the client, the development team may also propose various value added suggestions that could be added on to the software. But maintaining a track of all the requirements specified in the requirement document and checking whether all the requirements have been met by the end product is a cumbersome and a laborious process. But if high priority is not provided to this aspect of Software development cycle, it may result in a lot of confusion and arguments between the development team and the client once the product is built.
The remedy for this problem is the Traceability Matrix.
Requirements traceability is defined as the ability to describe and follow the life of a requirement, in both a forward and backward direction (i.e. from its origins, through its development and specification, to its subsequent deployment and use, and through periods of ongoing refinement and iteration in any of these phases).
In a software development process, a traceability matrix is a table that correlates any two baselined documents that require a many to many relationship to determine the completeness of the relationship. It is often used with high-level requirements (sometimes known as marketing requirements) and detailed requirements of the software product to the matching parts of high-level design, detailed design, test plan, and test cases.
Requirements Traceability Matrix Document is the output of Requirements Management phase of SDLC. The Requirements Traceability Matrix (RTM) captures the complete user and system requirements for the system, or a portion of the system. The RTM captures all requirements and their traceability in a single document, and is a mandatory deliverable at the conclusion of the life-cycle.