Skip to content

Instantly share code, notes, and snippets.

@crystoll
Created February 7, 2021 09:48
Show Gist options
  • Save crystoll/3c57e41fb4d6d4d0307b77b69c254560 to your computer and use it in GitHub Desktop.
Save crystoll/3c57e41fb4d6d4d0307b77b69c254560 to your computer and use it in GitHub Desktop.
/** Crazy 1 */
import java.util.ArrayList;
public class Crazy1 {
// Props to:
// - https://stackoverflow.com/questions/15496/hidden-features-of-java
// - Java Puzzlers (Joshua Bloch)
public static void main(String[] args) {
// Double brace initialization
// http://wiki.c2.com/?DoubleBraceInitialization
var validCodes = new ArrayList<String>() {{
add("HOW");
add("IS");
add("THIS");
add("WORKING");
}};
validCodes.forEach(System.out::println);
}
}
/** Crazy 2 */
import java.util.Collection;
public class Crazy2 {
// Joint union in type parameter variance
public static <A, B extends Collection<A> & Comparable<B>> boolean foo(B b1, B b2, A a) {
return (b1.compareTo(b2) == 0) || b1.contains(a) || b2.contains(a);
}
public static void main(String[] args) {
System.out.println("Yes");
}
}
/** Crazy 3 */
interface Room {
public Room north();
public Room south();
public Room east();
public Room west();
}
// Use enum to implement an interface
enum Rooms implements Room {
FIRST {
public Room north() {
return SECOND;
}
},
SECOND {
public Room south() {
return FIRST;
}
};
public Room north() { return null; }
public Room south() { return null; }
public Room east() { return null; }
public Room west() { return null; }
}
public class Crazy3 {
public static void main(String[] args) {
System.out.println(Rooms.FIRST.north());
}
}
/** Crazy 4 */
public class Crazy4 {
public static void main(String[] args) {
// Can the next row be valid??? IDE says no!
http://www.google.com
System.out.println("Another odd thing");
}
}
/** Crazy 5 */
public class Crazy5 {
public static void main(String[] args) {
System.out.println("Let's do some basic math! Is 1 == 1? Is 128 == 128?");
// Boxing/Unboxing + Integer pool oddities
var a = 1;
var b = 1;
var c = Integer.valueOf(1);
var d = Integer.valueOf(1);
var e = new Integer(1);
var f = new Integer(1);
Integer g = 128;
Integer h = 128;
System.out.printf("Is %s same as %s? %s%n",a, b, a == b);
System.out.printf("Is %s same as %s? %s%n",a, c, a == c);
System.out.printf("Is %s same as %s? %s%n",c, d, c == d);
System.out.printf("Is %s same as %s? %s%n",a, e, a == e);
System.out.printf("Is %s same as %s? %s%n",e, f, e == f);
System.out.printf("Is %s same as %s? %s%n",g, h, g == h);
}
}
/** Crazy 6 */
// This needs to be run from the command line
public class Crazy6{static{System.out.print("ONELINER");System.exit(0);}}
/** Crazy 7 */
public class Crazy7 {
public static void main(String[] args) {
// Variable names are defined to be perhaps more flexible than you think...
var $ = "THIS IS OKAY!";
var \u0223 = "\u0223THIS IS OKAY TOO!";
System.out.printf("Yeah, I can live with these%n%s%n%s%n", $, \u0223);
System.out.println("Okay, still please don't use these! :)");
}
}
/** Crazy 8 */
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
public class Crazy8 {
public static void main(String[] args) {
var currentDate = new Date(2021,1,1);
System.out.printf("Welcome to %s%n",currentDate);
Calendar cal = Calendar.getInstance();
cal.set(2021,12,31);
System.out.println(cal.getTime());
}
}
/** Crazy 9 */
import java.util.Random;
public class Crazy9 {
public static void main(String... args) {
// Are the random number generators really random? They are so random that
// sometimes they are not random at all!
// This party trick is here just here for no purpose, mathematically this is what you'd expect
// Remember this the next time lottery numbers are generated!
Random random = new Random(-6732303926L);
for (int i = 0; i < 10; i++)
System.out.print(random.nextInt(10) + " ");
System.out.println("");
System.out.println(randomString(-229985452) + ' ' + randomString(-147909649));
}
public static String randomString(int seed) {
var rand = new Random(seed);
var sb = new StringBuilder();
for (int i = 0;; i++) {
int n = rand.nextInt(27);
if (n == 0)
break;
sb.append((char) ('`' + n));
}
return sb.toString();
}
}
/** Crazy 10 */
public class Crazy10 {
public static void main(String[] args) {
// WHaaaaat witchcraftery is this?
System.out.println((byte) + (char) - (int) + (long) - 1);
String _‎ = "Hello "; String _‏ = "World"; String _‎‏ = " !!";
System.out.println(_‎+_‏+_‎‏);
}
}
/** Bonus Crazy */
import java.lang.reflect.Field;
public class Crazy11 {
public static void main(String[] args) {
System.out.println("Okay what's the output?");
}
static {
try {
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("Okay what's the output?", value.get("Terveisiä peräseinäjoelta!"));
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment