Comparision of Java versions

Today I have learned java basics. Thats not about java programming. Specially about java versions. In the lecture, we have learned 3 java versions
  • Java 6
  • Java 7
  • Java 8
We have compared all versions of them. 

First we look at the java 6 and lets compare with java 7.
It's also called Java 1.6. Java 6 have less functionalities than newer versions (it must be like that). As we discussed, java is made with C and C++. So the base of java is C and C++. So in Java also there are some parts inherited by its relatives (C/C++). We can get an example, 
  • In C and C++, switch case statement checks only integer value. As the same Java 6 also inherited that thing. 
But in normally, we get some scenarios to check chars, strings. So what happen to them? We use if else conditions to rid over problem. But if we thing of performance, if else statement has worst performance with compared to switch statement. As a solution for that problem, Java language developers introduced switch case statement with support of string, chars even booleans as a part of Java 7.

Try with resource is another difference changed with java 7. Lets see what is Try-with-Resource.
Lets get an example,

public static void main(String[] args)
{
   BufferedReader br = new BufferedReader(new FileReader(filePath));
   try
   {
      String line = br.ReadLine(); //Read first line of file
   }
   finally
   {
       br.Close();
   }
}

we use above code to read file in java. But after reading that file, we have to close that file. If we don't close, It will throw IOException. Then lets see new approach of java 7 (Try-with-resource).

   try(BufferedReader br = new BufferedReader(new FileReader(filePath)))
   {
      String line = br.ReadLine(); //Read first line of file
   }

see, BufferedReader definition and declaration has sent into try block's ( ), It says the resources (BufferedReader is the resource in our example) are only available for the scope of it's try block. Then what happen after try block is over? (and also we haven't closed resources.) Is it won't throw Exceptions?
Actually it throws exceptions, but in java 7 Try-with-Resources will suppress that exceptions. So we don't have to be worry. Is it not a real advantage?

Another new feature is Java 7 give chance to give binary values for  (byte, short, int, long)  values.
Ex.
instead of        int x = 3589
we can use      int x = 0b00000000000000000000111000000101
Note.
In binary value 0b is prefix to specify that it is a binary value.

Another new feature is multi catch exceptions. 

try
{
       Scanner scanner = new Scanner(System.in);
       String inputValue = scanner.getString();
       int myValue = Integer.parseInt(inputValue);
}
catch(NumberFormatException | NullPointerException ex)
{
      ex.printStackTrace();
}

This will help to reduce number of code lines.

Comments

Popular posts from this blog

Introduction to docker!

How to view queries executed in MySQL?

CSRF Defence - Synchronizer Token Pattern Demo