How to learn java programming
Tags: java, programming
Published: 2019-04-11 17:13:48

---
title: How to learn java programming
published: true
description: 
tags: #java #programming
---
Java is one of the most used programming languages in the world for developing the desktop application and web application. It has completed 22 years in the programming world.

In this post, we will see how to learn java programming in depth and in the most efficient way.

Java has a huge learning curve, it is a complete programming language with lots of classes, APIs, etc. Don' try to learn everything at once, go step by step and master Java programming world.
<h2>Ways to learn Java Programming:</h2>
<h3>Read books:</h3>
There are many good books from which you can learn Java Programming.
Some of them are:
<ul>
 	<li>Head first Java</li>
 	<li>Effective Java</li>
 	<li>Thinking in Java</li>
</ul>
Head first Java is my favorite among all. I really liked head first Java and enjoyed reading it. Head first Java explains Java Concepts in very unique and interesting way. You will be able to learn Class, Object, Thread, Collections and Generics and various other concepts.

<h3>Follow good Java blogs:</h3>
There are many good online blogs/website from which you can learn Java easily.

Some of them are:
<ul>
 	<li><a href="https://docs.oracle.com/javase/tutorial/" target="_blank" rel="noopener">Oracle docs</a></li>
 	<li><a rel="nofollow" href="https://www.javacodegeeks.com/" target="_blank" rel="noopener">Javacodegeeks</a></li>
 	<li><a rel="nofollow" href="https://dzone.com/" target="_blank" rel="noopener">Dzone</a></li>
 	</ul>
I have also created a <a href="http://www.java2blog.com/2017/05/java-tutorial-for-beginners.html">complete core java tutorial for beginners and experienced programmers</a> which will help you to learn Java Programming.
<h2>Tips to learn the internals of Java:</h2>
<h3>Understand Java basics:</h3>
It is always essential to understand basics of any language when you get started. You need to understand basic concepts of Java such as <a href="http://www.java2blog.com/2017/05/object-class-java.html">classes, objects</a>, packages, <a href="http://www.java2blog.com/2017/05/variables-java.html">variables</a>, keywords, <a href="http://www.java2blog.com/2017/05/data-types-in-java.html">data types </a>etc.
<h3>Think in Object oriented way:</h3>
As you know Java is an Object oriented programming language, you need to understand object oriented concepts such as <a href="http://www.java2blog.com/2017/04/abstraction-java-example.html">abstraction</a>, <a href="http://www.java2blog.com/2017/05/encapsulation-java-example.html">encapsulation</a>, <a href="http://www.java2blog.com/2017/05/polymorphism-java-example.html">polymorphism</a>, and <a href="https://java2blog.com/inheritance-java/">inheritance</a>

You need to think in terms of object and class. For example Employee has attributes such as name, age, and behavior such as completeAssignment etc.
<h3>Master Collections:</h3>
Collections framework is the most used framework in Java.Don't just understand the usage of collections such as HashMap, HashSet, TreeMap, Treeset but also understand the internal working of Collections.

For example: If you know <a href="http://www.java2blog.com/2014/02/how-hashmap-works-in-java.html">internal working of HashMap</a> and then it is easy to debug program and see the content of HashMap. It will also help you understand how Java collections are designed.
<div style="text-align: justify;">Let's take a very simple example. I have a Country class, we are going to use Country class object as key and its capital name(string) as value. Below example will help you to understand, how these key value pair will be stored in hashmap.</div>
<b>1. Country.java </b>
<pre name="code" class="">package org.arpit.java2blog;
public class Country {

 String name;
 long population;
 
 public Country(String name, long population) {
  super();
  this.name = name;
  this.population = population;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public long getPopulation() {
  return population;
 }
 public void setPopulation(long population) {
  this.population = population;
 }
 
 // If length of name in country object is even then return 31(any random number) and if odd then return 95(any random number).
 // This is not a good practice to generate hashcode as below method but I am doing so to give better and easy understanding of hashmap.
 @Override
 public int hashCode() {
  if(this.name.length()%2==0)
   return 31;
  else 
   return 95;
 }
 @Override
 public boolean equals(Object obj) {
  
  Country other = (Country) obj;
   if (name.equalsIgnoreCase((other.name)))
   return true;
  return false;
 }
  
}</pre>
If you want to understand more about hashcode and equals method of object, you may refer<b> <a href="http://www.java2blog.com/2014/02/hashcode-and-equals-method-in-java.html" target="_blank" rel="noopener noreferrer">hashcode() and equals() method in java</a></b>

<b>2. HashMapStructure.java</b>(main class)
<pre name="code">import java.util.HashMap;
import java.util.Iterator;
  
public class HashMapStructure {
  
    /**
     * @author Arpit Mandliya
     */
    public static void main(String[] args) {
          
        Country india=new Country("India",1000);
        Country japan=new Country("Japan",10000);
          
        Country france=new Country("France",2000);
        Country russia=new Country("Russia",20000);
          
       HashMap&lt;Country, String&gt; countryCapitalMap=new HashMap&lt;Country,String&gt;();  
        countryCapitalMap.put(india,"Delhi");  
        countryCapitalMap.put(japan,"Tokyo");  
        countryCapitalMap.put(france,"Paris");  
        countryCapitalMap.put(russia,"Moscow");  
            
        Iterator countryCapitalIter=countryCapitalMap.keySet().iterator();//put debug point at this line  
        while(countryCapitalIter.hasNext())  
        {  
            Country countryObj=countryCapitalIter.next();  
            String capital=countryCapitalMap.get(countryObj);  
            System.out.println(countryObj.getName()+"----"+capital);  
         }  
        }  
  
  
}</pre>
<div style="text-align: justify;">Now put debug point at line 24 and right click on project-&gt;debug as-&gt; java application. Program will stop execution at line 24 then right click on countryCapitalMap then select watch.You will be able to see structure as below.</div>
<div style="clear: both; text-align: center;"><a style="margin-left: 1em; margin-right: 1em;" href="http://3.bp.blogspot.com/-T4TsTlhwaXU/Uwjj9ypm69I/AAAAAAAACsY/J4-a6rxZLLE/s1600/HashMapStructure1.bmp"><img class="alignnone" src="https://3.bp.blogspot.com/-T4TsTlhwaXU/Uwjj9ypm69I/AAAAAAAACsY/J4-a6rxZLLE/s1600/HashMapStructure1.bmp" alt="How HashMap works in java" width="640" height="363" border="0" /></a></div>
<div style="text-align: justify;">Now From above diagram, you can observe following points</div>
You have put 4 entries in HashMap but you see only two entries in Entry[] array. If you want to understand why there are only two entries in Entry[] array, you need to understand <a href="http://www.java2blog.com/2014/02/how-hashmap-works-in-java.html">How HashMap works in </a>java and Hashcode and equals method.

This is a very simple HashMap example but you might much more complex example in real life, hence to debug the program and see values in HashMap, it is essential to learn the internal working of HashMap.
<h3>Choose any IDE:</h3>
You can choose IDE such as Eclipse, intellij or Netbeans. It will help you to do programming faster and will in turn increase your efficiency. You should also learn some important keyboard shorts for that IDE.

For example:

When you copy any code and put any code in eclipse, you need to import all import statements for that program, you can simply use <a href="http://www.java2blog.com/2016/02/how-to-import-all-classes-at-once-in.html">control+ shift+ O in eclipse</a> and it will take care of imports. Hence it will help you to increase your efficiency while programming.
<h3>Write Basic Programs:</h3>
Practice <a href="http://www.java2blog.com/2017/06/java-interview-programs-for-freshers.html" target="_blank" rel="noopener noreferrer">basic java programs</a> on String, Numbers and Array etc. It will help you to write simple logics.
<h3>Practice at HackerRank:</h3>
Once you are familiar with collections and object-oriented concepts, you can practice Java programming at <a href="https://www.hackerrank.com/domains/java/java-introduction">HackerRank</a>. it will help to master syntax and building logic.
<h3>Learn Multithreading:</h3>
It is quite hard to master Multithreading. It requires good amount of practice to learn multithreading as thread outputs are unpredictable in most of the cases. You can learn basics of threads such as <a href="http://www.java2blog.com/2015/06/java-thread-example.html">how to create a thread</a>, life cycle of thread and different method of Thread class such join, sleep, yield and thread synchronization.

Once you are comfortable with basics of thread, learn executor framework. Java 5 has introduced executor framework which takes care about thread creation and management, you can just concentrate on business logic.
<h3>Create small Projects:</h3>
You can create simple projects of your choice such as Library Management, Transport system. Create a simple design, Identify major entities, use cases and write a Java application to solve the program.
<h3>Browse some good Projects on github:</h3>
There are many good open source projects on <a href="https://github.com/">GitHub</a>. You can browse some good projects, it will help you to understand some good practices on design and coding.
<h3>Learn design pattern:</h3>
It is always a good idea to learn some design patterns. Learn a few design patterns and apply them in Java applications.

<strong>For </strong>example: <a href="http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html">Observer design pattern </a>depicts publisher-subscriber relationship.You can identify use case and easily implement it in Java program.
<h3>Practice, Practice, and Practice:</h3>
There are no shortcuts to learn Java. If you want to become a good Java developer, you need to practice more and more.

That's all about How to learn Java Programming. If you have more suggestions, please do comment.