JFreeChart - A great tool to implement Graphs & Charts in Java

While working with graphs in Java, the most strong tool I have ever found is : JFreeChart. Using this , it becomes very easy to create Bar chart, Linear Chart, Pie chart etc. Not only, these charts, it also supports some less popular charts as well, e.g. - wind chart, polar chart etc.
So, to start with JFreeChat, we need 2 jars : jfreechart-1.0.13.jar & jcommon-1.0.16.jar. Just download these jars & add them in the classpath
Here are the  quick links for downloading these  jar files:

jfreechart-1.0.13.jar - Download Here
jcommon-1.0.16.jar - Download here

We will start from a basic project in JFreeChart here.

Reference for Facebook APIs

Today I found an interesting page about Facebook APIs. Facebook has provided sample code & code fragments which you can use in your very own web applications to use FB chat or share or like etc etc.
Here is the URL for the link. I am yet to discover the details -\

https://developers.facebook.com/docs/reference/apis/



Happy Learning !!

Splitting a String into array using newline characters ("\n")

We can split a String & store it into an array using split() method. But, when it comes to a newline character , we need to use a regex for this .

The code will be:

String splittedArray[] = myString.split("\\r?\\n");

If you need to remove the blank lines & not store them in the array, you may use this:

String splittedArray[] = myString.split("[\\r?\\n]+");


Happy Learning

Decimal Formatting in Java

There is a class DecimalFormat in Java for the purpose of decimal formatting.
Have a look at the following code:

import java.text.DecimalFormat;

public class ThreeDecimel {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String j = "781.5400000";
  double i= Double.parseDouble(j);
  DecimalFormat format = new DecimalFormat("#.###");
  System.out.println(format.format(i));
 }
}
But, "#.###" will convert 781.5400000 to 781.54 though we are using 3 decimals after the point. If the requirement is 781.540, we must use the following code fragment, instead of the previous one.

import java.text.DecimalFormat;

public class ThreeDecimel {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String j = "781.5400000";
  i= Double.parseDouble(j);
  format = new DecimalFormat("0.000");
  String a = format.format(i).toString();
  System.out.println(a);
 }

}
For more information, please refer to the following link: Oracle Documenttaion for DecimalFormatter Happy Learning!!

Arrays.sort() or Collections.sort() ? Which one is better to use?

Always using Arrays.sort() gives better performance .
Because, when we call Collections.sort() , it internally converts the list into an array & then sorts the list using method Arrays.sort().

Here is the practical example :

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Sort {

 public static void main(String[] args) {
  //sorted using Collections.sort()
  List aList = new ArrayList<>();
  for(int i =10000000;i>=0;i--)
  {
   aList.add(i);
  }
  long t1 = System.currentTimeMillis(); 
  Collections.sort(aList);
  long t2 = System.currentTimeMillis();
  System.out.println("Time Required using Collections.sort() :: "+(t2-t1)+"ms");
  
  //sorted using Arrays.sort()
  List bList = new ArrayList<>();
  for(int i =10000000;i>=0;i--)
  {
   bList.add(i);
  }
  long t3 = System.currentTimeMillis(); 
  Arrays.sort(bList.toArray());
  long t4 = System.currentTimeMillis();
  System.out.println("Time Required using Arrays.sort() :: "+(t4-t3)+"ms");
  
 }

}


The output for the former one is : 153 ms & that for the later is 76 ms for me.
So, now you know, Using Arrays.sort() is much better than using Collections.sort().


Happy Learning!!

Can you create immutable object in Java?

Yeah, it's pretty simple.
A small line of code can do the magic :) :)

Static final int i = 100;
Now int i is immutable.

Note: An object is immutable means , the value of the object can not be destroyed.

Happy Learning !!


String, StringBuffer & StringBuilder

Java provides us String , StringBuffer & StringBuilder.

So, what is the basic differences between them?
Well , the answer is a theoretical one.

String is immutable, where, StringBuffer & StringBuilder are not.
(To see how String is mutable, see this post - Strings are immutable).

Now, what is the 
Difference between StringBuffer & StringBuilder?
The answer is :
StringBuffer is synchronized & StringBuilder is not.

So, performance wise, StringBuilder > StringBuffer > String (fastest to slowest)

So, When String , StringBuffer & StringBuilder should be used?

1. Use String, when the object is not changing frequently. In this case String gives a better performance, because, while creating a String, it searches String Constant Pool first & if it doesn't exist, then creates a new object. So, memory usage is optimum. When you are doing multiple operation with a string always use StringBuffer or StringBuilder

2. Use StringBuilder, if the object is not used by multiple threads (StringBuilder is not synchronized).

3. Use StringBuffer, if the objects is going to be used by multiple threads in the application. Because, in case of multi-threading, we need synchronization & StringBuffer is synchronized by default.


Happy Learning !!

Comparing String Objects, intern() & other confusions

We all know that we use .equals() to compare two strings & we do not use == to compare them. The primary reason for that is "==" compares the physical location of the Strings(i.e. addresses) & not the value in them.
But, we should know what will happen if we compare two strings using "==" for interviews or may be just for fun!!
Let's follow the below piece of code -
ex-1
public class compareStrings {
 public static void main(String[] args) {
  String a = new String("Test");
  String b = new String("Test");
  // comparing the Strings as objects & not the value
  System.out.println(a==b);
 }
}

The result is : false
It is obvious. Because we are creating two different objects of String class & comparing them. Obviously their addresses are different.
Now, let's look at the below code:

Strings are Immutable

No matter whatever you do, you cannot destroy a String, unless and until JVM is recycled.
Yeah, it's sad but true !!

Java has designed Strings in such a way that Strings are immutable. So, keep in mind that whenever we do operations on a String, it actually creates a new instance of a String & assign the new value into it.











public class ImmutableString {

 public static void main(String[] args) {
  String a = "Test";
  String b = a;
  System.out.println(a==b);
  a= a+"";
  System.out.println(a==b);;
 }
}
The Output is :

true
false

So, we can see that after adding a blank string to the string 'a' the object has been changed. That means that the old String is abadoned now & a new string object is created & the value is now assigned to it.


Happy Learning !!