Is it possible to write a Java printf statement that prints the statement itself?

Posted by polygenelubricants on Stack Overflow See other posts from Stack Overflow or by polygenelubricants
Published on 2010-05-22T04:52:07Z Indexed on 2010/05/22 5:00 UTC
Read the original article Hit count: 206

Filed under:
|
|

Is it possible to have a Java printf statement, whose output is the statement itself?

Some snippet to illustrate:

// attempt #1
public class Main {
public static void main(String[] args) {

System.out.printf("something");

}
}

This prints something.

So the output of attempt #1 is not quite exactly the printf statement in attempt #1. We can try something like this:

// attempt #2
public class Main {
public static void main(String[] args) {

System.out.printf("System.out.printf(\"something\");");

}
}

And now the output is System.out.printf("something");

So now the output of attempt #2 matches the statement in output #1, but we're back to the problem we had before, since we need the output of attempt #2 to match the statement in attempt #2.

So is it possible to write a one-line printf statement that prints itself?

© Stack Overflow or respective owner

Related posts about java

Related posts about quine