0

I've noticed that the # symbol is commonly used in Java literature. Does it denote methods of class instances? Is this some kind of standard or convention?

Here are examples of this symbol being used:

Spring Boot Persistence Best Practices (ch 3, page 150)

// behind the scene it calls EntityManager#merge()

Spring Boot Reference Documentation (ch 4.25.3, page 82)

Alternatively, you can use @ImportAutoConfiguration#exclude.

Michael
  • 41,989
  • 11
  • 82
  • 128
bezbos.
  • 1,551
  • 2
  • 18
  • 33
  • 5
    `#` is used in JavaDocs to reference methods of classes. It's caught on in other places as a way to indicate a class member – ernest_k Sep 21 '20 at 11:29
  • Related: https://stackoverflow.com/questions/5915992/how-to-reference-a-method-in-javadoc – Hulk Sep 21 '20 at 11:30

4 Answers4

3

It used to denote the delineation between a class and a method, much like the method reference operator :: *.

EntityManager#merge() means the merge method of the class EntityManager. Because overloading exists, sometimes the types of the parameters are necessarily included to remove any ambiguity, but sometimes they are omitted. In this case, we can see the method has no parameters.

There is no way to tell from this notation whether it's a static or instance method, what its modifiers are, etc. It is used in similar way to a URI fragment; it is designed to act more like a signpost to the method in question.

You see it used for fields and annotation type elements too but methods are the most common.


* I learned yesterday that this is called "Paamayim Nekudotayim". Now you know
Michael
  • 41,989
  • 11
  • 82
  • 128
  • 1
    @BoškoBezik The same way, but without any parentheses. (though sometimes parenthesis are omitted on methods too. it is an ambiguous notation) – Michael Sep 21 '20 at 11:54
1

# refers to class members or methods.

It is very useful because it allows you to specify better documentation for Java code in general (i.e.: javadocs)

Example:

public class Dog {
    private String name;

   // ...

   /**
    * Returns the dog's description using it's name and how it barks.
    *
    * @see Dog#name
    * @see Dog#bark()
    */
    public String getDescription() {
       // ...
    }

    public String bark() {
       // ...
    }
}
jbuddy
  • 180
  • 4
  • 14
1

For all Java applications, # is used in a .properties file to comment out a line.

The example which you have mentioned is just a convention for denoting a method e.g String#split can be used to denote the split function of the class, String.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

It's an evil scourge that should be stamped out. The # notation is a leakage from HTML-based documentation into mainstream written text. It's egregious because there are already perfectly good ways of writing class method calls and attributes, that match those actually used in the language. Why write "EntityManager#merge()" when the "EntityManager.merge()" is already good Java?

I don't know why these affectations break out, but they should be resisted. Goodness knows, programming is difficult enough, without introducing dysfunctional language syntax into documentation.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15