JAVA

String is immutable

Overview We will find answers for following questions What is String pool. How does memory allocation works for String. Why are String immutable. String pool String pool is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. Does that make sense ??? If not Read more…

By Ashish, ago
Collections

Collection Framework in Java – Overview

What Is a Collections Framework? A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following: Interfaces Implementations Algorithms Benefits of the Java Collections Framework Reduces programming effort: By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level “plumbing” required to make it work. Increases program speed and quality: This Collections Framework Read more…

By Ashish, ago
JAVA

Difference between Comparable and Comparator in Java

Based on my previous posts related to Comparator and Comparable let’s see the difference. Comparable Comparator Should be used when sorting with one element only Should be used when sorting with multiple elements Need to implement compareTo(Obj ob) Need to implement compare(Obj e1, Obj e2) Found in java.lang package Found in java.util package Actual class is not modified. Actual class is modified. Compares “this” reference with the object specified Compares two objects provided to compare

By Ashish, ago
JAVA

Sorting in Java (Comparator)

In my previous post we saw Comparable and it’s implementation. Java provides another way to compare objects. Instead of implementing the comparable interface, we can implement the Comparator interface. The main difference between these two interfaces is that the comparable interface defines one method compareTo(), which takes one parameter. The comparator interface defines one method compare() that takes two parameters (of the same type) and returns the comparison of these two objects. Lets see implementation of comparator User.java UserComparatorExample.java Read more…

By Ashish, ago
JAVA

Sorting in Java (Comparable)

How do we sort in Java? We can go the traditional way by implementing Bubble Sort or Merge Sort etc We can use Java APIs In this post we will focus on Sorting using Java APIs Sort List of Integer Given an Integer List say [7, 4, 2, 9, 5, 2, 3] to sort We will use Java Collection Framework to do this. Points to Note: List<Integer> => The Integer class wraps a value of Read more…

By Ashish, ago
JAVA

Date Conversion in Java

Let’s start with 2 basic rules to follow Convert Date (java.util.Date) into String => format Convert String into Date => parse I have used SimpleDateFormat which extends DateFormat of java.text package. Date can be in various format. Here are few Lets see an Example Note: Suppose we need to convert String Date format to another String Date format. Implementing above rules, Convert String => Date => String Handling Timezone To get Date time for a particular timezone just pass timezone Read more…

By Ashish, ago
JAVA

Exception Handling in Java

An exception is an abnormal condition that arises in a code sequence at run time. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Workflow Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch) and handle it in some rational manner. To manually throw an exception, use Read more…

By Ashish, ago
MAVEN

Setup Maven for Windows, Mac and Linux

Maven Maven is a build automation tool used primarily for Java projects. Consider a project consisting of 100s and 1000s of Java file. need to compile each and every .java file for .jar packing. Similarly in case of .ear or .war file we need to add all the dependencies needed along with .jar files. You can see compiling and adding dependencies for 100s of files is a serious task. This is place where maven comes Read more…

By Ashish, ago
JAVA

Hello World

Let’s start with a little program in java to print a line “Hello World” Line 1 public : One of Access Modifiers. It makes this piece of code visible to the world. class : A class is the blueprint from which individual objects are created. HelloWorld : Name of the class Line 2  static : static is a keyword which allows main( ) to be called without having to instantiate a particular instance of the class. void Read more…

By Ashish, ago