jls

Why does returning null for a primitive work in this case?

。_饼干妹妹 提交于 2019-12-23 06:49:39
问题 This ugly piece of code does compile but throws NPE if s == null public static boolean isNullOrEmpty(String s) { return s != null ? s.isEmpty() : null; } while this does not (as expected): public static boolean isNullOrEmpty(String s) { if(s != null) return s.isEmpty(); else return null; } I know both of them are plainly wrong, but as I found the first piece of code in our sources, I was quite surprised it did compile. Edit: Here's the relevant part of the JLS from Java 7. I guessed the first

Is the JDK documentation part of the language specification?

六眼飞鱼酱① 提交于 2019-12-22 09:19:02
问题 There is only one official Java Language Specification and all Java implementations must comply with it. How about the API documentation: do all Java implementations need to comply with this version or could an implementation define the behaviour of some methods and classes differently, as long as it's compatible with the JLS? Let's throw a concrete example: could a Java implementation include a HashMap that doesn't accept null elements? 回答1: I believe, that, yes it is. Upon closer

Annotation attribute must be a class literal? Why? Constants should be fine too

柔情痞子 提交于 2019-12-21 03:58:14
问题 Can someone explain why String and Class annotation parameters are expected differently? Why does the compiler require literals for Classes, wherby accepting constants for Strings as well? Working example with Spring's @RequestMapping: public class MyController { public static final String REQUEST_MAPPING = "/index.html"; @RequestMapping(MyController.REQUEST_MAPPING) // ALL OK! ... } WTF example with TestNG's @Test: public class MyControllerTest { public static final Class TEST_EXCEPTION =

Why can't a class extend an enum?

大憨熊 提交于 2019-12-19 12:50:45
问题 I am wondering why in the Java language a class cannot extend an enum . I'm not talking about an enum extending an enum (which can't be done, since java doesn't have multiple inheritance, and that enum s implicitly extend java.lang.Enum ), but a class that extends an enum in order to only add extra methods, not extra enumeration values. Something like: enum MyEnum { ASD(5), QWE(3), ZXC(7); private int number; private asd(int number) { this.number=number; } public int myMethod() { return this

Restrictions on Java intersection types with interfaces classes and type variables

拟墨画扇 提交于 2019-12-19 09:53:10
问题 Today I tried to write a class with a generic method that uses intersection types and got confused by the different error messages depending on the intersected types. Let's assume that we have an interface and a class and define generic methods in a generic interface: class ClassType { } interface InterfaceType { } interface I<T> { public <X extends InterfaceType & InterfaceType> void foo(); public <X extends ClassType & ClassType> void foo1(); public <X extends ClassType & InterfaceType>

In Java, why can't I write i++++ or (i++)++?

。_饼干妹妹 提交于 2019-12-19 02:06:09
问题 When I try to write a postfix/prefix in/decrement, followed by a post/prefix in/decrement, I get the following error: Invalid argument to operation ++/-- . But, according to JLS: PostIncrementExpression: PostfixExpression ++ and PostfixExpression: Primary ExpressionName PostIncrementExpression PostDecrementExpression so writing: PostfixExpression ++ ++ should be possible... Any thoughts? 回答1: Note that the raw grammar lacks any semantics. It's just syntax, and not every syntactically valid

Why usage of prefix incrementation is considered better than postfix incrementation in standard for construction

怎甘沉沦 提交于 2019-12-18 20:02:09
问题 I recently installed Checkstyle plugin for Eclipse and personally think that it is awesome. But one of the warnings it gives me is a bit obscure. The exact warning is "Using ++ is not allowed". It is about postfix ++ in some row like for(int i = 0; i < SOMETHING; i++) Ok, I 'm aware that foreach is the better construction for iteration, but it can't be applied everywhere, sometimes old-school ++ is the only alternative. When I change the row to for(int i = 0; i < SOMETHING; ++i) the warning

Java - implementing multiple interfaces with same method and different return types

依然范特西╮ 提交于 2019-12-18 13:37:23
问题 Consider the following code: public interface A { public A another(); } public interface B { public B another(); } public interface AB extends A,B { public AB another(); } This leads to a compile error on AB : types B and A are incompatible; both define another(), but with unrelated return types I've seen this SO question, and follow the incompatibility example in the the accepted answer - i.e. public interface C { public void doSomething(); } public interface D { public boolean doSomething()

Is “public static final” redundant for a constant in a Java interface?

允我心安 提交于 2019-12-18 12:07:19
问题 This code: interface Config { int MAX_CONN = 20; } compiled and worked as I expected. It looks like this is the same as: interface Config { public static final int MAX_CONN = 20; } Is "public static final" redundant for a constant in a Java interface? Is this true for Java 1.1, 1.2, 1.3, 1.4,..., 1.8 or did it change in a Java release? 回答1: Variables declared in Interface are implicitly public static final . This is what JLS 9.3 says : Every field declaration in the body of an interface is