What is Autoboxing , Generics, Enum and Varargs method in Java 5
Java 5 introduces Autoboxing, Generics, varargs, and Enum along with several other features and improvement. It's been few years when Java programming language was enhanced with these features but still Java programmer thing Autoboxing, Enum, Generics or Variable arguments as an advanced feature and afraid to learn them. They are very much part of Java fundamentals just like Abstraction, Inheritance, Encapsulation, and Polymorphism are part of Object-oriented programming concepts. It's important to understand what are these feature and How to use them, even if you don't use them in your code, you may have to work on someone else code which is written in Java 5 and uses Generics Collection, Autoboxing quite frequently. In this Java 5 tutorial, we will see the brief overview of Java 5 features like Generics, Enum, Autoboxing and unboxing and variable arguments methods along with examples and explanations.
What is Autoboxing in Java
First starts with Autoboxing feature of Java 5 which allows a programmer to forget about converting primitive to wrapper class code. Autoboxing means Java compiler will automatically convert primitive to object wherever necessary.
For example, if you are writing a method which accepts an int argument and you are passing an Integer object, Unlike previous Java version, Java compiler will not throw any compile time error, Instead, it will convert Integer to int by using unboxing in Java.
On the other hand, if method expect a Double Object as an argument and you pass double primitive then Java will convert primitive to a Double object using Autoboxing in Java. See What is Autoboxing in Java - Example corner cases for more information and detailed example of how to use autoboxing in Java.
One of the points which is worth noting while using autoboxing in Java 5 code is not mixing primitive with an object while comparing with equals objects, this may give you unpredictable results, see What is the problem while using "==" with autoboxing in Java 5 for complete details on actual problem.
What is Generics in Java
Generics is another high-value addition in Java programming language added on JDK 5. Generics provides compile time type-safety and reducing risk on ClassCastException in Java. By using Generics in Java, you can detect type-safety related error compile timer rather than get them at runtime. You can also use Generics wildcards in Java to write better type-safe API methods. Generics works on the principle of type-erasure, which erases all type information during compile time. for more details see How Generics works in Java. If you are preparing for any Java 5 interview and looking for some interview questions on Generics, then this list of Java Generics question will help you to prepare better.
What is Enum in Java
Enum is a way to represent a fixed set of well-known values in Java e.g. number of days in a week, states of water, number of months in a calendar year etc. After Generics, Enum is my second favorite introduced in Java 5. Enum in Java is not like its counterparts in C and C++, where it just used to represent a fixed number of things, It is much more volatile can do lot more things which is not even possible in C or C++. Enum in Java is a Type like any class or interface in Java. Enum also provides type-safety and readability compare to popular Enum int pattern and String enum pattern popular before Java 5. One of the popular use of Enum is to create Thread-safe Singleton using Enum in Java. Enum can override a method in Java, Enum can implement the interface in Java. All Enum extends java.lang.Enum and compile time constant. See 10 Example of Enum in Java for more examples on how to use Enum in Java.
What is the variable argument method in Java
As the name suggests variable argument method are Java method which can accept a variable number of arguments e.g. you can call the method by passing either 1 argument or two arguments or even zero arguments. The main method in Java 5 is retrofitted as a varargs method and now its String[] argument is converted to variable arguments. You can implement the varargs method even prior to Java 5 either by using an anonymous array or using Collection classes e.g. List or Set. Variable argument methods are also called as varargs and they are denoted by three ...(dot) after there name. See What is the varargs method in Java and How to use it for more details on variable arguments.
More About :
Java 1.5 version also introduced the following features :
1) Introduction of Java concurrency utilities e.g. CountDownLatch
2) Java ThreadPool Framework e.g. Executor
3) Concurrent Collection classes from java.util.concurrent package e.g. ConcurrentHashMap and CopyOnWriteArrayList, BlockingQueue etc
4) Changes on Java memory model and happens before relationship
Java 5 introduces Autoboxing, Generics, varargs, and Enum along with several other features and improvement. It's been few years when Java programming language was enhanced with these features but still Java programmer thing Autoboxing, Enum, Generics or Variable arguments as an advanced feature and afraid to learn them. They are very much part of Java fundamentals just like Abstraction, Inheritance, Encapsulation, and Polymorphism are part of Object-oriented programming concepts. It's important to understand what are these feature and How to use them, even if you don't use them in your code, you may have to work on someone else code which is written in Java 5 and uses Generics Collection, Autoboxing quite frequently. In this Java 5 tutorial, we will see the brief overview of Java 5 features like Generics, Enum, Autoboxing and unboxing and variable arguments methods along with examples and explanations.
What is Autoboxing in Java
First starts with Autoboxing feature of Java 5 which allows a programmer to forget about converting primitive to wrapper class code. Autoboxing means Java compiler will automatically convert primitive to object wherever necessary.
For example, if you are writing a method which accepts an int argument and you are passing an Integer object, Unlike previous Java version, Java compiler will not throw any compile time error, Instead, it will convert Integer to int by using unboxing in Java.
On the other hand, if method expect a Double Object as an argument and you pass double primitive then Java will convert primitive to a Double object using Autoboxing in Java. See What is Autoboxing in Java - Example corner cases for more information and detailed example of how to use autoboxing in Java.
One of the points which is worth noting while using autoboxing in Java 5 code is not mixing primitive with an object while comparing with equals objects, this may give you unpredictable results, see What is the problem while using "==" with autoboxing in Java 5 for complete details on actual problem.
What is Generics in Java
Generics is another high-value addition in Java programming language added on JDK 5. Generics provides compile time type-safety and reducing risk on ClassCastException in Java. By using Generics in Java, you can detect type-safety related error compile timer rather than get them at runtime. You can also use Generics wildcards in Java to write better type-safe API methods. Generics works on the principle of type-erasure, which erases all type information during compile time. for more details see How Generics works in Java. If you are preparing for any Java 5 interview and looking for some interview questions on Generics, then this list of Java Generics question will help you to prepare better.
What is Enum in Java
Enum is a way to represent a fixed set of well-known values in Java e.g. number of days in a week, states of water, number of months in a calendar year etc. After Generics, Enum is my second favorite introduced in Java 5. Enum in Java is not like its counterparts in C and C++, where it just used to represent a fixed number of things, It is much more volatile can do lot more things which is not even possible in C or C++. Enum in Java is a Type like any class or interface in Java. Enum also provides type-safety and readability compare to popular Enum int pattern and String enum pattern popular before Java 5. One of the popular use of Enum is to create Thread-safe Singleton using Enum in Java. Enum can override a method in Java, Enum can implement the interface in Java. All Enum extends java.lang.Enum and compile time constant. See 10 Example of Enum in Java for more examples on how to use Enum in Java.
What is the variable argument method in Java
As the name suggests variable argument method are Java method which can accept a variable number of arguments e.g. you can call the method by passing either 1 argument or two arguments or even zero arguments. The main method in Java 5 is retrofitted as a varargs method and now its String[] argument is converted to variable arguments. You can implement the varargs method even prior to Java 5 either by using an anonymous array or using Collection classes e.g. List or Set. Variable argument methods are also called as varargs and they are denoted by three ...(dot) after there name. See What is the varargs method in Java and How to use it for more details on variable arguments.
More About :
Java 1.5 version also introduced the following features :
1) Introduction of Java concurrency utilities e.g. CountDownLatch
2) Java ThreadPool Framework e.g. Executor
3) Concurrent Collection classes from java.util.concurrent package e.g. ConcurrentHashMap and CopyOnWriteArrayList, BlockingQueue etc
4) Changes on Java memory model and happens before relationship