$ cpp -ansi Main.partial.java | sed 's|^#|//|' > Main.java
$ javac Main.java
$ java Main
Magic!
Not true. From the manpage of GNU cpp 4.9.2:
The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules. For example, apostrophes will be interpreted as the beginning of character constants, and cause errors. Also, you cannot rely on it preserving characteristics of the input which are not significant to C-family languages. If a Makefile is preprocessed, all the hard tabs will be removed, and the Makefile will not work.
Having said that, you can often get away with using cpp on things which are not C. Other Algol-ish programming languages are often safe (Pascal, Ada, etc.) So is assembly, with caution.
Well, Java is sufficiently C-like for CPP to not throw up on simple files.
Output from raw cpp:
# 1 "Main.partial.java"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "Main.partial.java"
public class Fuck {
# 1 "main2.java" 1
public static void frobozz() {
System.out.println("Magic!");
}
# 3 "Main.partial.java" 2
public static void main(String[] args) {
frobozz();
}
}
Useful for the C compiler, but will mess up javac. So we turn all lines that start with a #
and nothing else into comments.
You probably shouldn't. But you can. A "real" use case would be something like keeping autogenerated Java from, say, a graphical form designer and your application logic separate like C# did since the beginning, but then why not use something that lets you define your layouts in XML?