Are you in dire need of a self-printing Java program? A few intrepid souls have submitted these amusements to us. If you want to submit your own then please send your self printing java program in an email to javaweb-submit@gnu.org.
import java.text.*;class a{public static void main(String x[]){char b[]={34};char c[]={123};String s[]=new String[3];s[0]="import java.text.*;class a{2}public static void main(String x[]){2}char b[]={2}34};char c[]={2}123};String s[]=new String[3];s[0]={1}{0}{1};s[1]=new String(b);s[2]=new String(c);System.out.println(MessageFormat.format(s[0],s));}}";s[1]=new String(b);s[2]=new String(c);System.out.println(MessageFormat.format(s[0],s));}}
class p{public static void main(String[]a){char q=34,r=125,s=59;System.out.println(t+q+t+q+s+r);}static String t="class p{public static void main(String[]a){char q=34,r=125,s=59;System.out.println(t+q+t+q+s+r);}static String t=";}
class S{public static void main(String[]a){new S().p('"',"'","class S{public static void main(String[]a){new S().p('",");}void p(char d,String q,String a,String b){System.out.println(a+d+q+','+d+q+d+','+d+a+d+','+d+b+d+b);}}");}void p(char d,String q,String a,String b){System.out.println(a+d+q+','+d+q+d+','+d+a+d+','+d+b+d+b);}}
class S{static char q='"';static String s="class S{static char q='';static String s=;public static void main(String[]a){StringBuffer b=new StringBuffer(s);b.insert(41,q+s+q);b.insert(23,q);System.out.println(b);}}";public static void main(String[]a){StringBuffer b=new StringBuffer(s);b.insert(41,q+s+q);b.insert(23,q);System.out.println(b);}}
class P{public static void main(String[]a){char d=34,e=39;String t="+d+t;System.out.println(t+d+';t='+d+t.replace(e,d));}}";t="class P{public static void main(String[]a){char d=34,e=39;String t="+d+t;System.out.println(t+d+";t="+d+t.replace(e,d));}}
class a{static void main(String[]a){char c=34;System.out.print(s+c+s+c+';'+'}');}static String s="class a{static void main(String[]a){char c=34;System.out.print(s+c+s+c+';'+'}');}static String s=";}
class a{static void main(String[]a){System.out.print((s+=(char)34)+s+';'+'}');}static String s="class a{static void main(String[]a){System.out.print((s+=(char)34)+s+';'+'}');}static String s=";}
Eric says:
The savings come from appending the quote character to s in-place, rather than by a separate char variable.
By the way, the removal of public in the type signature for main(String[]) args, while legal Java, should not be executable by the VM. JVMS 5.2 and JLS 12.1.4 are both strict on the fact that execution can only begin on a public method main, even if a bug in the JDK makes it seem otherwise. Therefore, the smallest legal executable is 14 bytes longer, or 208 bytes, after including the keyword public twice.
Eric has also submitted his own entry:
162 characters:
class s{static{new s(){{System.out.print((s+=(char)34)+s+';'+'}');}};}String s="class s{static{new s(){{System.out.print((s+=(char)34)+s+';'+'}');}};}String s=";}
He says:
As long as you are willing to bend the rules, this is the shortest self-printing Java program I could come up with.
Notice that `java s | diff s.java -` completes successfully, indicating that output to stdout matches the original program exactly. This behavior is due to a bug in the JDK which executes the static initializer before throwing the NoMethodFoundError for the non-existant main method.
If the output to stderr bothers you, use the 192 byte version below.
class _{static{new _(){{System.out.print((_+=(char)34)+_+';'+'}');}};System.exit(0);}String _="class _{static{new _(){{System.out.print((_+=(char)34)+_+';'+'}');}};System.exit(0);}String _=";}
Return to GNU's home page.
Please send FSF & GNU inquiries & questions to gnu@gnu.org. There are also other ways to contact the FSF.
Please send comments on these web pages to webmasters@www.gnu.org, send other questions to gnu@gnu.org.
Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA
Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
Updated: Last modified: Thu Apr 19 23:42:20 EDT 2001