Hello Kishan, welcome to the Ranch.
ArrayList without the generics is called a
raw type. Raw types exist in Java only because of backward compatibility. When a new version of Java comes out, Oracle is always extremely careful to make sure that code written for the previous version still works without changes on the new version.
In Java 5, generics were added to Java. But there was at that point already a lot of code written without generics, and it was very important that this continued to work. So it is still possible to write code without using generics.
You should not do this, however, when you write new code. It is only necessary for backward compatibility.
kishan Jaiswal wrote:Why generic are called compile time ?
When the Java compiler translates your source code to byte code, it uses the generics in your code to check if you didn't make any mistakes with the types (for example, putting the wrong kind of object in a List<String>). But the generics are not translated into byte code - the byte code that is produced contains no information about the generics that you used.
So, generics are only important at compile time (when the compiler is translating your code) and not at runtime (when the JVM is running your program).
kishan Jaiswal wrote:How JVM internally handle this two.
Therefore, there is no difference between these two at the JVM level - the byte code for both of these two lines is the same.
Note that since Java 7 you can write this:
Notice the <> on the right side. This is shortcut syntax, so that you don't have to repeat the <String> that you already wrote on the left side. So, this syntax is exactly the same as:
but just with a little shorter syntax.