Questions tagged [jls]
The Java Language Specification is the definitive technical reference of the Java programming language.
347
questions
1
vote
1
answer
75
views
Does the semantics of java volatile guarantee that wrong results will not appear? (Two threads write first and then read)
volatile x=y=0
Thread1
x=1
r1=y
Thread2
y=1
r2=x
r1 and r2 are local variables
Question1:
Is the result of r1==r2==0 illegal and does not appear?
All statements are writing or reading volatile ...
0
votes
0
answers
16
views
Reduction rule for Type Compatibility Constraint when Generic types are involved in Java
Consider the following section from JLS(§18.2.2)
A constraint formula of the form ‹S → T› is reduced as follows:
...
Otherwise, if T is a parameterized type of the form G<T1, ..., Tn>, and ...
2
votes
1
answer
101
views
Conditions for Method Reference Expression to be "exact"
Consider the following article from the JLS (§15.13.1)
A method reference expression ending with Identifier is exact if it satisfies all of the following:
If the method reference expression has the ...
1
vote
1
answer
30
views
Reduction procedure for MethodReference Expressions
Consider the following article from JLS that describes the reduction procedure for the MethodReference expressions during Type Inference process-:
A constraint formula of the form ‹MethodReference → ...
5
votes
2
answers
71
views
Type Inference resolution procedure involving throws clause in Java
Consider the following article in JLS §18.1.3 - Bounds
Here when we try to identify the set of bounds on the inference variables - we have one of the following situations:
...
throws α: The ...
0
votes
0
answers
91
views
Shouldn’t this type inference fail to compile?
Consider the following snippet:
Stream.of(stringList, Arrays.asList(1))
.map(list -> list)
.reduce((list1, list2) -> {list1.addAll(list2); return list1;});
As far as I can tell, this ...
2
votes
0
answers
44
views
Why is this Java method reported as a bridge method?
Consider the following Java program:
import java.lang.reflect.Method;
public class BridgeTest {
public static class ClassA {
public void foo() {
}
}
protected static ...
0
votes
0
answers
22
views
Type Inference in Method Invocation Poly Expression within the Invocation Context with/without method chaining
I was going through this question - this triggered the following set of thoughts regarding the Type Inference:
class Song {
public Integer duration;
public String artist;
public String title;
// ...
0
votes
0
answers
42
views
When type arguments are supplied for the constructor - then Diamond Operator cannot be used and Type Arguments must be supplied? [duplicate]
Consider the following clause from the JLS (§15.9)
It is a compile-time error if a class instance creation expression provides type
arguments to a constructor but uses the diamond form for type ...
0
votes
0
answers
33
views
Binary names for the Type Variables - in classes/interfaces/method/constructor
Consider the following clause from the JLS §13.1
The binary name of a type variable declared by a generic class or interface (§8.1.2, §9.1.2) is the binary name of its immediately enclosing type, ...
1
vote
1
answer
56
views
Implicit constructor in case of anonymous class whose super class is an Inner Class
Consider the following article from the JLS: §15.9.5.1
When the anonymous class extends an inner class - then for the implicit constructor of the anonymous class - following is the rule regarding the ...
0
votes
1
answer
34
views
Places where an expression can occur as per the JLS?
Consider the Article §15.1 regarding places where the expression can occur:
An expression occurs in either:
• The declaration of some (class or interface) type that is being declared: in a
field ...
4
votes
0
answers
59
views
What subtyping/supertyping rule in the Java Language Specification allows this assignment?
Suppose I have this (contrived) situation:
public final Comparable<? super Number> frob() {
return x -> 0; // whatever
}
final Comparable<? super Integer> c = frob();
The JLS ...
0
votes
0
answers
15
views
Local class declaration and instantiation rules within static context in JLS [duplicate]
Consider the following rules from JLS regarding Determination of Enclosing Instances §:15.9.2:
If C is a local class, then:
– If C occurs in a static context, then i has no immediately enclosing ...
0
votes
1
answer
64
views
What is the formal definition of an unqualified identifier in Java?
In Java, what is the formal definition of an "unqualified identifier"?
The Java Language Specification mentions an unqualified identifier while defining what a type variable is, in section 4....
4
votes
0
answers
31
views
Meaning of the bound of the form `throws α` during Type Inference in Java
According to JLS (§18.1.3)"Bounds" : it specifies regarding the inference of throws α:
A bound of the form throws α is purely informational: it directs resolution to optimize the ...
-1
votes
1
answer
194
views
How is the scope of pattern variables resolved in java?
I was going through the pattern variables in java. While reading the JLS it mentioned several conditions regarding when a pattern variable is introduced.
It mentions -
The analysis relies on the ...
11
votes
2
answers
324
views
Is it legal to use `record` as a variable name even though it's a keyword?
This came as a surprise: I am able to declare a variable with the name record even though it now has become a keyword. Have a look at this:
public class Main {
static class Foo {
void ...
2
votes
0
answers
41
views
Overloaded methods in the class must have different Erasures - why only leftmost bound is considered to be the Erasure type?
Consider the following two method definitions:
static <T extends Do & Done>T whatToDo(T ele){return null;}
static <R extends Done & Do>R whatToDo(R ele){return null;}
Both will ...
-1
votes
1
answer
58
views
Why Variable-Parameterized variant of (cast) operator in Java is flagged as unchecked warning and not error?
Consider the following snippet:
class MyClass<E>{
...
public void checkType(Object o){
if(o instanceof List<E>){ //this gives compilation error
List<E> list = (List<E>)o; //...
0
votes
0
answers
32
views
How to print the bounded type inferred by the compiler when using Generic types?
Consider the following snippet:
List<Exception> exceptionList = Arrays.asList(new Exception("344"));
List<Error> errorList = Arrays.asList(new Error("44"));
//consider ...
1
vote
1
answer
67
views
List<? super List<? super Integer>> and List<? extends List<? super Integer>> and how to use it correctly?
Consider the following snippet:
List<Double> doubleList = null;
List<Integer> integerList = null;
List<Number> numberList = null;
//expression:1
List<? super List<? super ...
0
votes
1
answer
102
views
Unchecked Cast warning - shows up for Type parameters but not for Concrete types?
Consider the following snippet:
Integer a = Integer.valueOf(23);
Double d = (Double) (Number) a; //not unchecked cast
List<String> stringList = new ArrayList<>();
...
0
votes
0
answers
36
views
How do I avoid infinite loops while calculating a parameterized type's supertypes?
(This is not a basic recursion question, nor is it a question about how to work with ParameterizedType. This is a question concerning the application of some algorithms in the Java Language ...
3
votes
1
answer
138
views
Is it specified in the JVM/JLS that classes in unused code paths will never be loaded?
Given the following class using Java 8 Optional:
final class Main {
public static void main(final String[] args) {
System.out.println(Optional.of("test").get());
}
}
If I ...
1
vote
2
answers
70
views
In the Java Language Specification version 11, section 4.10.2, is it true that a parameterized type containing a wildcard has no direct supertypes?
In the Java Language Specification, version 11, section 4.10.2, it reads, in part, while discussing direct supertypes of parameterized types:
Given a generic type declaration C<F₁,…,Fₙ> (n > ...
0
votes
1
answer
45
views
Should a parameterized type's direct supertype set contain it?
In the Java Language Specification, version 11, section 4.10.2, it reads, in part, while talking about direct supertypes of class and interface types:
Given a generic class or interface C with type ...
7
votes
0
answers
129
views
In the Java Language Specification version 11, section 4.10.2, how do I read Uₖ θ?
(I'm aware of a previous question in this area but you'll notice that the root of the original poster's issue was never resolved. My question, like theirs, is notational in nature.)
In the Java ...
0
votes
0
answers
53
views
Is a class type in the Java Language Specification permitted to be its own supertype?
The Java Language Specification says, in section 4.10, in part:
The supertypes of a type are obtained by reflexive and transitive closure over the direct supertype relation, written S >₁ T, which ...
1
vote
1
answer
44
views
JLS violations via Number and custom types for switch selector expression
JLS 17 mentions for the switch selector expression (like the JLS' before it)
The type of the selector expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum ...
0
votes
0
answers
51
views
Is it true that Arrays in java are not continuous? [duplicate]
Since array objects are created in Heap memory in Java and as specified in JLS, objects in heap memory are not necessarily contiguous, so Arrays are not contiguous in Java?
So it actually contrasts ...
3
votes
1
answer
140
views
Why, when checking compile-time constant, one branch is enough for resolving variable assignation, but is not enough for returning from that branch?
We know, that if if statement's boolean expression/condition contains compile-time constant (or a variable, holding compile-time constant), then compiler can resolve this constant expression and:
...
1
vote
0
answers
55
views
Erasure vs implicit cast for multiple bounded types in a Generic Class/Method
According to JLS§4.4:
The order of types in a bound is only significant in that the erasure of a type variable is determined by the first type in its bound, and that a class type or type variable may ...
4
votes
2
answers
104
views
Interfaces implicitly declaring public methods of Object class? [duplicate]
According to The Java Language Specification, Java SE 16 Edition (JLS) §9.2 Interface Members:
If an interface has no direct superinterface types, then the interface implicitly declares a public ...
2
votes
0
answers
43
views
Generic Parameterized superinterface declaration according to JLS
According to JLS(§:9.1.3):
Given a generic interface declaration I<F1,...,Fn> (n > 0), the direct superinterfaces of the parameterized interface type I<T1,...,Tn>, where Ti (1 ≤ i ≤ n) ...
0
votes
2
answers
56
views
Java unary promotion, left shift operator, and short
The JLS §5.6.1 says:
Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type:
...
if the operand is of compile-time type byte, short, or char, ...
1
vote
2
answers
132
views
Seek a example code showing how Java not followed sequential consistency
As I know, JMM does not follow Sequential Consistency, and we need the volatile keyword to guarantee visibility.
A typical example without SC often describe like
A = 0;
B = 0;
--thread 1--
A = 1;
B = ...
3
votes
2
answers
218
views
Non-Atomic Treatment of double and long
The Java Language Specification states only 'write' operation is treated as two parts:
For the purposes of the Java programming language memory model, a single write to a non-volatile long or double ...
2
votes
0
answers
184
views
Is casting from Wildcard to a specific generic parameterized type an example of capture conversion in Java?
For the following code that compiles without error and runs without exception:
public class Main
{
public static void main(String[] args) {
List<?> unbounded = new ArrayList<...
0
votes
1
answer
81
views
Why Java couldn't figure out some obvious illegal casts when type parameters are involved?
Consider the following example:
public class Example {
public static <T> void f(T obj) {
Integer i = (Integer) obj; // runtime error
}
public static void main(String[] args) {...
2
votes
0
answers
685
views
Incubator Module vs. Preview Feature in Java
JEP 11: Incubator Modules:
Summary
Incubator modules are a means of putting non-final APIs and non-final tools in the hands of developers, while the APIs/tools progress towards either finalization or ...
8
votes
2
answers
409
views
Can a thread first acquire an object via safe publication and then publish it unsafely?
This question came to me after reading this answer.
Code example:
class Obj1 {
int f1 = 0;
}
volatile Obj1 v1;
Obj1 v2;
Thread 1 | Thread 2 | Thread 3
-----------------------------------...
2
votes
2
answers
122
views
increments order in java
Why in java this code outputs 1:
int bracketsTest = 0;
int resultBrackets = 1 + bracketsTest + bracketsTest++; // return 1
But this outputs 2:
int bracketsTest = 0;
int resultBrackets = 1 + ...
15
votes
4
answers
813
views
Java final fields: is "taint" behavior possible with the current JLS
I'm currently trying to understand this JLS section on final fields.
To understand the text in the JLS better I'm also reading The Java Memory Model by Jeremy Manson (one of creators of the JMM).
The ...
0
votes
0
answers
186
views
Is Identifier in the JLS's production for TypeParameter actually TypeVariable?
I'm reading the JLS, Java SE 8 Edition and I'm stuck at the section about type variables.
It reads:
A type variable is an unqualified identifier used as a type in class,
interface, method, and ...
6
votes
5
answers
349
views
Are Java arrays class instances?
The Java Language Specification says:
An object is a class instance or an array.
And it also says:
arrays [...] may be assigned to variables of type Object
But the part that confuses me is:
The ...
1
vote
1
answer
44
views
What does "text" in JLS 3.1 Unicode refer to?
Section 3.1 Unicode of the JLS states:
The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding.
What does "text" refer to?
I'm wondering ...
0
votes
0
answers
90
views
Resource directory has the same name as a Class without ".class"
I have the following directory structure within my project:
src/
├── main
│ ├── java
│ │ └── com
│ │ └── soebes
│ │ └── packages
│ │ └── f1
│ │ ...
4
votes
2
answers
366
views
Sub-typing on Primitive types in Java
I have this statement in my university classes' notes on sub-typing relations:
T is a subtype of S if code written for variables of type S can also be safely used on variables of type T
Section 4.10....
8
votes
2
answers
252
views
Java Memory Model: a JLS statement about sequential consistency seems incorrect
I'm reading Chapter 17. Threads and Locks of JLS and the following statement about sequential consistency in Java seems incorrect to me:
If a program has no data races, then all executions of the ...