Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

printf

quine

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?

like image 967
polygenelubricants Avatar asked May 22 '10 04:05

polygenelubricants


People also ask

What is a printf statement in Java?

The printf method in Java can be used to output a formatted string to the console using various format specifiers. It is also an overloaded method of the PrintStream class.

Can we use printf in Java?

printf() method is not only there in C, but also in Java. This method belongs to the PrintStream class. It's used to print formatted strings using various format specifiers.

What does a printf statement do?

The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

What is the difference between printf and Println in Java?

println is short for "print line", meaning after the argument is printed then goes to the next line. printf is short for print formatter, it gives you the ability to mark where in the String variables will go and pass in those variables with it.


2 Answers

It's not pretty, but this is certainly possible:

public class Main {
public static void main(String[] args) {

System.out.printf("System.out.printf(%c%s%1$c,34,%1$c%2$s%1$c);",34,"System.out.printf(%c%s%1$c,34,%1$c%2$s%1$c);");

}
}

The output (as run on ideone.com) is:

System.out.printf("System.out.printf(%c%s%1$c,34,%1$c%2$s%1$c);",34,"System.out.printf(%c%s%1$c,34,%1$c%2$s%1$c);");

This output matches the printf statement.

There are likely to be shorter solutions.

See also

  • java.util.Formatter syntax
    • %[argument_index$]conversion
like image 78
polygenelubricants Avatar answered Nov 14 '22 22:11

polygenelubricants


System.out is a static PrintStream instance which may be replaced with any PrintStream by inovking System.out.setOut(PrintStream s). So, just write a subclass of PrintStream and override the necessary methods. The following is just a very simple example for demonstration. It's advisable to override more methods.

    public class VerbosePrintStream extends PrintStream{

        public VerbosePrintStream (PrintStream ps){
            super(ps, true);
        }

        @Override
        public void println(String x) {
            super.println("System.out.println(\""+x + "\");");
        }

    }

Now we test the above class:

VerbosePrintStream vps = new VerbosePrintStream(System.out);
    System.setOut(vps);
    System.out.println("test string");
like image 41
Vincent Avatar answered Nov 14 '22 23:11

Vincent