3

I was looking into some tutorial and stumbled on this code:

public void run(String... args) throws Exception {
    eventBus.on($("quotes"), receiver);
    publisher.publishQuotes(NUMBER_OF_QUOTES);
}

This is the first time I see $("quotes") in Java. I'd appreciate someone explaining to me what's going on there.

Here's the source:

https://github.com/spring-guides/deprecate-gs-messaging-reactor/blob/master/complete/src/main/java/hello/Application.java#L53

Update:

This is not a question about $ as Java variable name. I know that it's a valid variable name. However, the format of $("something") looks JQuery-like; hence threw me off thinking that it's a special directive or something.

maresa
  • 571
  • 6
  • 15
  • 5
    `import static reactor.bus.selector.Selectors.$;` – Elliott Frisch Mar 31 '17 at 22:30
  • 1
    duplicate of http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name – mwallner Mar 31 '17 at 22:30
  • $ is just a static method of the Selectors class. Read its javadoc to know what it does. – JB Nizet Mar 31 '17 at 22:31
  • It means to call the static method [`reactor.bus.selector.Selectors.$(T obj)`](https://github.com/reactor/reactor-bus/blob/master/src/main/java/reactor/bus/selector/Selectors.java#L68) with the given argument. – Andreas Mar 31 '17 at 22:31
  • whoops - wrong one, duplicate of http://stackoverflow.com/questions/5322632/spring-expression-language-spel-with-value-dollar-vs-hash-vs – mwallner Mar 31 '17 at 22:32
  • 1
    @mwallner It has nothing to do with Spring Expression Language (SpEL). – Andreas Mar 31 '17 at 22:35
  • @mwallner not duplicate, the link you gave is about the String `"$(...)"` not the static call `$(...)` – user85421 Mar 31 '17 at 22:49
  • It is stated in the JLS not to use `$` that way. "The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems." – Lew Bloch Mar 31 '17 at 22:49
  • You mean your answer, and I added emphasis to that point there as well. – Lew Bloch Mar 31 '17 at 22:52
  • Possible duplicate of [What is the meaning of $ in a variable name?](http://stackoverflow.com/questions/7484210/what-is-the-meaning-of-in-a-variable-name) – Tom Blodget Apr 01 '17 at 13:42

2 Answers2

4

it is just a call to the method called $ that is statically imported:

import static reactor.bus.selector.Selectors.$;

it is just a normal method with a strange name.

'$' is a valid character for identifiers according the Java Language Specification 3.8

The "Java letters" include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ sign should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

user85421
  • 28,957
  • 10
  • 64
  • 87
  • @maresa similar to having a line with `http://www.example.com/index` as a normal source code line in the middle of method- it is just a label (`http:`) followed by a comment (`// ...`) – user85421 Mar 31 '17 at 22:44
  • The quote cited shows that that is not a good name for a function. – Lew Bloch Mar 31 '17 at 22:50
  • @LewBloch I am not responsible for that method/package/library neither is the questions author (I think) – user85421 Mar 31 '17 at 22:52
  • I knew $ is legal char for names but I guess it threw me off cas it makes the code looks like JQuery-like. – maresa Mar 31 '17 at 23:05
4

You have an import static reactor.bus.selector.Selectors.$;

From the documentation that is a short-hand alias for object(T) which in turn creates a Selector based on the given object.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249