Javatpoint Logo
Javatpoint Logo

Generic Code in Java

Generic code in Java refers to code that is created to be reused with various data types. Generics were first made available in Java 5 and have subsequently evolved into a key component of the language. They make it possible to write reusable code that can operate on a range of data types without compromising type safety.

Java programming was formerly developed using object-oriented concepts, where objects were passed around and their types were validated at runtime. This was before generics were introduced. This method had some drawbacks, such as low speed because runtime type checking was required, and a lack of compile-time type safety.

Since generics were introduced, Java programmers are now able to write code that is type-safe at compile time, which allows the code to detect type issues before the programme is even run. This results in more dependable and resilient code.

When defining a class, method, or interface, a generic type argument can be specified by the programmer. Then, any data type can be represented by this type parameter within the code. Take the following code as an illustration:

The Box class in this code contains a single type argument T that can stand in for any data type. The getData function returns a value of type T, and the setData method accepts a parameter of type T as a parameter.

Any type of data can be stored in a Box object created with this code:

In this illustration, even though stringBox and intbox are both instances of the Box class, they each contain a distinct type of data. For stringBox and intBox, the type argument T is changed to a String and an Integer, respectively.

The ability to write reusable code is one of the key advantages of using generics in Java. Think of the Java standard library's Collections class as an illustration. For working with object collections like lists, sets, and maps, this class offers a broad variety of useful methods. Generics allow the Collections class to offer type-safe methods that can be used with any kind of collection:

The Collections in this illustration.Any type of List can be used with the getFirst method, which returns the list's first item. The getFirst method is aware that it should return a String because the names list is declared to be of type ListString>.

An application that asks the user for their name and greets them is shown here as an example:

Greeting.java

Output:

What is your name? John
Hello, John!

Let's go through the code step by step:

In order to get started, we import the Scanner class from the java.util package. With the help of this class, we can read user input.

We establish the greeting class.

The main() method is defined inside the Greeting class. Our program's entry point is located here, and when the programme is run, the code contained in this method will be put to use.

In order to read input from the user, we build a new Scanner object called input.

The user is prompted for their name using the System.out.print function.

The input.nextLine method is used to read the user's input, which is then saved in a variable called name.

The System.out.println method is used to address the user by name.

Using the user's input, we ask them for their name and then address them by name in this example. This programme is straightforward, but it illustrates some fundamental ideas in Java programming, including taking user input, saving information in variables, and utilising methods to send information to the terminal.

In conclusion, Java's generics feature is a potent tool that lets programmers write reusable code that can operate on a range of data types while still preserving type safety at compile time. Java programmers can make use of the various utility classes and methods offered by the Java standard library and produce more durable, dependable code by using generics.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA