Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java have macros? [closed]

In class the prof said one reason he likes C better than Java is that C has a preprocesor, and in particular macros. Is there any benefit to macros over declaring afinal/const variable with the desired value? I know Java doesn't have global variables so would there ever be a situation where using a variable in substitution for macros would not work?

like image 919
Celeritas Avatar asked Nov 30 '25 02:11

Celeritas


1 Answers

Using a macro system in C has been a long standing tradition that often leads to incomprehensible build chains, and very project specific build configurations. I would argue that the reason that macros exist in C is to overcome the shortcomings it has as a language for when you need things that macros provide (for example, you can use interfaces in Java, which solve the problem of needing to make a "generic" instance of something).

Still, there are times when having a macro system just seems to fit a problem easier. I do know of some shops that do this -- for example -- when they need to support multiple different versions of an API. Generally you can overcome this problem in Java by proper use of interfaces and a good abstraction layer in your design. However, there are macro systems for Java out there! You can use them, if you please. You might also note that, as far as macros go, C macros aren't really anything very special.

(In my opinion your teacher was somewhat off base...)

like image 143
Kristopher Micinski Avatar answered Dec 02 '25 16:12

Kristopher Micinski