Solved

Mimicking preprocessor

Posted on 2002-03-13
3
222 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 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now