Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mimicking preprocessor

Posted on 2002-03-13
3
Medium Priority
?
230 Views
Last Modified: 2011-09-20
New to Java.  I am told that Java does not have the concept of a preprocessor like other languages such as C/C++.  How do you perform conditional compilations in Java?  What I want to be able to do is have certain code executable in release or system test code but not in unit test code.

0
Comment
Question by:c095276
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 400 total points
ID: 6860893
You're right, there is no preprocessor in Java. In fact, there isn't one that is standard with any language, except C/C++.

The Java solution to conditional compilation is to use constants. The constant can be set, before compilation, to control what happens. For instance:

public static final boolean DEBUG  =false;

if(DEBUG)
{   System.out.println("Stuff");
}



This will wind up being conditional compilation (rather than just an if statement) because any Java compiler will omit compiling this code fragment if DEBUG is false, since it is easy to see that it will never be executed. To "turn it on" you change the value of DEBUG to true before your next compile.
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6860901
java 1.4 is the first try to differentiate the release and debug code. In all other releases you have same code for release and debug purposes. If you want to add some code, that must not be executed in the release, you can do this by using a real variables. For example:
class MyClass {
public static final boolean DEBUG = true;
...
public static void main(String[] arg) {
...
if (DEBUG) {
  // do something
}
...
}
}

because the DEBUG is public and static - it is global and you can access it from all classes using this code:
MiClass.DEBUG
0
 
LVL 16

Expert Comment

by:imladris
ID: 6869166
Did that information help?

If so then it is now time to grade the answer. If not, perhaps a clarifying question would be useful.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question