Eclipse的Java编译器只是程序所在的同一个核心的包装器javac,还是一个单独的编译器?如果是后者,为什么他们会重新发明轮子?
Comparable合同规定e.compareTo(null)必须抛出NullPointerException.
来自API:
请注意,这
null不是任何类的实例,并且e.compareTo(null)应该抛出一个NullPointerException偶数e.equals(null)返回false.
另一方面,ComparatorAPI没有提及比较时需要发生的事情null.考虑以下尝试采用a的泛型方法Comparable,并Comparator为其null作为最小元素返回.
static <T extends Comparable<? super T>> Comparator<T> nullComparableComparator() {
return new Comparator<T>() {
@Override public int compare(T el1, T el2) {
return
el1 == null ? -1 :
el2 == null ? +1 :
el1.compareTo(el2);
}
};
}
Run Code Online (Sandbox Code Playgroud)
这允许我们执行以下操作:
List<Integer> numbers = new ArrayList<Integer>(
Arrays.asList(3, 2, 1, null, …Run Code Online (Sandbox Code Playgroud) 还有其他一些SO问题正在讨论使用Eclipse编译器编译OK的泛型而不是javac(即Java:Generics在Eclipse中处理差异,javac和Generics编译并在Eclipse中运行,但不在javac中编译) - 但这看起来很像就像一个略有不同的人.
我有一enum节课:
public class LogEvent {
public enum Type {
// ... values here ...
}
...
}
Run Code Online (Sandbox Code Playgroud)
我有另一个类,其方法可以接收来自Enum以下类型的任意对象:
@Override public <E extends Enum<E>> void postEvent(
Context context, E code, Object additionalData)
{
if (code instanceof LogEvent.Type)
{
LogEvent.Type scode = (LogEvent.Type)code;
...
Run Code Online (Sandbox Code Playgroud)
这在Eclipse中工作得很好,但是当我做一个干净的构建时ant,我得到一对错误,一个instanceof在线上,另一个在转换线上:
443: inconvertible types
[javac] found : E
[javac] required: mypackage.LogEvent.Type
[javac] if (code instanceof LogEvent.Type)
[javac] ^
445: inconvertible types
[javac] found …Run Code Online (Sandbox Code Playgroud) 以下代码与Eclipse完美编译,但无法使用javac进行编译:
public class HowBizarre {
public static <P extends Number, T extends P> void doIt(P value) {
}
public static void main(String[] args) {
doIt(null);
}
}
Run Code Online (Sandbox Code Playgroud)
我简化了代码,所以现在根本不使用T. 不过,我没有看到错误的原因.由于某种原因,javac决定T代表Object,然后抱怨Object不符合T的界限(这是真的):
HowBizarre.java:6:不兼容的类型; 推断类型参数java.lang.Number,java.lang.Object不符合类型变量的范围P(T)
发现:
<P,T>无效要求:无效
Run Code Online (Sandbox Code Playgroud)doIt(null); ^
请注意,如果我将null参数替换为非null值,则编译正常.
哪个编译器行为正确,为什么?这是其中之一的错误吗?
这么奇怪!请先查看代码:
public class A {}
public class B extends A {}
public class C extends A {}
public class TestMain {
public <T extends A> void test(T a, T b) {}
public <T extends A> void test(List<T> a, List<T> b) {}
public void test1(List<? extends A> a, List<? extends A> b) {}
public static void main(String[] args) {
new TestMain().test(new B(), new C());
new TestMain().test(new ArrayList<C>(), new ArrayList<C>());
new TestMain().test(new ArrayList<B>(), new ArrayList<C>());
new TestMain().test1(new ArrayList<B>(), new ArrayList<C>());
}
} …Run Code Online (Sandbox Code Playgroud) 对不起,标题含糊不清.我有这段代码编译Eclipse Juno(4.2)但不是javac(1.7.0_09):
package test;
public final class Test {
public static class N<T extends N<T>> {}
public static class R<T extends N<T>> {
public T o;
}
public <T extends N<T>> void p(final T n) {}
public void v(final R<?> r) {
p(r.o); // <-- javac fails on this line
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
Test.java:13: error: method p in class Test cannot be applied to given types;
p(r.o);
^
required: T
found: N<CAP#1>
reason: inferred type does not conform to declared … 我目前正在使用Sierra和Bates学习指南学习SCJP认证,并且在许多自我测试(模拟考试问题)中我一直遇到同样的问题 - 我无法判断特定错误是否会在运行时(一个异常)或编译时(编译错误).我知道这是一个模糊的问题而且可能无法回答但是,如何在编译或运行时发现错误?你能给我发一些可以帮助我的网站链接吗?
method(1); // This works -
void method(int... x) { }
void method(int x) { } // - this method is called
Run Code Online (Sandbox Code Playgroud)
如果我向第二个方法添加 varargs 参数,则会收到“对方法的引用不明确”编译错误:
method(1); // This now fails
void method(int... x) { }
void method(int x, String... y) { } // adding String... y causes a problem.
Run Code Online (Sandbox Code Playgroud)
由于 String...y 参数可以留为“空白”,为什么 Java 仍然不选择该方法?谢谢,如果有关于SO的紧密匹配的解释,我们深表歉意;我确实找过一个。
我只是对泛型有一个普遍的问题.我已经使用它们多年了,并且听说它们不完美,因为它们在编译时被剥离了它们的类型(对吗?).我很难找到导致它们失败的特定代码的示例或解释.任何人都可以提供代码示例和/或解释吗?
java ×9
generics ×6
eclipse ×3
javac ×3
compilation ×2
null ×2
collections ×1
comparable ×1
comparator ×1
exception ×1
scjp ×1