11

Possible Duplicate:
How to mark java code such that it’s not compiled

In , we can prevent compilation of block code like this :

#if 0

    //code here

#endif

So even if code block is error prone the code compiles, I want the same thing in Java, So that I can skip that part of code which won't compile because some library is missing.

Can anyone help me ?

Community
  • 1
  • 1
Charan Pai
  • 2,288
  • 5
  • 32
  • 44
  • 1
    No. No preprocessor. comment it – Prince John Wesley Jan 08 '13 at 05:10
  • 3
    An alternative to commenting (though a VERY VERY slight performance concern), since commenting out 100 blocks every time you want to release your build is lame, is to put `if (SomeClass.DEBUG == true) { ... }` then using a `public static final boolean DEBUG` in `SomeClass`. (Also: http://stackoverflow.com/questions/8293124/how-to-mark-java-code-such-that-its-not-compiled) – Cat Jan 08 '13 at 05:12
  • Related: http://stackoverflow.com/questions/1813853/ifdef-ifndef-in-java – Pacerier Jun 25 '14 at 03:07

6 Answers6

21

There is no preprocessor in Java. Depending on your build system, you may be able to use a third-party preprocessor (you can find lots of them by searching for "java preprocessor"). Some examples are

Depending on the scope you want to comment out, you can use block comments and sometimes something like

if (false) {
    . . .
}

If all else fails, just comment out every line using //. Most IDEs have a way to do this (and undo it) efficiently.

P.S. If you can use block comments (not always possible, since block comments can't be nested in Java), there's a nice trick that makes it easier to toggle off and on the comment-out portion. Start your block comment on a line by itself, but end it with another isolated line that starts with a line comment, like this:

/*
   <block of code ignored as comment>
//*/

Then if you want to turn the commented-out section back on, just add a second / at the start of the block:

//*
   <block of code now active>
//*/

To toggle the code off again, just remove the first /. (Without the // at the start of the last line, the dangling */ would be a syntax error whenever you activated the code by adding a / to the opening line of the block.)

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
14

You have to comment out the code, you can't use pre-processor directive in java.

Habib
  • 219,104
  • 29
  • 407
  • 436
8

I'm under the assumption that the compiler will strip the code inside blocks enforced with constant/final flags? This is how you can leave code in your project that isn't shipped in the final apk.

public final static boolean DEBUG = false;

if(DEBUG) {
    //add messy error prone code here
    //compiler will strip this out as unreachable when false
}

Read here:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

Chuck D
  • 1,629
  • 2
  • 16
  • 32
  • "An optimizing compiler _may_ realize that the statement x=3; will never be executed and _may_ choose to omit the code for that statement from the generated class file" - that's subtly different to your assertion that it "will". Whether it's true for the Java compiler in Eclipse, I don't know. – paxdiablo Jan 08 '13 at 05:22
  • Yes, this is with regards to the Dalvik environment but more a rule of thumb. No certainties with a statement that general but i've seen this stripped out when evaluating dex2jar and inspecting. If you use Proguard, that may affect your results as well. – Chuck D Jan 08 '13 at 05:27
  • ok. But eclipse wont allow me to compile right ? – Charan Pai Jan 08 '13 at 05:29
  • 1
    It's called build in Eclipse and you can set it up to auto-build in the Project tab. That's one of several ways you can compile the project. Not sure of your setup/environment. – Chuck D Jan 08 '13 at 05:32
  • @Charan, if there are _errors_ within the code then, no, Eclipse won't compile. This solution _will_ work if you just want _valid_ code to be left out of the final app (even if the code is there, it won't be run). – paxdiablo Jan 08 '13 at 05:33
  • 1
    Yes, as @paxdiablo says, this is different than preprocessor directives ignoring a code block. The code still has to compile/build. Preventing compilation due to errors is different than stripping error prone code that still compiles. If the block of code has runtime issues, this will work if the compiler strips the block. – Chuck D Jan 08 '13 at 05:35
  • @CharanPai maybe start a new thread with your compilation issue if this doesn't help? – Chuck D Jan 08 '13 at 05:39
  • Does not work if the code inside of the if does not compile ... – Angel O'Sphere Nov 01 '19 at 17:22
1

Java has no pre-processor along the lines of C.

Since, according to the tags, you're using Eclipse, you can simply mark the entire block of code you want to comment out then use CTRL - /.

You can also use that same key sequence to uncomment an already-commented-out block.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

One technique I've used is to use m4 as a preprocessor for java files. You write your code in classname.java.m4 and use a Makefile or other build system rule to run m4:

%: %.m4
        @echo "/* automatically generated from $< -- don not edit*/" >$@
        m4 $< >>$@
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

java Do not provide facility of pre-processor.

BTW you Can Use Comment Like as Below :

you're using Eclipse, you can simply mark the entire block of code you want to comment out then use CTRL+SHIFT+C for Commenting and uncommenting the Block of code.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
  • 1
    This answer is just plagiarizing @paxdiablo's answer below. – Jeremy Thompson Jan 08 '13 at 05:16
  • 1
    Actually, it's not _quite_ plagiarism. The commenting keystrokes are different though I still prefer mine since it's one less keypress :-) – paxdiablo Jan 08 '13 at 05:18
  • Surrounding code with block comments will fail if the code itself contains a block comment. Also, the [official keyboard shortcut](http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_comment_out.htm) for commenting code is CTRL+/ (or CTRL+SHIFT+/), not CTRL+SHIFT+C. – Ted Hopp Feb 16 '15 at 17:23