Java Questions
Java Questions
interfaces, and methods with placeholders for types. These placeholders are
specified when you instantiate the class or call the method, allowing for type-safe
data structures and algorithms without sacrificing performance. Generics provide the
compile-time type safety. Let's look at how to write a generic method in Java that
public static <T> void swap(T[] array, int index1, int index2) {
if (index1 >= 0 && index1 < array.length && index2 >= 0 && index2 <
array.length) {
T temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
} else {
}
public static void main(String[] args) {
// Integer array
swap(intArray, 1, 3);
// String array
swap(strArray, 0, 4);
Explanation:
1. Generic Method Definition: The method swap is defined with the generic type
<T>. This means that the method can work with any type T.
2. Parameters:
● T[] array: The array of elements of type T.
● int index1: The first index of the element to swap.
● int index2: The second index of the element to swap.
3. Swap Logic: The elements at the specified indices are swapped using a
temporary variable.
4. Bounds Checking: The method checks if the provided indices are within the
bounds of the array to avoid IndexOutOfBoundsException.
5. Main Method: The main method demonstrates how to use the swap method
with different types of arrays (Integer and String).
This generic method can be used to swap elements in any array, regardless of the
type of the elements, as long as the type is specified when calling the method.
In Java Generics, a bounded type parameter restricts the types that can be used as
arguments for a generic type. This allows you to enforce certain constraints on the
types that can be passed to a generic class, method, or interface.
● <T extends Number> means that T can be any type that is a subclass of
Number.
This is useful when you want to ensure that the type parameter implements certain
methods or behaviors.
// Float array
Float[] floatArray = {5.5f, 3.3f, 8.8f, 1.1f, 9.9f};
System.out.println("Before sorting Float array: " + java.util.Arrays.toString(floatArray));
bubbleSort(floatArray);
System.out.println("After sorting Float array: " + java.util.Arrays.toString(floatArray));
// Double array
Double[] doubleArray = {5.5, 3.3, 8.8, 1.1, 9.9};
System.out.println("Before sorting Double array: " +
java.util.Arrays.toString(doubleArray));
bubbleSort(doubleArray);
System.out.println("After sorting Double array: " +
java.util.Arrays.toString(doubleArray));
}
}
Types(Classes).
class MyList {
private List<String> values;
hard-coding String class as the only type the class can work with, we make the class type a
Let’s replace String with T and create a new class. Now the MyListGeneric class can be used to
class MyListGeneric<T> {
private List<T> values;
T get(int index) {
return values.get(index);
}
}
arguments for a generic type. This allows you to enforce certain constraints on the
types that can be passed to a generic class, method, or interface, ensuring that the
java
Copy code
Multiple Bounds
Java also supports multiple bounds for type parameters. A type parameter can have
multiple bounds by specifying an upper bound (a class) and one or more interfaces.
java
Copy code
{ // class body }
In this scenario, T can be any type that extends SomeClass and implements both
import java.util.ArrayList;
import java.util.List;
interface Printable {
void print();
this.length = length;
this.width = width;
@Override
double area() {
@Override
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
@Override
public static <T extends Shape & Printable> void printShapes(List<T> shapes) {
shape.print();
circles.add(new Circle(1));
circles.add(new Circle(2));
System.out.println("Rectangles:");
printShapes(rectangles);
System.out.println("Circles:");
printShapes(circles);
Explanation:
1. Interfaces and Classes:
● Printable is an interface with a print method.
● Shape is an abstract class with an area method.
● Rectangle and Circle are concrete classes that extend Shape and
implement Printable.
2. Generic Method with Multiple Bounds:
● The method printShapes uses a bounded type parameter <T extends
Shape & Printable>. This ensures that the method can only accept
types that are subclasses of Shape and implement Printable.
3. Main Method:
● We create lists of Rectangle and Circle objects.
● We call the printShapes method with these lists, and it prints
information about the shapes.
constraints on generic types, ensuring that they meet specific requirements and can