jls

What is a capture conversion in Java and can anyone give me examples?

廉价感情. 提交于 2019-11-26 14:03:31
问题 I've noticed JLS talks of 5.1.10 Capture Conversion, but I fail to understand what they are. Can anyone explain them to me/give examples? 回答1: Capture conversion was designed to make wildcards (in generics), ? useful. Suppose we have the following class: public interface Test<T> { public void shout(T whatever); public T repeatPreviousShout(); } and somewhere on our code we have, public static void instantTest(Test<?> test) { System.out.println(test.repeatPreviousShout()); } Because test is

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

为君一笑 提交于 2019-11-26 06:45:56
问题 System.out.println(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE); is true. I understand that integer in Java is 32 bit and can\'t go above 2 31 -1, but I can\'t understand why adding 1 to its MAX_VALUE results in MIN_VALUE and not in some kind of exception. Not mentioning something like transparent conversion to a bigger type, like Ruby does. Is this behavior specified somewhere? Can I rely on it? 回答1: Because the integer overflows. When it overflows, the next value is Integer.MIN_VALUE .

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

烂漫一生 提交于 2019-11-26 05:56:28
问题 I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test pass without any runtime errors? @Test public void test() { final Beta a = new Gama(); a.number = \"its a string\"; ((Alpha) a).number = 13; ((Gama) a).number = 42; assertEquals(\"its a string\", a.number); assertEquals(13, ((Alpha) a).number); assertEquals(42,

How to create a class literal of a known type: Class<List<String>>

▼魔方 西西 提交于 2019-11-26 05:29:12
问题 Take the following: public Class<List<String>> getObjectType() { // what can I return here? } What class literal expression can I return from this method which will satisfy the generics and compile? List.class won\'t compile, and neither will List.<String>class . If you\'re wondering \"why\", I\'m writing an implementation of Spring\'s FactoryBean<List<String>> , which requires me to implement Class<List<String>> getObjectType() . However, this is not a Spring question. edit: My plaintive

Lambda expression and method overloading doubts

橙三吉。 提交于 2019-11-26 04:45:05
问题 OK, so method overloading is-a-bad-thing™. Now that this has been settled, let\'s assume I actually want to overload a method like this: static void run(Consumer<Integer> consumer) { System.out.println(\"consumer\"); } static void run(Function<Integer, Integer> function) { System.out.println(\"function\"); } In Java 7, I could call them easily with non-ambiguous anonymous classes as arguments: run(new Consumer<Integer>() { public void accept(Integer integer) {} }); run(new Function<Integer,

Why no static methods in Interfaces, but static fields and inner classes OK? [pre-Java8] [duplicate]

我的未来我决定 提交于 2019-11-26 04:38:03
问题 This question already has an answer here: Why can't I define a static method in a Java interface? 24 answers There have been a few questions asked here about why you can\'t define static methods within interfaces, but none of them address a basic inconsistency: why can you define static fields and static inner types within an interface, but not static methods? Static inner types perhaps aren\'t a fair comparison, since that\'s just syntactic sugar that generates a new class, but why fields