Friday 21 April 2017

Difference between ArrayList and ArrayList in Java - Raw Type vs Wildcard ~ GNIITHELP

One of my readers asked me about the difference between ArrayList vs ArrayList< in Java?>, which was actually asked to him on a recent Java development interview. The key difference between them is that ArrayList is not using generics while ArrayList is a generic ArrayList but they looks very similar. If a method accepts ArrayList or ArrayList<?> as a parameter then it can accept any type of ArrayList e.g. ArrayList of StringIntegerDate, or Object, but if you look closely you will find that one is raw type while other is using an unbounded wildcard. What difference that could make? Well, that makes a significant difference because ArrayList with raw type is not type safe but ArrayList<?> with the unbounded wildcard is type safe.


You can add objects of any type into raw ArrayList but you cannot do that with a generic ArrayList with unbounded wildcard i.e. ArrayList, it will be a compile-time error, as we'll see by a code example in this article.

This is one of the very interesting Java Interview questions from Generics and Collection, but unfortunately, I didn't have that one in my list of Java Generics Interview questions but will add it soon.
Read More »

Difference between Abstraction and Encapsulation in Java - OOP ~ GNIITHELP

Both Abstraction and Encapsulation are two of the four basic OOP concepts which allow you to model real-world things into objects so that you can implement them in your program and code. Many beginners get confused between Abstraction and Encapsulation because they both look very similar. If you ask someone what is Abstraction, he will tell that it's an OOP concept which focuses on relevant information by hiding unnecessary detail, and when you ask about Encapsulation, many will tell that it's another OOP concept which hides data from outside world. The definitions are not wrong as both Abstraction and Encapsulation does hide something, but the key difference is on intent.

Abstraction hides complexity by giving you a more abstract picture, a sort of 10,000 feet view, while Encapsulation hides internal working so that you can change it later. In other words, Abstraction hides details at the design level, while Encapsulation hides details at the implementation level.

For example, when you first describe an object, you talk in more abstract term e.g. a Vehicle which can move, you don't tell how Vehicle will move, whether it will move by using tires or it will fly or it will sell. It just moves. This is called Abstraction. We are talking about a most essential thing, which is moving, rather than focusing on details like moving in plane, sky, or water.
Read More »

How to compare two XML files in Java - XMLUnit Example ~ GNIITHELP

The XMLUnit library can be used to compare two XML files in Java. Similar to JUnit, XMLUnit can also be used to test XML files for comparison by extending the XMLTestcase class. It is a rich library and provides a detailed comparison of XML files. Btw, comparing XML is completely different than comparing String in Java or comparing object using equals(), as two XML which contains different comment and whitespace can be equals, which is not true for String or character comparison. Also while comparing XML files, it's very important to know exactly which content or part is different and XMLUnit not only shows the content which is different but also XPath of elements which is getting compared.

The heart and soul of XMLUnit are the DifferenceEngine class but we won't use it directly. Instead, you will use Diff and DetailedDiff the two important classes for XML comparison. They provide comparison engine for comparing XML. The XMLUnit library not only compares complete XML document but also can perform a lot of useful activities related to XPath e.g. it can check if an XPATH exists or not exists. It can even check if XPath contains expected value or not.
Read More »

Top 5 books to Learn Object Oriented Programming - Must Read, Best of Lot ~ GNIITHELP

The OOP or Object Oriented Programming is one of the most popular  programming paradigms which helps you to organize code in re the l world system. It's a tool which helps you to write complex software by thinking in terms of objects. Unlike its predecessor procedural programming paradigm which is implemented most notably by C, which solves the problem and complete task by writing code for computers, OOP style of programming allows you to think in terms of real world objects which has both state and behavior. You can view anything as objects and then find their state and behaviors, this will help you to simulate that object in code.

Unfortunately, programmers don't learn OOP or Procedural or Functional programming, what they learn is a programming language and as a side effect of that, they learn these paradigms. Since many developers learn Java, C++, or Python they learn OOP, but not in the true sense, hence a college graduate struggle to apply common OOP concepts in practice.


That's why it's very important for a professional programmer to read a couple of books on Object-Oriented Analysis and design until you learn that, there is no use of learning OOP basics e.g. Abstraction, Encapsulation, Inheritance, or Polymorphism. It is the process of applying those principles in practice which matters.
Read More »

10 Examples of CUT command in UNIX and Linux ~ GNIITHELP

The cut command in UNIX is a nice utility program which allows you to cut data from a text file. The Linux cut command allows you to cut data by character, by field or by column. if used correctly along with sedfindor grep in UNIX, the cut can do lots of reporting stuff. For example, you can extract columns from a comma separated file or a pipe or colon delimited file using cut command. For example, if you are only interested in first two columns you can show them using this command.  In this Linux cut command tutorial we will see different options of cut command, different examples of Linux cut command and some important points about cut in UNIX.

In order to demonstrate the power of cut command through various examples, we will following colon delimited text file as input. This text file contains details of popular smartphones in 2011. The file contains 5 columns i.e. model, company, price, camera, and 4G in the same order.


Here is the content of the file for your reference

Original File
trader@asia:~/perl cat list-of-smartphones-2014.txt
Model:Company:Price:Camera:4G
IPhone4:Apple:1000$:Yes:Yes
Galaxy:Samsung:900$:Yes:Yes
Optimus:LG:800$:Yes:Yes
Sensation:HTC:400$:Yes:Yes
IPhone4S:Apple:1100:Yes:Yes
N9:Nokia:400:Yes:Yes
Read More »

Java Program to Multiply Two Matrices - Matrix Multiplication Example ~ GNIITHELP

How to write a Java program to multiply two matrices in Java is a very good programming exercise to get familiar with the two-dimensional array in Java. this example teaches about how to multiply arrays, how to access elements from a multi-dimensional array, how to pass them to a function etc. Since the matrix is a natural representation of multi-dimensional array in Java, they are often used to illustrate real word matrix exercises e.g. the calculating sum of two matrices or calculating the difference of two matrices etc. By the way, before writing the program, let's recap how to multiply two matrices in mathematics first. If you remember, you can only multiply two matrices if, and only if, the number of columns in the first matrix equals the number of rows in the second matrix. That is known as matrix multiplication criterion.


If both matrices don't satisfy that criterion then the product of two matrices is undefined. The Product matrix's dimensions will be equal to (rows of the first matrix) × (columns of the second matrix ). For example, if we multiply a 2×3 matrix with a 3×1 matrix, then the product matrix or result matrix will be a 2×1 matrix i.e. two rows and 1 columns.

I used to remember this trick by writing dimension of matrices adjacent to each other and canceling their matching dimension e.g. if you write 2x3 and 3x1, and then cancel 3 from each side you will get a matrix of dimension 2x1, which is basically the dimension of product matrix.
Read More »