Published 03 Sep, 2022

Java - Comment/uncomment a block of code in NetBeans shortcut

Category Java
Modified : Sep 29, 2022
21

Is there a shortcut in NetBeans to highlight a block of code and comment/uncomment it?

Answers

There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Netbeans. These have been categorized in sections for a clear and precise explanation.

35

The list of keyboard shortcuts can be found in the NetBeans Wiki.

Turn On/Off Search Result highlights

Alt + Shift + H

Add/remove comment. Should work for all languages

Ctrl + / or in mac + /


130

Try this combination in the Netbeans Editor: ctrl + shift + c


38

In mac, it is more stable to use command+shift+c. Sometimes, command+/ could be usable but not so stable.


31

An IDE independent trick (that works for all languages in the C/Java/C++/C# family) I found to comment/uncomment blocks of code fast is the following:

int a = 5;
int b = 2;
//*
if(a < b) {
    int t = a;
    a = b;
    b = t;
}
//*/
System.out.println("a: "+a);

Say you want to comment and uncomment the if block frequently. You can use the //* and //*/ markers. You comment the block by removing one / in the //* part. Thus:

int a = 5;
int b = 2;
/*
if(a < b) {
    int t = a;
    a = b;
    b = t;
}
//*/
System.out.println("a: "+a);

Why this works

In case the first line reads //*, it is interpreted as // *, thus you comment the * and don't comment the remainder of the block. The //*/ is ignored as well since it is interpreted as // */.

In case the first line reads /*, it is interpreted as the start of a comment block. Java searches for the corresponding end which is // */ (the // is ignored).