4

The JLS for Java 8 introduces three new separators:

...   @   ::

The ellipsis (...) is used as "last formal parameter of a method or constructor". But already the JLS for Java 7 described this formal parameter as variable arity parameter. It isn't mentioned as a separator.

The separator :: is used to form method reference expressions.

The @ is meant "to distinguish an annotation type declaration from an ordinary interface declaration" and thus part of an annotation type declaration. Annotations were already part of the JLS for Java 7. I see no other usage for the "at-sign".

Searching for questions and answers about Java 8+ separators I found nothing, which explains the usage of the new separators.

What do the both separators ..., @ separate? How are they used?

LuCio
  • 5,055
  • 2
  • 18
  • 34

2 Answers2

3

First of all, as you note ... was present in Java 7. (I think it was introduced in Java 5!) But with Java 8 they listed it explicitly as one of the "separator" tokens.

I don't think there is any real significance to that. It is merely an editorial correction.

What do the both separators ..., @ separate?

They don't really separate anything. They are classified as "separators" because they are not operators, literals, identifiers or keywords.

Notes:

  1. In JLS 11 Section 3.1, the spec defines an alternative term "punctuator". Ugly, but more accurate.)

  2. It would not be useful for the JLS to distinguish "separators" that separate from those that don't. The term "separator" is hardly used in the rest of the spec, and certainly not in any context that implies separation.

  3. There is precedent for "separators" not separating. The existing tokens (, ), {, }, [ and ] don't really act as separators either. Their purpose is to start or end constructs.

  4. The < and > tokens are also a bit odd. The lexical grammar calls them operators, but in some contexts they act as (loosely) separators.

Don't try to read too much into this.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I was thinking about "an editorial correction" too. But honestly I couldn't imagine that `...`could be missed for years and several Java versions. – LuCio Feb 11 '19 at 12:15
  • Imagination is a funny thing. Check the "lexical grammar" chapter in Java 7 JLS. The `...` token is simply not mentioned. But clearly it exists as far as the compiler is concerned ... ans as far as Chapter 8 is concerned. The JLS contains mistakes, some of them minor (like this), some more significant. – Stephen C Feb 11 '19 at 12:21
  • 3
    Note that [the last chapter](https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html) contains a complete grammar, using `...` and `@`, which makes them valid tokens for the Java language, even when they are not categorized as separator. As this answer mentions, this categorization has no practical consequences, just like the distinction between “Separators” and “Operators” (isn’t `::` a kind of operator?) makes no difference. – Holger Feb 11 '19 at 12:38
0

The top sentence in your link says

This chapter specifies the lexical structure of the Java programming language.

That is the whole point here. It is a (half-formal) description of the Java language syntax. We are not talking semantics here, it is just "wording". Describing the different elements in the syntax of the Java programming language.

You could say in public foo(Bar... vararg)

  • ( separates the method name from the parameter list
  • ... separates the parameter type from the parameter name

You probably have to dive into the how of Java source code parsing to get to the reasons why ... is listed as separator. Meaning: that term is derived from how the parser will deal with ..., not necessarily a semantical meaning.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I also thought about `...` as a separator within a parameters declaration. And also the `@` within an annotation type declaration can be seen as a separator. It separates a potential modifier from the keyword `interface`. But then these separators aren't new to Java 8 and that's irritating. – LuCio Feb 11 '19 at 12:12
  • As said: I think you are trying to find (semantical) sense in a purely syntactical construct. That is like trying to solve a conflict with your spouse by explaining to her, that in fact, you took out the garbage three times this week. But that isn't the point that helps her after telling you "you never take out the garbage" ;-) – GhostCat Feb 11 '19 at 12:14