Instance initializer block

Inside instance block we can perform some extra operations while initializing the instance variable. Of cause we can do initialization inside constructor also, but when we have multiple constructors (overloaded constructors) in our class, and we have certain common operations or initialization in each constructor then we can write those common operations in instance block. Like this way we can make sure that irrespective of any constructor invoked that common operations would be executed or taken care by JVM itself.

This instance block helps to avoid boiler plate code in overloaded constructor

There are mainly three rules for the instance initializer block. They are as follows

  1. The instance initializer block is created when instance of the class is created.
  2. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call).
  3. The instance initializer block comes in the order in which they appear.

Code before compile

public class Demo {
  System.out.println("Hello Instance block1");
  System.out.println("Hello Instance block2");
  System.out.println("Hello Constructor");
 void display() {
  System.out.println("Hello Display");
 public static void main(String[] args) {
  new Demo().display();

Code After Compile(Class file)

public class Demo {
   System.out.println("Hello Instance block1");
   System.out.println("Hello Instance block2");
  System.out.println("Hello Constructor");
 void display() {
  System.out.println("Hello Display");
 public static void main(String[] args) {
  new Demo().display();

Instance initializer block is useful when there are multiple constructors overloaded in the same class, then we might have to repeat some set of codes in all constructor instead we can make use of initializer block which is automatically called in all the constrcutors.

public class Demo {
  System.out.print("Hello Instance block ");
  System.out.print("Hello Constructor1 ");
 Demo(String x){
  System.out.print("Hello Constructor2 ");  
 void display() {
  System.out.println("Hello Display ");
 public static void main(String[] args) {
  new Demo().display();
  new Demo("Hello Heapwizard").display();

GIT aliases

GIT does not infer to your commands if you type it partially, but if you don’t want to type the entire command with every time you can create aliases for these commands so that you can run these command quickly.

git config --global alias.<alisa_name> <command_with_attibutes>

Some of the alias that i use are below

$ git config --global checkout
$ git config --global branch
$ git config --global 'commit -v'
$ git config --global 'status -s'

GIT Branching

How to create new branch in git?

git branch <branch_name>

Note creating new branch only creates new branch it does not switch you to that new branch. You have to do that new branch manually

Check the list of branch present for in the working repository?

git branch
* shows the working branch

You can see which branch of the project is pointing also using the command git log –oneline –decorate

git log --oneline --decorate

Switching branch?

git checkout testing
Note after switch HEAD is pointing to the testing branch

If you want a detailed map of which branch is created from which branch you can run the below command to get the graph of it.

 git log --oneline --decorate --graph --all

You can create a new branch and switch to that new branch in one line using checkout command with -b switch.

git checkout -b <new branch name>

Remote repository in GIT (GIT Remote)

To work in a collaborative project we require GIT repository to be hosted at some location which is accessible to the project technical team. This access can be ready only or read and write depending upon the role you are assigned in the team.

git remote

Git remote is the command to get the shortnames assigned to the repository, using the attribute -v you can check if you have fetch(read) and push(write) access to the repository

When you execute the command git clone <url> then GIT automatically adds the remote of the URL under the short name origin and then executes the command git fetch origin

git clone

is same as

git remote add origin
git fetch origin

git remote show <shortname of remote>

You can find the detail information about a particular remote using in the git remote show command.
git remote show :

Above command shows more details about the repository

Amending, unstaging and unmodifying files in GIT

You can make amendments to your comments using the below command

git commit –amend

If you have just committed a file and you want to make some changes to the same commit you can do that using amend in GIT

You can remove the files added to the staging area using the reset command

git reset <filename>

You can remove a file added to the staging area using the reset command.

Unmodifying a file can be done by checking out the file from the repository

git checkout — <file_name>

You can checkout the file from the repository directly using this command, note the changes made to the file will be lost after running this command.

Viewing GIT logs

git log

git log is the GIT command that shows you the commit history of your repository in reverse chronological order(most recent comments shows up first) when executed without any arguments.

Details shown are
1. The SHA-1 checkshum (Example : ca82a6dff817ec66f44342007202690a93763949)
2. Name of the Author name with email id (Example : Scott Chacon
3. Date of commit (Example : Mon Mar 17 21:52:11 2008 -0700)

GIT Ignore and removing files

If you want certain files not to be read by git then you can add those files in the .gitignore file.
This is more useful when your application creates log files within the projects itself, or there is a file which when opened with editor it creates temp file(Example Microsoft Word). You don’t those temp files to be visible when you check the status of your repository. Hence you can add the pattern of those temp files in the .gitignore file.

So the name of the file is .gitignore and you cannot create this file directly in windows, run the below command in command prompt to create the empty .gitignore file in the root directory of your repository

copy NUL .gitignore

Rules for the patterns you can put in the .gitignore file are below
1. Lines starting with # will be considered as comments and ignored
2. Standard global patterns work and it is applied recursively in the complete files in the project.
3. Patterns starting with / Forward slash are avoided recursively
4. Patterns ending with / Forward slash specified a directory
5. You can negate a pattern by starting it with an exclamation point !

Now you can see some of the sample patterns below

# ignore all .tmp files
# but do track lib.a, even though you're ignoring .a files above
# only ignore the ABC file in the current directory, not subdir/ABC
# ignore all files in any directory named bin
# ignore bin/comments.txt, but not bin/deploy/comments.txt
# ignore all .pdf files in the bin/ directory and any of its subdirectories

Now when we create the file accordingly it will not be shown when we run the GIT status command.

Example : In the below example we have created hello.txt and hello.tmp, but since add .tmp file has to be ignore according to the *.tmp added into the .gitignore file, hence when we run git status -s command after creating hello.txt and hello.tmp only hello.txt is displayed and not hello.tmp. Screenshot of the example below.

Removing files from GIT

Removing files from GIT can be done using git rm command. But just git rm command will not remove the files but only remove the files and put it into staging area, you need to commit the files to actually remove.

git rm <file_name>
git commit -m "Comment"

Removing file from GIT but keeping a copy in local

We can remove the file from GIT but keep the file in local i.e make GIT no longer track the file using the below command

git rm --cached "Removing file from GIT but keeping the file in local"
git commit -m "Comment"


Benefits of Lambda
1. To enable functional programming in java.
2. Write more readable, maintainable and concisely.
3. Write one or API very easily and effectively.
4. To enable parallel processing.

Explaining Lambda :
Since java is a object oriented language and if we create a function/method in java we need to create a class for it but suppose if the class is has only one method and we don’t want to create a class just to store one method in it then in that case we can use lambda
So what is lambda? Just like we assign a value to a variable like String name = “Java” or int x = 10 , we can assign a function itself to a variable.
Lets us take an example of

Interface obj = public void perform(){
                    System.out.println("Hello World");

So what all things can be removed from the above method perform to make it more concise? Since the function will be available to all the method which has the variable hence it make no sense to add access modifiers lets remove that.

//Removed access specifiers
Interface obj = void perform(){
                    System.out.println("Hello World");

Now in the above code what can be removed to make it more concise? since the name of the function is required to call it but if it assigned to a variable we don’t the name of the function hence remove it and check how it looks

//Removed name of the function
Interface obj = void (){
                    System.out.println("Hello World");

Now when you look at the above code it is half way reached to the lambda expression, now what java 8 compiler says is the block of code which is assigned to temp variable and java 8 compiler can identify the return type of the block of code just by looking at the block of code we dont want explicitly notifying the return type of variable in case of lambda hence we can remove that too, now the function that is assigned to a variable looks something like below

//Removed the return type of the method
Interface obj = (){
                    System.out.println("Hello World");

Good now we have create a block of code which can be assigned to a variable but how can we tell compiler that this is a lambda express? By using the symbol -> in between the bracket of parenthesis and curly braces of code, now after adding that the code looks some thing like this.

//Added the symbol-> to make java compiler understand that it is a lambda exp
//Lambda Expression 1
Interface obj = () -> {
                    System.out.println("Hello World");

Now if your code has only one line then you can also remove the curly braces and the code looks something like this

//Curly braces can be removed if code is of single line
//Lambda Expression 2
Interface obj = () -> System.out.println("Hello World");

Now when suppose you have a lambda expression when returns a value, following the above steps it would look something like this :

Interface obj = (int a, int b) -> return a + b;
//Not valid keyword return since it is a onliner code

But since your code is just one line you can skip the keyword return as well, after removing that the code will look something like this

//Since since line code we can skip keyword return
//Lambda Expression 3
Interface obj = (int a, int b) -> a + b;

Now when you see the example above you can see that in lambda we are implementing one of the function declared in the interface, hence we have the data type of the parameters declared in the interface itself and hence we do not require the type of inputs to be mentioned in the lambda hence we can remove that as well.

//Since input parameters data type is mentioned in function declaration of interface we can remove that from the below lambda expression
//Lambda Expression 4
Interface obj = (a,b) -> a + b;

Now suppose we have only one input argument mentioned in the lambda then we can remove the brackets from the lambda as well

//one input argument example
Interface obj = (a) -> a *2 ;

Then we can remove the brackets from the above example since we can omit it in lambda the expression looks like this

//one input argument example
//Lambda Expression 5
Interface obj = a -> a *2 ;

What java 8? What intention of java 8.

Objectives of Java 8

  1. To simplify programming : Concise code
  2. To gain the benefits of functional programming in java (Lamada Expression)
  3. To utilize the advantage of parallel processing in jav(Parallel streams)

Features of Java 8

  1. Lambda Expressions
  2. Functional Interfaces
  3. Default Method in interfaces
  4. Static method inside interfaces
  5. Predicate (Predefined functional interfaces)
  6. Function (Predefined functional interfaces)
  7. Consumer (Predefined functional interfaces)
  8. Method reference and Constructor reference by double colon(::)operator
  9. Stream API
  10. Java 8 Date and Time API( Joda API)

Types of files status in GIT and viewing status of files and difference in them.

Files in a local GIT repository can be in 4 status :
a. Untracked : These files are newly added into the repository.
b. Unmodified : These files exist in repository and local and both have same content.
c. Modified : These files exist in repository and local but some changes are made to the local copy of the file.
d. Staged : These files exist in repository and local but some changes are made to the local copy of the files and the files has been added to the staging area using git add <file_name> command. These file will go with the next commit made to the repository

Status of GIT files can be viewed using below commands

git status
git status -s
#to see the time taken to check status
time git status

So there are different stages files in GIT for example below

Command : git status -s
  1. File f1.txt is modified but not yet staged hence M in the right column with read color
  2. File f2.txt is modified and added to the staging area hence M in the left column with green color
  3. File f3.txt was modified and added to the staging area but after that again there was modification done to f3.txt this time it was not added into staging are. If we commit f3.txt now GIT will only commit that version of file which was added to the staging area, the modified version of f3.txt will not be committed
  4. File f5.txt was a completely new file, it was created and added to the staging are hence A in the left column with GREEN color signifying it a newly added file
  5. File f4.txt displays ?? in the left and the right column which signifies it is an untracked file which is not present in the repository nor added into git staging area.

Now the same set of status can be viewed as a complete version with command git status, below is the screenshot of how it looks.

Command : git status

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