The Artima Developer Community
Sponsored Link

Java Answers Forum
Any similar technique like C's #ifdef in Java?

5 replies on 1 page. Most recent reply: Aug 14, 2003 4:25 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Kenneth Rajkumar

Posts: 1
Nickname: aalker123
Registered: Aug, 2003

Any similar technique like C's #ifdef in Java? Posted: Aug 13, 2003 12:16 AM
Reply to this message Reply
Advertisement
Hello all,

I am looking for a similar technique of C's #ifdef in Java (J2ME)? Any idea or tips or help?

Thanks.


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Any similar technique like C's #ifdef in Java? Posted: Aug 13, 2003 1:52 AM
Reply to this message Reply
There is no pre-processor in Java.

If I was you, I'd use the System Properties to set flags such as those for which you would use a pre-processor.

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Any similar technique like C's #ifdef in Java? Posted: Aug 14, 2003 12:41 AM
Reply to this message Reply
Indeed there is no preprocessor.

You can use this construct in some situations :

static final boolean DEBUG = true/false;
if(DEBUG) { ... }

If DEBUG is set to false, the compiler will leave out the entire if block, because it is declared final.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Any similar technique like C's #ifdef in Java? Posted: Aug 14, 2003 12:51 AM
Reply to this message Reply
Do you know that for sure?

Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: Any similar technique like C's #ifdef in Java? Posted: Aug 14, 2003 12:58 AM
Reply to this message Reply
Yup, at least with sun's compiler.

The following class :

public class Optimize {
public static final boolean DEBUG = false;

public Optimize() {
if(DEBUG) {
System.out.println("Test");
}
}

}



Compiled with DEBUG=true:

Method Optimize()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 getstatic #2 <Field java.io.PrintStream out>
7 ldc #3 <String "Test">
9 invokevirtual #4 <Method void println(java.lang.String)>
12 return


And compiled with DEBUG=false:


Method Optimize()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 return

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Any similar technique like C's #ifdef in Java? Posted: Aug 14, 2003 4:25 PM
Reply to this message Reply
In conjunction with this, you could write a simple script that goes through all the java files in your project and modifies the line matching something like "^\s*public static final boolean DEBUG = (false|true);\s*$" (I'm not a regualar expression guru, so it can probably be improved upon) to either true or false as appropriate (debug vs. release).

<mount object="soapbox">Incidentally, I think the knowledge of conditionally-compiled code makes people a bit too anal retentive on this issue. That is, using a system property as in a previous suggestion is probably acceptible -- and more flexible -- in most cases. Once people become aware of the idea that a trace or log message can be conditionally compiled out, they get this notion that if it doesn't, it will have some huge impact on the performance of the program. This is usually not the case. In the small number of cases where it is, it can be specifically addressed.</mount>

Flat View: This topic has 5 replies on 1 page
Topic: Can Java Frame contain a window of web browser? Previous Topic   Next Topic Topic: process.waitFor() causes program to hang!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use