Questions tagged [jls]

The Java Language Specification is the definitive technical reference of the Java programming language.

The Java Language Specification is the definitive technical reference of the Java programming language. It describes the language in full detail and is usually the final source of answers for questions about Java.

Questions with this tag often expect an answer that is based on the JLS and is not specific to any compiler or implementation. If possible, the relevant section of the JLS should be provided.

369 questions
35
votes
3 answers

Is it true that every inner class requires an enclosing instance?

The term inner class is conventionally taken to mean "a nested class which requires an enclosing instance". However, the JLS states as follows: 8.1.3. Inner Classes and Enclosing Instances [...] Inner classes include local (§14.3), anonymous…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
33
votes
5 answers

Java in operator

For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this if (value in (a, b, c)) { } else if (value in (d, e)) { } ...would…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
33
votes
2 answers

Is `1/0` a constant expression in Java?

As far as I understand the Java 8 JLS the expression (1/0) is considered a constant expression, but when I try to compile the following program with OpenJDK 8 I get an error public class Switch { public static void main(String[] args) { …
ReyCharles
  • 1,772
  • 10
  • 30
32
votes
1 answer

Java 8 Consumer/Function Lambda Ambiguity

I have an overloaded method that takes a Consumer and a Function object respectively and returns a generic type that matches the corresponding Consumer/Function. I thought this would be fine, but when I try to call either method with a lambda…
johnlcox
  • 520
  • 6
  • 12
32
votes
3 answers

Return value of assignment operator in concurrent code

Given the following class: class Foo { public volatile int number; public int method1() { int ret = number = 1; return ret; } public int method2() { int ret = number = 2; return ret; } } and given multiple threads…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
30
votes
9 answers

Testing initialization safety of final fields

I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I'm writing. However, I am unable to get it to 'fail' based on my current code. Can someone tell me what I'm doing wrong, or if…
sma
  • 9,449
  • 8
  • 51
  • 80
30
votes
2 answers

Final fields initialization order

Here is some code that calls static method A.f() on class that is not initialized yet. Can someone explain behavior of this code in terms of JLS? class A { final static Object b = new B(); final static int S1 = 1; final static Integer S2…
Sergey Alaev
  • 3,851
  • 2
  • 20
  • 35
29
votes
5 answers

What parts of the JLS justify being able to throw checked exceptions as if they were unchecked?

I have recently discovered and blogged about the fact that it is possible to sneak a checked exception through the javac compiler and throw it where it mustn't be thrown. This compiles and runs in Java 6 and 7, throwing a SQLException without throws…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
25
votes
2 answers

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. void printType(T... args) { …
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
25
votes
2 answers

Is a write to a volatile a memory-barrier in Java

I recently heard in a talk that a write to a volatile triggers a memory-barrier for each variable that the thread has written to. Is that really correct? From the JLS, it seems that only the variable concerned gets flushed out, but not others. Does…
MKK
  • 670
  • 5
  • 13
22
votes
2 answers

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

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?
John Assymptoth
  • 8,227
  • 12
  • 49
  • 68
21
votes
3 answers

Why Java methods with varargs identified as transient?

I was playing with Java Reflection API and observed that methods with variadic argument list become transient. Why is that and what does transient keyword mean in this context? From Java Glossary, transient: A keyword in the Java programming…
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
20
votes
2 answers

Java casting: is the compiler wrong, or is the language spec wrong, or am I wrong?

I have been reading the Java Language Spec, 3rd edition, and have found what I think is a discrepancy between the spec and the javac compiler implementation. The same discrepancies exist in the Eclipse compiler. Section 15.16 talks about cast…
davmac
  • 20,150
  • 1
  • 40
  • 68
19
votes
3 answers

When will the Java Language Specification, 4th edition be available?

Now that the JDK 7 developer preview is out, one might think that it's time for a new JLS. After all, there have been changes to the language, albeit small ones. I haven't found anything yet. When will a new JLS will be available, and from whence…
Ingo
  • 36,037
  • 5
  • 53
  • 100
1
2
3
24 25