Thursday, April 13, 2023

Autoboxing, Enum, Generics, Varargs methods - Java 5 Features Quick Overview

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.


1. 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.




2. What are 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.


3. What is Enum in Java?

Enum is a way to represent 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 fixed number of things, It is much more volatile can 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 method in Java, Enum can implement 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.


4. 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 varargs method and now its String[] argument is converted to variable arguments. You can implement 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.

Java 5 features overview Generics Enum Varargs and Autoboxing

These were a quick overview of some of my favorite Java 5 features. I have included Autoboxing, Varargs, Enum, and Generics only but there are many more features in Java 5 which is worth looking e.g. static imports, regular expression and Java Concurrency API which introduces some really nice utility like CountDownLatch and CyclicBarrier, explore them to learn more about Java 5 advantages.


Related Java Enum Tutorials for further reading :
  1. Top 15 Java Enum Interview Questions with Answers (see here)
  2. Difference between RegularEnumSet and JumboEnumSet in Java (read here)
  3. String to Enum in Java with Example (check here)
  4. Can we use Enum in Switch Statement in Java (Yes)
  5. How to use valueOf method of Enum (check here)
  6. What Every Java Programmer Should know about Enum (click here)
  7. 10 points about Enum in Java (see here)
  8. Java tip to convert  Enum to String in Java (see tip)
  9. Can Enum have Constructor in Java (learn here)
  10. How to loop over Enum constants in Java (example)
  11. Learn how to use Enum in Java with a Simple Example (check here)
  12. 10 Courses to learn Java for Beginners (courses)
Thanks for reading this article so far. If you find this article useful then please share it on Facebook, Twitter, and LinkedIn. 

1 comment:

  1. I think Java 1.5 version also introduced 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

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.