Olivier Grégoire
  • Member for 7 years, 11 months
  • Last seen this week
Java is to JavaScript as Car is to Carpet
Accepted answer
104 votes

Java/JavaScript Polyglot, 108 107 106 bytes //\u000As->s.matches("(?i)(?!.*javascript)java.*"/* a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet" Run as Java //\u000As->s.matches("(?i)...

View answer
Determine your language's version
66 votes

Java, 189 bytes, 10 versions, score = 18.9 Supported versions: 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8 and 9 Note that most versions after 10 didn't include any new classes in java.util, so no ...

View answer
Shortest Code to Legitimately Slack Off
43 votes

Java, 102 95 89 88 78 bytes class A<T>{}class B<T>extends A<A<?super B<B<T>>>>{A<?super B<A>>a=new B<>();} This terminates with a ...

View answer
How many arguments were passed?
34 votes

Java (JDK 10), 11 bytes a->a.length Try it online!

View answer
"Hello, World!"
32 votes

Java, 61 bytes enum H{H;{System.out.print("Hello, World!");System.exit(0);}} This is valid in both Java 5 and Java 6. This won't work in Java 4 or earlier (because enum didn't exist) and will not ...

View answer
"As we travel the universe..."
20 votes

Java (JDK), 90 bytes, score = 97 s->("ýCĄ (ᬺ!˂Fɍ".charAt(s.substring(2).chars().sum()%96%49%25)-7)*100 Try it online! This ...

View answer
Print some JSON
16 votes

Java (JDK 10), 20 bytes v->"{\"\":"+1/.3+"}" Try it online! Output {"":3.3333333333333335}

View answer
Exchange capitalization
15 votes

Java (JDK 10), 66 bytes a->b->{for(int i=a.length,t;i-->0;b[i]^=t)a[i]^=t=(a[i]^b[i])&32;} Try it online! Explanations a->b->{ // Curried lambda for(...

View answer
Java is to JavaScript as Car is to Carpet
15 votes

Java (OpenJDK 8), 92 82 72 58 57 bytes s->s.matches("(?i)(?!.*javascript)java.*")?"car":"carpet" Try it online! 1 byte saved thanks to @Nevay!

View answer
iHateOddNumbers
13 votes

Java (JDK), 10 bytes n->n/=~n&1 Try it online! Explanations This code basically divides by zero when the input number is odd or by one when it is even. ~n The ~ character negates each bit of ...

View answer
IP address or not?
13 votes

Java (JDK), 63 bytes s->("."+s).matches("(\\.(25[0-5]|(2[0-4]|1\\d|[1-9])?\\d)){4}") Try it online! Credits -1 byte thanks to Kevin Cruijssen l4m2 for showing the previously-failing case .1.1.1....

View answer
Implementing a stack
13 votes

Java (JDK 10), 42 bytes Since "[the] output is flexible [...], the only thing that matters is the order", this changes the input array into a 0-terminated array. Example : [1,0,2] will return [2,0,2] ...

View answer
Sing Baby Shark
12 votes

Java (JDK), 135 bytes v->{for(var s:"Baby Daddy Mommy Grandpa Grandma".split(" "))System.out.println(((s+=" Shark")+" doo".repeat(6)+"\n").repeat(3)+s+"!");} Try it online!

View answer
It was just a bug
12 votes

Java 11 (JDK), 98 bytes o->{for(int i=-9;++i<9;)System.out.println("".join(" ".repeat(i<0?-i:i),"1234567890".split("")));} Try it online! -14 bytes by switching to JDK 11, which now has a ...

View answer
Output with the same length as the code
12 votes

Java (OpenJDK 8), 11 bytes o->1e8-1+"" Try it online! Output: 9.9999999E7 Just a tad more elaborate than the obvious answer, ()->"".format("%23s",0). Saves 18 -> 16 bytes: More ...

View answer
Sleep for 1000 years
10 votes

Java (JDK), 23 bytes v->Thread.sleep(7L<<42) Try it online! It's hard to find a short way to write a number in Java. Thread.sleep only accepts a long number of milliseconds. So the ...

View answer
Print all ASCII alphanumeric characters without using them
10 votes

Java (OpenJDK 9), 277 bytes Yes, Java, you read that well! $->""+($='='+'=')+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+--$+($='-'+'-')+--$+--$...

View answer
Sum of all integers from 1 to n
10 votes

Java (OpenJDK 8), 10 bytes n->n++*n/2 Try it online!

View answer
What is the standard scratch?
10 votes

Java (OpenJDK 8), 24 bytes (a,b,c,d)->a*3+b*4+c*5-d Try it online!

View answer
Lucky dice rolls
9 votes

AnyDice, 82 bytes function:l N K L{ifL<0{result:[lowestNof(N-L)dK]}else{result:[highestNof(N+L)dK]}} Try it online! For the output, check the "export" view and "summary" data and take the first ...

View answer
Leak Memory in as few bytes as possible
9 votes

Java 1.3, 23 bytes void l(){new Thread();} Creating a thread but not starting it. The thread is registered in the internal thread pool, but will never be started, so never ended and therefore never ...

View answer
I'm thinking of a number (Robber's thread)
9 votes

Java, by okx Note: the original challenge to which this post answers has been deleted, it's however still accessible with the link to anyone having the privilege to see deleted answers. The ...

View answer
Tips for golfing in Java
8 votes

Use Java 10's var If you define a single variable of a specific type, use var. Examples var i=0; // int var l=0L; // long var s=""; ...

View answer
Hack into a Lottery
8 votes

Java (JDK), 28 bytes v->Long.toUnsignedString(~0) Try it online!

View answer
Create an "H" from smaller "H"s
8 votes

Java (JDK), 126 bytes n->{int s=1,H[][]=new int[n+=Math.pow(3,n)-n][n],x;for(;s<n;s*=3)for(x=n*n;x-->0;)H[x/n][x%n]|=~(x/n/s%3)&x%n/s%3&1;return H;} Try it online! Returns an int[][...

View answer
"As a child, I was told Spiders felt no emotion."
8 votes

Java (JDK), 494 bytes n->{String r="",m="ZZO+():-\n'.\\/_| ";for(int b:"ýaÝøëaÜèîb¿bbïbbÏÿþèîbûbÿüëbÿübþèîbü¹­ÚŸošÝ©ËbþèîýÚŸûbÿïbüÿšÝþèîšbü¹wyiwyËbúžèî¿šüŸûÝýíýÜÿ›úŸÎèîþÿÊÿü¿ŸoŸËÿú¿...

View answer
Draw some expanding arrows
8 votes

Java (JDK), 81 bytes n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));} Try it online! Explanations n->{ // int-accepting consumer ...

View answer
Half, Half Half, and, Half
8 votes

Java (JDK 10), 30 bytes n->(n+.5)/n.highestOneBit(n)-1 Try it online! Returns the nth item in the sequence. This answer is originally a succession of golfs of TCFP's Java answer. In the end, ...

View answer
Blue duck, red duck, gray duck
8 votes

Java (OpenJDK 9), 133 bytes v->{for(int x=9;x>0;)System.out.println("Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")[x+=Math.random()*8-x]+" duck");} Try it online! Explanations ...

View answer
Fastest draw in the west!
7 votes

Java 11 (Oracle JDK) Just curious about how it runs. import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util....

View answer
1
2 3 4 5
12