- Комментарии на Java: однострочные, многострочные, разница между / * * / и / ** * /
- Однострочный комментарий в Java
- Пример
- Многострочный комментарий в Java
- Пример
- Разница между / * * / и / ** * / комментариями в Java
- Пример с / * * /
- Итог с / * * /
- Пример с / ** * /
- Итог с / ** * /
- Java code commenting best practices
- 3 Answers 3
- A keyboard shortcut to comment/uncomment the select text in Android Studio
- 9 Answers 9
- Comments in Android Layout xml
- 12 Answers 12
- Comments INSIDE tags possible
- Comment shortcut Android Studio
- 25 Answers 25
- Mac With Numeric pad
- Windows/linux :
- Reviewing and Changing Keyboard Shortcuts in Android Studio
Комментарии на Java: однострочные, многострочные, разница между / * * / и / ** * /
Комментарии на любом языке программирования помогают сделать исходный код читаемым, компилятор игнорирует эти комментарии.
Java поддерживает три типа комментариев –
- Однострочные комментарии – С их помощью вы можете прокомментировать отдельные строки. Компилятор игнорирует все от // до конца строки.
- Многострочные комментарии – Можете прокомментировать несколько строк. Компилятор игнорирует все от / * до * /.
- Документационные комментарии – Инструмент Javadoc JDK использует этот вид при подготовке автоматически сгенерированной документации.
Следующий пример демонстрирует использование всех трех типов комментариев в Java.
Однострочный комментарий в Java
Чтобы прокомментировать конкретную строку, просто поместите двойную косую черту (//) перед строкой, как показано ниже.
Пример
Следующий пример демонстрирует использование однострочных комментариев в Java –
Многострочный комментарий в Java
Многострочные комментарии в Java начинаются с / * и заканчиваются * /. Вы можете комментировать несколько строк, просто поместив их между / * и * /.
Пример
Разница между / * * / и / ** * / комментариями в Java
Чтобы разобраться в разнице между / * * / и / ** * / комментариями в Java приведем примеры.
Многострочные комментарии (/ * * /) используются для комментариев в несколько строк в исходном коде.
Пример с / * * /
Итог с / * * /
Документационный комментарий (/ ** * /) используется для создания документации по исходному коду с помощью инструмента Javadoc.
Пример с / ** * /
Итог с / ** * /
Вы можете сгенерировать Java документацию выше класса с помощью команды Javadoc, как –
Средняя оценка / 5. Количество голосов:
Спасибо, помогите другим — напишите комментарий, добавьте информации к статье.
Или поделись статьей
Видим, что вы не нашли ответ на свой вопрос.
Источник
Java code commenting best practices
I have finalized my Java/Android project and now I need to comment the codes (mainly classes and important methods).
I need to do it by following the best industrial standards as later if someone else need to modify, it should be well staright forward.
I read many articles and found 3 main types of java commenting styles.
- Single line comment (//. )
- Block comments (/* . */)
- Doc comments (/** . */)
I read mainly about the option 2 and 3. Stack overflow discussions
So I thought of going with 2nd option as I don’t need to generate HTML docs as these classes are not going to use by any other people and this is the implementation of this app.
Wonder what are the best practices in block commenting indicating «return» type, «parameters» and «breif description» of the method or class.
Would like to hear the best industrial standard practices of the java code commenting.
Thanks in advance.
3 Answers 3
I would recommend going with the 3rd option, because if someone looks at your code for example through an IDE which supports the JavaDOCs (e.g. Eclipse), it’ll show relevant information about the objects he/she examines when he/she hovers over an element that interests him/her.
This way, the developer will not have to open the actual class source file to understand what it’s contract is, what does it do, or perhaps what Exceptions you have to lookout for when using it.
You can link relevant classes/methods together via JavaDOC hooks like @see.
Personally, I usually like to put DOC comments at least on my class, and public methods, for private methods I don’t usually see much use for DOC comments since I don’t usually generate the JavaDOC HTML. Other than DOC comments I usually tend to use the single line comments, and only use block comments when I feel like 1 sentence will not be enough to express what I wanted to, or when the print margin restrictions come into play.
Источник
A keyboard shortcut to comment/uncomment the select text in Android Studio
How do I comment out several lines of text selected in the IDE with a control key combination?
I thought that Ctrl + Alt + C was the correct sequence, but that’s not working.
9 Answers 9
To comment/uncomment one line, use: Ctrl + / .
To comment/uncomment a block, use: Ctrl + Shift + / .
MAC QWERTY (US- keyboard layout) without numpad:
Line comment : ⌘ + /
Block comment: ⌘ + ⌥ + /
MAC QWERTZ (e.g. German keyboard layout):
Android Studio Version ≥ 3.2:
Line comment : ⌘ + Numpad /
Block comment: ⌘ + ⌥ + Numpad /
Android Studio Version ≤ 3.0:
Line comment : ⌘ + —
Block comment: ⌘ + Shift + —
From menu, Code -> Comment with Line Commment. So simple.
Or, alternatively, add a shortcut as the following:
I had the same problem, usually, you have found the shortcut but it doesn’t work because you have not a NumPad. Actually, the only one issue I found is to set my own shortcut with the one I suppose should works.
First step, find the IDE shortcuts : cmd + shift + A enter shortcuts cmd + shift + A enter shortcuts»>
Second step : Find Comments Shortcut with the finder
Third step : Set your custom shortcut (I suggest cmd + shift + / or cmd + : )
Now enjoy, it works on a macBook without NumPad
edit : cmd + shift + : has conflicts
Edit : this both works without conflicts Issue on MacBook
At least in Android Studio 2.2.3 with default key mapping, to comment or uncomment a select text, same hotkeys are used as Toggle. Using the hotkeys changes the state from comment to uncomment, and next time Uncomment to comment on next and vice versa.
1. Comment / uncomment a block of code
Comment sample: /* — Code block —- */
Hotkey: Ctrl + Shift + «/»
Using the same key combinations again will «toggle» commenting or uncommenting the selection.
2. Comment / uncomment a line of code
Comment sample: // — Code —
Hotkey: Ctrl + «/»
Using the same key combinations again will «toggle» commenting or uncommenting the selection.
Note: Key mapping can be changed to different schemes like eclipse, visual studio. or to the custom hotkeys in File -> Settings -> Keymap
Источник
Comments in Android Layout xml
I would like to enter some comments into the layout XML files, how would I do that?
12 Answers 12
As other said, the comment in XML are like this
Notice that they can span on multiple lines
But they cannot be nested
Also you cannot use them inside tags
The World Wide Web Consortium (W3C) actually defined a comment interface. The definition says all the characters between the starting ‘ ‘ form a part of comment content and no lexical check is done on the content of a comment .
More details are available on developer.android.com site.
So you can simply add your comment in between any starting and ending tag. In Eclipse IDE simply typing would auto complete the comment for you. You can then add your comment text in between.
Purpose of specifically mentioning in between is because you cannot use it inside a tag.
is wrong and will give following error
XML comments start with and end with —> .
There are two ways you can do that
Start Your comment with » then end your comment with » —>»
Highlight the part you want to comment and press CTRL + SHIFT + /
ctrl+shift+/ You can comment the code.
Comments INSIDE tags possible
It’s possible to create custom attributes that can be used for commenting/documentation purposes.
In the example below, a documentation:info attribute is defined, with an example comment value:
Note that in this case, documentation.mycompany.com is just a definition for the new custom XML namespace (of documentation ), and is thus just a unique URI string — it can be anything as long as it’s unique. The documentation to the right of the xmlns: can also be anything — this works the same way that the android: XML namespace is defined and used.
Using this format, any number of attributes can be created, such as documentation:info , documentation:translation_notes etc., along with a description value, the format being the same as any XML attribute.
Источник
Comment shortcut Android Studio
I’m searching for useful Android Studio keyboard shortcut for commenting code, as in Sublime Text or Eclipse.
When I press either cmd + / or cmd + maj + / nothing happens.
25 Answers 25
Mac With Numeric pad
Line Comment hold both: Cmd + /
Block Comment hold all three: Cmd + Alt + /
Line Comment hold both: Cmd + + =
Block Comment hold all three: Cmd + Alt + + =
Windows/linux :
Line Comment hold both: Ctrl + /
Block Comment hold all three: Ctrl + Shift + /
Same way to remove the comment block.
To Provide Method Documentation comment type /** and press Enter just above the method name (
It will create a block comment with parameter list and return type like this
In spanish keyboard without changing anything I can make a comment with the keys:
This works because in english keyboard / is located at the same place than — on a spanish keyboard
Be sure you use the slash (/) on right side of keyboard.
For Line Comment:
For Block Comment:
You can see all keymap in Android Studio: Help ➤ Default Keymap Reference
On Mac and Turkish Keyboard it is (both for commenting out and uncommenting)
line comment: cmd + .
block comment: cmd + alt + .
An Example for a Mac OS X 10.5+ with a German layout keyboard without NumPads:
- Open Preferences
- Search for «Keymap»
- Expand «Main menu»
- Expand «Code»
- Delete all shortcuts (just a suggestion)
- Add a shortcut for a single line comment (e.g. Ctrl + + )
- Add a shortcut for a block comment (e.g. Ctrl + alt + )
This way you can be sure that those keys are not mapped (except if you assigned them before to another function)
Reviewing and Changing Keyboard Shortcuts in Android Studio
Keyboard shortcuts can be configured (and reviewed) via the Settings dialog, accessible via File -> Settings .
Once this dialog appears, to review (and/or add) a shortcut, do the following:
Select Keymap in the list on the left. Note that there is a «Keymaps» option, and depending on the original selection, shortcuts assigned to a given feature differ based on the Keymap.
Type «comment» or text corresponding to the feature for which the keyboard shortcut is to be reviewed/assigned. Each match has a line entry in the list below, and keyboard shortcuts are shown right-justified along with the feature. For example, both CTRL + ALT + SLASH and CTRL + K , CTRL + C are the two assignments for creating a line comment.
To modify a shortcut:
- Select the resultant line corresponding to the feature for which the keyboard shortcut is to be reviewed/assigned
- Right-click, and select the desired Add or Remove option.
Источник