jls

Which Java Errors and Exceptions may (not) be thrown by “empty statements”?

一世执手 提交于 2019-12-01 08:19:06
问题 Which subclass(es) of java.lang.Throwable may be thrown by an empty statement? By the phrase "an empty statement", I'm referring to the "nothing", the "semi-colon", and the "semi-colons": // .... A(); B(); C(); try { // nothing } catch (java.lang.Throwable e) { // which Throwable subclass might we see? } D(); E(); F(); try { ; // semi-colon } catch (java.lang.Throwable e) { // which Throwable subclass might we see? } G(); H(); I(); try { ; ; ;; ;;;;; ; ; ;;; ;; ;; ;; ;; ; ;; ; ;; // ... semi

How does the JLS grammar match simple field accesses (obj.f)?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 07:12:57
I was looking at the JLS Chapter 19 grammar trying to figure out how a simple field access is parsed: obj.field It looks to me like the first variant of the FieldAccess production is probably involved FieldAccess : Primary . Identifier super . Identifier TypeName . super . Identifier and that Primary should then be involved in parsing the obj part. Primary doesn't seem to be involved with parsing a simple reference like ExpressionName . That seems to be reached via PostfixExpression . PostfixExpression : Primary ExpressionName PostIncrementExpression PostDecrementExpression and, AFAICT,

Class loading vs class initialization

 ̄綄美尐妖づ 提交于 2019-12-01 05:46:17
问题 I always thought that class loading and class initialization are synonymous and usually happens on demand when the class being initialized/loaded is used in some way or the other for the first time. But now I know from this answer on SO regarding the behavior of final static fileds that holds compile time constants that my belief is wrong. Please note the following which makes it quite clear that class loading and initialization are two different mechanism. As a side point, please note the

Order of automatically imported packages and ambiguity

≡放荡痞女 提交于 2019-12-01 04:11:13
问题 JLS: Chapter 7. Packages: A package consists of a number of compilation units (§7.3). A compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang . Lets assume the following code: package com.example.p1; public class MyClass { } package com.example; public class MyClass { } package com.example; public class String { } package com.example; import com.example.p1.*; public

Why can't an inner class use static initializer?

爷,独闯天下 提交于 2019-12-01 03:43:59
问题 Quoth JLS #8.1.3: Inner classes may not declare static initializers (§8.7)...... This is demonstrated as such: class A { class B { static { // Compile-time Error: Cannot define static initializer in inner type A.B System.out.println("Class is initializing..."); } } } Now since Java's inner (non-static) classes are loaded by class loaders just like every other class, why can't we have static initializers for them? What's the reason behind this limitation? 回答1: I think it is because the Inner

mysql 查询补空行,提供给报表输出

╄→尐↘猪︶ㄣ 提交于 2019-11-30 19:57:45
案例为20行一页。 创建存储过程进行处理: BEGIN # 已查询记录数、每页记录数、需增加记录数 DECLARE jls,myjls,zjjls int; # DROP TEMPORARY TABLE if EXISTS t1; #将查询后的数据放入临时表 CREATE TEMPORARY TABLE t1 SELECT zd1,zd2 FROM `table1` LIMIT 35; SET myjls=20; SELECT COUNT(1) INTO jls FROM t1; #需增加的记录数计算 SET zjjls=jls mod myjls; if zjjls<>0 THEN SET zjjls=myjls-zjjls; end if; #循环补空行 while zjjls<>0 DO INSERT into t1 SELECT null,null; #SELECT zjjls; SET zjjls=zjjls-1; END while; #输出 SELECT * from t1; END 来源: CSDN 作者: andywan 链接: https://blog.csdn.net/andywan/article/details/52387364

What are the formal conditions for a wildcard parameter in a Java generic type to be within its bounds?

点点圈 提交于 2019-11-30 18:46:46
With parameterized types in Java, how do the rules that check if a parameter is within its bound work exactly for wildcards? Given a class like this: class Foo<T extends Number> {} Experimenting with what the compiler accepts learns that: A ? extends wildcard using an unrelated interface type is allowed: Foo<? extends Runnable> is valid A ? extends wildcard using an unrelated class type is not allowed: Foo<? extends Thread> is invalid. That makes sense because no type can be a subtype of both Number and Thread In a ? super wildcard, the lower bound in the wildcard must be subtype of the bound

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

不羁的心 提交于 2019-11-30 10:19:54
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(); } public interface CD extends C,D { } However, in that case the return types were genuinely

changing final variables through reflection, why difference between static and non-static final variable

独自空忆成欢 提交于 2019-11-30 09:08:56
Please refer to the below code. When I run the code, I am able to change the value of a final non-static variable. But if I try to change the value of a final static variable then it throws java.lang.IllegalAccessException . My question is why doesn't it throw an exception in case of non-static final variable also or vice versa. Why the difference? import java.lang.reflect.Field; import java.util.Random; public class FinalReflection { final static int stmark = computeRandom(); final int inmark = computeRandom(); public static void main(String[] args) throws SecurityException,

Java: overloaded method resolution and varargs — confusing example

左心房为你撑大大i 提交于 2019-11-30 08:37:09
Just when I thought I understood JLS15.12 as it applied to varargs, here's this example: package com.example.test.reflect; public class MethodResolutionTest2 { public int compute(Object obj1, Object obj2) { return 42; } public int compute(String s, Object... objects) { return 43; } public static void main(String[] args) { MethodResolutionTest2 mrt2 = new MethodResolutionTest2(); System.out.println(mrt2.compute("hi", mrt2)); System.out.println(mrt2.compute("hi", new Object[]{mrt2})); System.out.println(mrt2.compute("hi", new Object[]{mrt2, mrt2, mrt2})); } } which prints out 42 43 43 I