Why Generics in java

Quick Points :

  • The purpose of Generic is to
    • provide type-safety (At compile time determined)
    • Type casting issue – (If non generic is used TypeCasting is mandatory)
    • A generic code can be written for different types like Sorting and Search for CustomeObjects
    • Generics concept only applicable at Compile time, at Runtime no Generic is present

Why Generics ?

  • Arrays are type safe when we put some value other than defined one it gives compile time error
  • But in Collection like ArrayList the data type is not defined any type of object can be stored which causes the runtime exception when understermined type of object is tried to cast into another object

  • Generics was introduced in java 1.5
  • Generics enable classes and interfaces to be parameters while defining it, these parameters are type parameters.
  • Type parameters helps to provide a way to reuse the same code but with different input argument types.
  • The reason why Generics came into picture is to reduce the runtime exceptions in java programs, since before java 1.4 collections does not use to take parameters and developers where not sure which type of object is stored in that collection, developer always had take the object from the collection and cast it and check it, and if the object does not cast properly then it would result into a run time exception. To prevent it from happening using generics we can define what kind of object is stored in the collection while declaring it itself and could prevent the effort of casting the object and possible run time exception while code execution.
  • Thus by using generics we could prevent a lot of run time exceptions by declaring the type along with collection declaration.
  • We need not worry about casting the object since JVM already knows what kind of object is stored in the collection.
  • We can implement sorting searching algorithm in a very generic way using the generics functionality.

Advantages of Generics over non generic code:

  1. Stronger type check at compile time
    If violated then it results into a compile time error.
  2. We can eliminate casting
    List nameList = new ArrayList();
    String name = (String)nameList.get(0);

    List<String> nameList = new ArrayList<>();
    String name = nameList.get(0);
  3. Generic algorithms can be implemented to reuse search and sorting algorithms with the same code.

Comparing difference between files in git(Meld GUI tool)

GIT diff

If git status command is not enough for you and you want to find out what content of the files has been changed then you can do that using git diff

git diff command compares the committed files with the files that are not yet added to the staging area, if you want to compare the committed files with the staging area you need to run the command git diff command with —staged attribute, example below

Now can check what you have staged so for using the command git diff –cached

git diff --cached

There exist many different GUI tools under which you can view the GUI differences, run the below command to see the list of tools available and list of tools installed on your system.
Note : Some of these GIT diff tools are commercial product

git difftool --tool-help

We are going to use Meld tool as a git diff tool, for windows you can directly download the exe from their website, for ubuntu users you can run the below command

sudo apt-get install meld

Below commands can be used to set up meld tool on GIT as difftool and merge tool

git config --global diff.tool meld
git config --global difftool.meld.path "/usr/bin/meld"
git config --global difftool.prompt false
git config --global difftool.meld.cmd meld "$LOCAL" "$REMOTE"

git config --global merge.tool meld
git config --global mergetool.meld.path "/usr/bin/meld"
git config --global mergetool.prompt false

#confirm the path of meld using command : which meld

Now you can compare the unstaged and commited data using the meld difftool using below command:

git difftool
git difftool --staged #to Compare the staged data and the committed data

You use git difftool in exactly the same way as you use git diff. e.g.

git difftool <COMMIT_HASH> file_name
git difftool <BRANCH_NAME> file_name
git difftool <COMMIT_HASH_1> <COMMIT_HASH_2> file_name