-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsMethod2Example.java
More file actions
25 lines (21 loc) · 996 Bytes
/
GenericsMethod2Example.java
File metadata and controls
25 lines (21 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class GenericsMethod2Example {
// Generic method that accepts any type of argument and prints it
public static <T> void printGeneric(T value) {
System.out.println("Value: " + value);
}
// Generic method that returns two given parameters as a concatenated string
public static <T, U> String concatenate(T first, U second) {
return first.toString() + " " + second.toString();
}
public static void main(String[] args) {
// Calling generic method with different data types
printGeneric(10); // Integer
printGeneric("Hello"); // String
printGeneric(3.14); // Double
printGeneric(true); // Boolean
// Calling the concatenate method
System.out.println(concatenate("Hello", 100)); // Output: Hello 100
System.out.println(concatenate(42, 3.14)); // Output: 42 3.14
System.out.println(concatenate(true, "World")); // Output: true World
}
}