• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is /* */ not working?

 
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an error in a class.  I want to comment out the entire file and use it for reference later.

I put /*   at the top line.

I put  */  at the bottom.

The editor is complaining that */ a the bottom is an illegal character.  

I have never run into this before.  

What is IntelliJ complaining about?

Thanks,

Kevin
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added another /* at the top.  It cleared the error.  I removed the extra /*  and the error was still gone.

The other errors in the code are still showing.   How do I comment out an entire file in Intellij?  Is there a way to keep the file on my hard drive but remove it from the project tre
 
Marshal
Posts: 28261
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Am I right? The colour-coding supports me.
 
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Added later: This is roughly the same as what Paul Clapham posted, but I hadn't seen that when I wrote it.  See last paragraph for how to fix it in IntelliJ.]

Chances are good that there's at least one comment already in the file.  So if you start with something like this:

...and you add comment lines as you describe, you get:

The problem with this is, the compiler sees your comment as starting on line 1, and ending on line 5.  (The CodeRanch syntax highlighting in this post does the same thing, it seems.). Because it's just looking for the first */ it finds, and considers that the end of the comment.  Then, it's confused by what it sees after that, because the remaining code doesn't form a valid Java file.

Since you're using IntelliJ, the quick and easy way to comment out the whole file is to select the whole text of the file with shortcut ctrl-A or command-A (for "all") and comment it out with ctrl-/ or command-/.  The ctrl commands are for Windows, the command version is for Mac.  It should comment out everything for you using // on each line.   To reverse this, do the exact same thing a second time.  
 
Marshal
Posts: 4526
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Is there a way to keep the file on my hard drive but remove it from the project tre


I would just rename it so that it ends with txt then the IDE won't perform any syntax checking on it.

For example: rename Unwanted.java to Unwanted.java.txt
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,

The comments work the same for me.  

Thanks,

Kevin
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MIke,

I tried selecting all the text with ctrl A.   That was OK.
The ctrl-   made the background of the code change to blue and it wrote some unknown characters at the end in red.

Intellig was still showing the files with a red underline in the project tree.

Thanks,

Kevin
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MIke,

Where do I change the name of the file extension?   I don't see anything for rename on right clicking the name of the file in the project tree.

I haven't tried changing it in file explorer but I would expect it to confuse Intellij.

Kevin
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,

Refactor\rename worked well.

Thanks,

Kevin
 
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing your own commenting out on an IDE is like keeping a dog and barking yourself. The IDE will have a keystroke combination to comment out a block previously hightlighted; I think it is ctrl‑shift‑C on Eclipse. Not ctrl‑C.
Find a copy of Java Puzzlers by Joshua Bloch and Neal Gafter; there is an example where they explain why you should't use /* … */ for commenting out. They explain that you should use // … instead.
 
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer: '/*' comments do not nest. The first '*/' terminates ALL preceding '/*'s.

That's why '//' works better. '//' says "ignore EVERYTHING to the end of this line". Including any sort of comments, quotes, or whatever.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

I'm sure the people making the commenting rules thought about how it should work.  I would have like to have something that comments out code no matter what is in the middle.  

Renaming the file with a txt or text extension works well.   I can use that.

Thanks,

Kevin
 
Mike Simmons
Master Rancher
Posts: 4908
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the commenting rules were basically imported from C - they were developed long ago, before modern IDEs.  So I wouldn't assume they are necessarily the best possible, but that they were intended to be familiar to C programmers.

Renaming the file is easiest if you want to ignore the whole file.  Using crtl-/ in IntelliJ works well for ignoring big chunks, even if not the whole file.  And there, it's using // on each line.
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The comment mechanisms were imported from C++.

The "/*" was the only comment form that C originally supported. The "//" form came in when C++ was created and was then back-ported to C.

C code could generally be "block commented" by wrapping it in #ifdef, but that's a feature that DIDN'T come over to Java. Java doesn't have a pre-processor at all. Though I could make a good case for annotations doing something vaguely similar.

It's very east to block-comment/uncomment code in most modern IDEs, as well as many ordinary text editors. And Emacs, which some will not consider as "ordinary".

The usual mechanism is to highlight the code you want commented and do a key sequence such as CTRL+SHIFT+/. Doing it again typically reverses the process. Highlighted code can contain both "/*" and "//" comments, since this mechanism prefixes each line with "//" for C-like languages and often does somethiing similar for other languages ("#" for Python code, as an example).
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When working in BASIC I would make a while(False) loop around the code I wanted ignored.  
I don't know if others did this also.
It wouldn't work in Java.

Kevin
 
Paul Clapham
Marshal
Posts: 28261
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:It wouldn't work in Java.



You're right, it doesn't. But "if (false) { ... }" is legal Java. I think it's there for exactly the purpose you described. (It produces a warning message "Dead code" in Eclipse at least, but you wanted it dead anyway so you can ignore that.)
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried putting if(false) way at the top in Intillij.  Even before the imports.  and a } on the last line of code.   It gave an error on the top line but did not blank out the code
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:I tried putting if(false) way at the top in Intillij.  Even before the imports.  and a } on the last line of code.   It gave an error on the top line but did not blank out the code



You can't put an "if" statement outside of a method definition. I also have switched off code using "if (false)" or variations on that, though. The complaint about "no path to statement", "code will never execute" or whatever is USUALLY a warning, so OK for development.

The IDE I was using yesterday had "CTRL+/" as its block comment hotkey, BTW. That's really about the easiest way for Java.

Another option would be do do a git branch with the affected code temporarily deleted. Branching and merging are relatively quick and easy in git.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

I was glad to finally figure out how to get code backed up into git yesterday.

I'm going to stick to Java probably for many more months until I get to the end of the book.    

I'll be back to ask about git branching and other topics after that.

Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . I think it's there for exactly the purpose you described. . . .

Well, sort of, yes. The JLS (=Java┬« Language Specification) tells you the reasoning behind permitting if (false) ... and it is almost the same as that.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll add .txt to the end of the file name for now.  
I can't think of a better way that Java could have a notation to blank out code without having to have a matching block.    
 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think renaming the file is a good workaround, sorry. As we said, the simplest way to comment code out is like this.What's wrong with it? It is also simple to reverse because it should be the only instance where you have // at the beginning of the line. All “real” comments starting at the beginning of the line should be /* comments */.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

I see the code example making individual lines of code a comment.   What do I do if I have some "sandbox" code that is a big mess.  I want to comment out most or all of the code?  

How does the example do this?  

Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Get your IDE to comment it out. It can take a long time to comment out hundreds of lines of code by hand. But why should you want to comment out so much? If you are using if (false) ... or similar, that should have been coded already.
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:Campbell,

I see the code example making individual lines of code a comment.   What do I do if I have some "sandbox" code that is a big mess.  I want to comment out most or all of the code?  

How does the example do this?  

Thanks,

Kevin


Do what I said. Use your IDE to highlight the code you want to "hide". Use its "bulk comment" command to put "//" at the head of each line. The IDE I'm using right now simply uses "Ctrl+/" to do that. Quick and easy,

I do a LOT of experimental code, and this is what I do.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

I tried it on IntelliJ and it works great!  

I thought that I tried it before and all it was doing was putting strange looking characters with a blue background at the end of the text.    I don't know what I did differently before.
ctrl+/  and ctrl-/   did exactly what I was looking for.

The keystrokes at the same time take some practice.  Holding the ctrl key with one hand and pressing the other two keys with the other hand made it easier.

Thanks,

Kevin
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think all that was required was to hold down the CTRL key and press the "/" key. Unfortunately, we don't have a good unambigous standard for saying that. Or where the "any" key is.

Different IDEs may have different key sequences, though.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

I never expected commenting out lines of code could get complicated.  It seems as though the folks that designed it did a good job because there is a way to do everything we want it to do.  It takes some exploration, but there is always a way to get to the feature.  

I'll remember the ctrl and /  

Then I'll experiment each time I'm on a new IDE.

Thanks,

Kevin
 
Campbell Ritchie
Marshal
Posts: 79412
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kevin Abel wrote:. . . Then I'll experiment each time I'm on a new IDE. . . .

Reading the help pages for the IDE might be quicker.
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

kevin Abel wrote:. . . Then I'll experiment each time I'm on a new IDE. . . .

Reading the help pages for the IDE might be quicker.

HA! As if anybody'd do that.

Actually, have you TRIED reading help on Eclipse? It's a classic case of forest and trees.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,
I never looked at the the docs for netbeans, eclipse, or Intellij.  

I could search to find out where they reside.

I remember using eclipse the first time.  Everyone knew how to make a new projject, packages, classes and files.  They seemed to just know how to do it.  I had to ask how to do every little thing.  Now if someone new came along they would wonder how I know.  

Each feature was hidden and I never knew what I was supposed to do.    

Kevin
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Traditionally, you could by a book on Eclipse or IntelliJ. But often these days, whatever books you can find are years out of date.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I once read a book about the UFT Automation Tool.   It seemed to me that the author took notes from the time he started using the tool.   Then he took the time to research topics that he did not know about,  organized the book and published it.   I liked it because the author ran into the same issues that I did.

I think that I'd go that path if ever decided to write a technical book.   I'd prefer reading books such as the Head First Java book and Jeanne's Java books.  I'd want to get the book on the market quickly since there would be nothing else around.  Then in later editions I'd move it forward using an agile approach.  I think I'd choose to self publish so that I could keep making upgrades.  

Here I am talking about a book and I never even started one.      

Kevin
 
Tim Holloway
Saloon Keeper
Posts: 27862
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting idea. I'd probably set up a wiki, add concepts as they came to me, then eventually I could organize them into a unified document.

Come to think of it, I suspect that wikis have a lot to do with the relative scarcity of books like that. They simply stay wikis. And as a result, probably feel no obligation to be complete or linear.
 
kevin Abel
Ranch Foreman
Posts: 911
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim,

The thread started with learning about how to comment Java code in the IDEs.   It got us to chatting about how to spread knowledge.  I'd like to chat about it more.  Should I start a new topic/thread and make a link to it in this thread?  

I don't want to annoy a future reader on this informative thread looking at this in another 20 years from now.

Thanks,

Kevin
 
I found a beautiful pie. And a tiny ad:
create a simple IDEA theme and earn!
https://coderanch.com/wiki/708971/intellij-idea/ide/IntelliJ-Themes-Contest-Deadline
reply
    Bookmark Topic Watch Topic
  • New Topic