Share this page 

A curiosity (strange Java code)Tag(s): Varia


A collection of weird Java code!
A really short Java program with a visual output and no main()!
public class J{static{System.out.print
("Real's JavaHowTo");System.exit(0);}}

Can you spot why the following program is compiling ok ?
public class Curiosity {
 public static void main (String[]args){
  System.out.println ("A curiosity");
  http: //www.rgagnon.com/howto.html
  System.out.println ("compile Ok!");
  }
 }
The URL in the middle does not generate any compiler warning because http: is considered as a label and the //www.rgagnon... is seen as a comment.
The next one is strange. From the main(), we create a Foo instance, then we call getFoo() which returns a null and from "null" , we can access the value of fubar!
public class Foo {
    static int fubar = 42;

    Foo getFoo() {
        return null;
    }

    public static void main(String args[]) {
        Foo foo = new Foo();
        System.out.println(foo.getFoo().fubar);
    }
}
// output :  42

(from Usenet)
> I am new to java, and I really havent had time to do much work in it.
> However, I have a small project that is due next week for my class. Can
> anyone help me by providing some code? It should do the following;
>
> The program is simple. It should allow a user to input a string of digits,
> and then output the sum of those digits.
>
> For example; the user inputs 3563
> the program would then output 17 (3+5+6+3)
and one the answer (from E. Sosman) was
public class Homework{public static void main (String[] OoO) {for (int
oOo=0;oOo<OoO.length;++oOo) main (OoO [oOo]);} public static void main
(String oOo){String Oo0="9876543210"; long o0o=oOo.length()*Oo0.length
();for(int OoO=oOo.length();--OoO>=0;) {int O0O=Oo0.indexOf(oOo.charAt
(OoO));o0o-=O0O<0?012:++O0O;} main(oOo,o0o);} public static void main(
String Oo0,long oOo){System.out.print("Sum of digits in ");for(int O0O
=0;O0O<Oo0.length();++O0O)System.out.print(Oo0.charAt(O0O));System.out
.print(" = ");System.out.println(oOo);}}

Signature programs are short programs that people have been using as part of their signature in Usenet message or email. They are small programs, that are often rather cryptic because they have to save space as much as possible.

Here collection of classic signature programs, my own signature when I was a the Universite de Montreal (a long time ago...) is there!

-------------------------+ #include       /* BC ok , Apollo NFG */
Real Gagnon,             | int main(void) { int i=0;
Universite de Montreal   | while(putchar(i++["\13Dl~fxym789\26xd"]-i));
xxxxxxx@xxx.xxxxxxxxx.xx | while(putchar((--i)["\0\t+*)('&%$#\"\1"]+i));}

A signature program in Java is not an easy task because Java is lot more verbose than C. Here an attempt, try it to see the output!

The above code is shown as an image to make sure that special characters are rendered correctly. To see the actual code, download the source : Howto.java.


From SO

Prints hello world

for (long l = 4946144450195624l; l > 0; l >>= 5)
      System.out.print((char) (((l & 31 | 64) % 95) + 32));

See also this HowTo