@SuppressWarnings("unchecked")
public static Map<MyObject,MyObject> testObjectUnchecked(Object in) {
return (Map<MyObject,MyObject>) in; // warning
}
public static Map<MyObject,MyObject> testObjectChecked(Object in) {
return testMap(in); // no warning
}
public static Map<MyObject,MyObject> testMap(Object in) {
if (in instanceof Map) {
Map<?,?> m1 = (Map<?,?>)in;
Map<MyObject,MyObject> m2 = new HashMap<>();
for (Entry<?,?> e : m1.entrySet()) {
m2.put((MyObject)e.getKey(),(MyObject)e.getValue()); // no warning
}
return m2;
}
return Collections.<MyObject,MyObject>emptyMap();
}
System.out.println(test2(a2)); System.out.println(test2(a2).get(0).substring(0, 1));
On Wednesday, April 19, 2023 at 1:43:39 PM UTC-4, Arne Vajhøj wrote:
System.out.println(test2(a2));
System.out.println(test2(a2).get(0).substring(0, 1));
So no warnings about casting one thing to another possibly compatible
thing (Object to String) because the cast immediately throws
exception if Object is not a String, but using containers (List, Map)
it warns because the cast hides the error; it actually allows
incompatible casting (List<Integer> to List<String>) and can print
the resulting object, and crashes when you try to return an element
into a typed variable. Of course we don't need to worry about
immediate casting exceptions because it's Runtime. If you take a List<Integer> and cast it to a List<String>, you can call get(idx)
and return the value into a variable declared as String and crash
(but it compiles). If you return the value into a variable declared
as Object, it works, so when you called .get(0) it didn't care that
it wasn't a String until it tried to process the .substring. As long
as you treat that result as Object, it's fine. System.out.println(test2(a2).get(0).getClass()); // List<String>
doesn't care that element is Integer
On 4/19/2023 2:19 PM, e.d.pro...@gmail.com wrote:
On Wednesday, April 19, 2023 at 1:43:39 PM UTC-4, Arne Vajhøj wrote:Yes. That is how I see it all work.
So no warnings about casting one thing to another possibly compatible
thing (Object to String) because the cast immediately throws
exception if Object is not a String, but using containers (List, Map)
it warns because the cast hides the error; it actually allows
incompatible casting (List<Integer> to List<String>) and can print
the resulting object, and crashes when you try to return an element
into a typed variable. Of course we don't need to worry about
immediate casting exceptions because it's Runtime. If you take a
List<Integer> and cast it to a List<String>, you can call get(idx)
and return the value into a variable declared as String and crash
(but it compiles). If you return the value into a variable declared
as Object, it works, so when you called .get(0) it didn't care that
it wasn't a String until it tried to process the .substring. As long
as you treat that result as Object, it's
fine. System.out.println(test2(a2).get(0).getClass()); //
List<String>
doesn't care that element is Integer
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 458 |
Nodes: | 16 (2 / 14) |
Uptime: | 141:19:38 |
Calls: | 9,338 |
Calls today: | 9 |
Files: | 13,537 |
Messages: | 6,083,120 |
Posted today: | 1 |