What is the correct way to access Java members in the text?

When answering questions, I often mention method names and online documentation. I am confused about how method names should be indicated in the text.

For example, I often print:

String.equals() should be used to compare two strings for equality.

However, this is a bit misleading:

  • It makes equals() indicator of a static member.
  • This means that equals() does not accept any arguments.

In the interest of satisfaction, I would like to know:

What is the correct way to access both static members and instance members?

I have seen things like:

  • String.equals ()
  • String # equals ()
  • myString.equals ()

Is there a way to refer to methods with an argumentative-agnostic way?

For example, in C foo(void) explicitly uses a function with a null argument, and foo() can be overridden later to have a different set of arguments. (?)

+7
source share
7 answers

If you really want to be precise, you can use

 java.lang.String#equals(java.lang.Object) 

or shorter form

 String#equals(Object) 

This is similar to the notation used in javadoc, and interestingly also looks like a documentation link that ends with:

 String.html#equals(java.lang.Object) 

Edit: Here is a link to the javadoc documentation describing how to use the links.

+4
source

I found that the documentation link for the String equals method removes ambiguity.

+2
source

Using myString.equals () does not make sense unless you have indicated very clearly that myString is an instance of String. javadoc uses String # equals (), so most developers should be clear and understandable. That would be my choice.

+2
source

String.equals .

If you want to specify argument types String.equals(Object) .

Adding brackets ( equals() ) is the substance of C. In particular, the ANSI C thing in which the no-args function will be written as tostring(void) (I'm actually too young (!) To remember K & R / PCC C ) C ++ fixed this - so () really quite an old school.

+2
source

I usually refer to the name of the class and method, expecting the reader to be able to use it to track documentation. Perhaps this is a little presumptuous. I dont know.

If I try to indicate which signature to use the method, I usually provide a sample code. This is mainly because I find it easier than writing a description for a long time.

+1
source

myString.equals() is perfect. It implies a type and makes it clear that we are talking about an instance.

You can even add:

myString.equals(aString)

+1
source

When calling the method, I use "String.equals ()". This is for readers, and they probably know what you're talking about out of context. If this can be confusing, just say "String.equals () method"

 One should use String.equals() for comparing two strings for equality. 
Seriously, who would be embarrassed by this proposal? So don’t worry. This is just English, the compiler does not yell at you.
0
source

All Articles