raw-types

Why is Class<?> preferred to Class

喜夏-厌秋 提交于 2019-11-30 01:56:19
If I declare a Class as a field: Class fooClass; Eclipse gives me the warning: Class is a raw type. References to generic type Class should be parametrized What does this mean in practice? and why am I urged to do it? If I ask Eclipse for a "quick fix" it gives me: Class<?> fooClass; which doesn't seem to add much value but no longer gives a warning. EDIT: Why is Class generic? Could you please give an example of parameterization, i.e. could there be a valid use of something other than <?> ? EDIT: WOW! I had not realized the depths of this. I have also watched the Java Puzzler and it's

Why does the Java Compiler complain on using foreach with a raw type? [duplicate]

会有一股神秘感。 提交于 2019-11-29 13:41:44
This question already has an answer here: What is a raw type and why shouldn't we use it? 15 answers I got a strange compiler error when using generics within a for-each loop in Java. Is this a Java compiler bug, or am I really missing something here? Here is my whole class: public class Generics<T extends Object> { public Generics(T myObject){ // I didn't really need myObject } public List<String> getList(){ List<String> list = new ArrayList<String>(); list.add("w00t StackOverflow"); return list; } public static void main(String...a){ Generics generics = new Generics(new Object()); for(String

why I can set primitive types to null in ternary operations

我怕爱的太早我们不能终老 提交于 2019-11-29 06:12:35
I always thought that primitive types in Java cannot be null , as it is a compile time error if i attempt to do something like this: int test = null; However in a ternary operation, it seems to be allowed: int test = something != 0 ? 5 : null; Isn't a ternary operation just short for (in this case): int test; if (something != 0){ test = 5; } else { test = null } which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException due to autoboxing. So why the java-compiler doesn't fetch nonsense like this? What happens is that the Java compiler first

Why is Class<?> preferred to Class

有些话、适合烂在心里 提交于 2019-11-28 22:17:27
问题 If I declare a Class as a field: Class fooClass; Eclipse gives me the warning: Class is a raw type. References to generic type Class should be parametrized What does this mean in practice? and why am I urged to do it? If I ask Eclipse for a "quick fix" it gives me: Class<?> fooClass; which doesn't seem to add much value but no longer gives a warning. EDIT: Why is Class generic? Could you please give an example of parameterization, i.e. could there be a valid use of something other than <?> ?

Java 6: Unsupported @SuppressWarnings(“rawtypes”) warning

我的梦境 提交于 2019-11-28 06:43:47
I moved to a new machine which has the latest Sun's Java compiler and noticed some warnings in the existing Java 6 code. The Eclipse IDE, suggested that I annotate the assignment with: @SuppressWarnings("rawtypes") For example: class Foo<T> { ... } ... @SuppressWarnings("rawtypes") Foo foo = new Foo(); When I moved back to the machine with the older compiler (JDK 1.6.0_20), I have noticed that this older compiler now warns about the suppression of "rawtypes" warnings, claiming that this suppression is unsupported and proposing to replace it with @SuppressWarnings("unchecked"). Also, there were

Why calling method with generic return on a generic class is considered unsafe by javac?

空扰寡人 提交于 2019-11-28 00:42:57
Consider the following code: public class Main { public static class NormalClass { public Class<Integer> method() { return Integer.class; } } public static class GenericClass<T> { public Class<Integer> method() { return Integer.class; } } public static void main(String... args) { NormalClass safeInstance = new NormalClass(); Class<Integer> safeValue = safeInstance.method(); GenericClass unsafeInstance = new GenericClass(); Class<Integer> unsafeValue = unsafeInstance.method(); } } If I compile it with: $ javac -Xlint:unchecked Main.java It returns: Main.java:16: warning: [unchecked] unchecked

why I can set primitive types to null in ternary operations

做~自己de王妃 提交于 2019-11-27 23:42:23
问题 I always thought that primitive types in Java cannot be null , as it is a compile time error if i attempt to do something like this: int test = null; However in a ternary operation, it seems to be allowed: int test = something != 0 ? 5 : null; Isn't a ternary operation just short for (in this case): int test; if (something != 0){ test = 5; } else { test = null } which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException due to autoboxing.

Java 6: Unsupported @SuppressWarnings(“rawtypes”) warning

会有一股神秘感。 提交于 2019-11-27 01:33:57
问题 I moved to a new machine which has the latest Sun's Java compiler and noticed some warnings in the existing Java 6 code. The Eclipse IDE, suggested that I annotate the assignment with: @SuppressWarnings("rawtypes") For example: class Foo<T> { ... } ... @SuppressWarnings("rawtypes") Foo foo = new Foo(); When I moved back to the machine with the older compiler (JDK 1.6.0_20), I have noticed that this older compiler now warns about the suppression of "rawtypes" warnings, claiming that this

Cannot convert from List<List> to List<List<?>>

怎甘沉沦 提交于 2019-11-26 22:44:35
A raw list converts to List<?> just fine. Why can't a list of raw lists convert to a list of List<?> ? { // works List raw = null; List<?> wild = raw; } { // Type mismatch: cannot convert from List<List> to List<List<?>> List<List> raw = null; List<List<?>> wild = raw; } Backstory (to mitigate the xy problem ): An API I'm using returns List<JAXBElement> . I happen to know that it is always List<JAXBElement<String>> . I plan to loop and build my own List<String> , but I was trying to fix (but not suppress) the raw type compiler warning when I write List<JAXBElement> raw = api(); . I tried: List

Why calling method with generic return on a generic class is considered unsafe by javac?

百般思念 提交于 2019-11-26 21:47:16
问题 Consider the following code: public class Main { public static class NormalClass { public Class<Integer> method() { return Integer.class; } } public static class GenericClass<T> { public Class<Integer> method() { return Integer.class; } } public static void main(String... args) { NormalClass safeInstance = new NormalClass(); Class<Integer> safeValue = safeInstance.method(); GenericClass unsafeInstance = new GenericClass(); Class<Integer> unsafeValue = unsafeInstance.method(); } } If I compile