#if in java, like in c preprocessors [duplicate]

2020-06-03 03:41发布

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 ?

6条回答
劫难
2楼-- · 2020-06-03 04:04

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

查看更多
放我归山
3楼-- · 2020-06-03 04:05

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.)

查看更多
Viruses.
4楼-- · 2020-06-03 04:09

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

查看更多
唯我独甜
5楼-- · 2020-06-03 04:09

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.

查看更多
forever°为你锁心
6楼-- · 2020-06-03 04:11

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 $< >>$@
查看更多
聊天终结者
7楼-- · 2020-06-03 04:15

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.

查看更多
登录 后发表回答