Weird Funny Java!

Sometimes we can do really weird and funny things with Java; Some other times we are just being creative!

Take a look at the following three examples and you will find out what I mean!

Have Fun!

Strine translator

Translating to Strine ;)
 

public static void main(String... args) {
        System.out.println("Hello World");
    }

    static {
        try {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            value.set("Hello World", value.get("G'Day Mate."));
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }

prints

G’Day Mate.

BTW: Strine is the Australian Dialect of English.

Randomly not so random

In a random sequence, all sequences are equally likely, even not so random ones.

Random random = new Random(441287210);
    for(int i=0;i<10;i++)
        System.out.print(random.nextInt(10)+" ");
    }

prints 1 1 1 1 1 1 1 1 1 1

and

Random random = new Random(-6732303926L);
    for(int i=0;i<10;i++)
        System.out.println(random.nextInt(10)+" ");
    }

prints 0 1 2 3 4 5 6 7 8 9

Lastly

public static void main(String ... args) {
    System.out.println(randomString(-229985452)+' '+randomString(-147909649));
}

public static String randomString(int seed) {
    Random rand = new Random(seed);
    StringBuilder sb = new StringBuilder();
    for(int i=0;;i++) {
        int n = rand.nextInt(27);
        if (n == 0) break;
        sb.append((char) ('`' + n));
    }
    return sb.toString();
}

prints hello world

Java plus

A confusing piece of code here for you to parse. ;)

int i = (byte) + (char) - (int) + (long) - 1;
System.out.println(i);

prints 1

Reference: Java plusRandomly not so random and Strine translator from our JCG partner Peter Lawrey at the Vanilla Java.

Related Articles:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

3 Responses to "Weird Funny Java!"

  1. DP2010 says:

    For the confusing piece of code, (long) puts a curse on the next character. It does it if you put “;” for the rest of the expression, too. Putting “+ 0″ after (long) removes the curse. I don’t know why. Magic isn’t big on explanation.

  2. apprentice says:

    Out of curiosity, did you brute-force to find the seed that’d produce “Hello, World”? Or is there an easier way to do that?

  3. Johan says:

    haha biggest noob programmer i ever seen

Leave a Reply


− two = 1



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close