-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample4.java
More file actions
44 lines (36 loc) · 1.82 KB
/
Example4.java
File metadata and controls
44 lines (36 loc) · 1.82 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.tutorial.def;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class Example4 {
public static void main(String[] args) {
Map<Object, Object> map = new HashMap<>();
System.out.println(map instanceof Object);
System.out.println(map instanceof HashMap);
System.out.println(map instanceof Map);
SortedMap<Object, Object> map1 = new TreeMap<>();
System.out.println(map1 instanceof Map);
// Note that it is the actual type of the referenced object that is compared against
// - not the type of the variable referencing the object
Map<Object, Object> map2 = null;
//The Java instanceof operator always evaluates to false
// when a null variable is compared against any class or interface
System.out.println(map2 instanceof Map);
// Note that even if the type of map2 variable is Map,
// and the map2 variable is compared against the Map interface,
// the result of the comparison is still false.
// That is because it is not reference type that is compared against the target class or interface,
// but the actual type of the referenced object.
// instanceof with pattern matching - java 14 preview
Object obj = new String("Test instanceof operator patter matching");
// If the 'obj' variable references an object of type String,
// the str variable will be bound to that object - cast to a String.
if (obj instanceof String str){
System.out.println("instanceof operator with patter matching: " + str.substring(5, 15));
}
if(obj instanceof String str && str.startsWith("Test")){
System.out.println(str + " - starts with Test");
}
}
}