Search
This is a list of golfing tips for the language known as Java. If you have a tip, add it in!
Use for(;;) instead of while(true) The title's self-explanatory. A byte saver.
Abuse the C-like array syntax If you ever have to declare two variables, one of type X and the other of type X[], instead of two statements, you can declare them together using this atrocious synt...
Abuse anonymous classes Warning: This only works in very specific circumstances, and I've never actually needed it. Instead of defining a method outside of your method, you can make an instance o...
Use interface The special thing about this is that it lets you drop public when you originally call a class to run the main function. class F{public static void main(String[]a{}} interface M{sta...
Java (JDK), 311 301 296 bytes interface A{static void main(String[]x){String a=" bottle",b=" of beer",c=" on the wall. ",e;for(int i=99;i>0;i--){e=i>1?"s":"";System.out.println(i+a+e+b+c+(...
JavaScript, 28 bytes alert(`Hello, ${prompt()}!`) Try it online!
AWK, 72 bytes BEGIN{for(i=1;i<101;i++){s="";i%3||s="Fizz";i%5||s=s"Buzz";print s?s:i}} Try it online!
Use pointer arithmetic and \0 with string literals In C, strings are just zero-terminated character arrays, which means you can play tricks with pointer arithmetic. For example, the line char*s=a...
JavaScript (Node.js), 160 bytes (r,d,n,l=r.length,s=d.length,p=(n,i)=>n?"01110110"[4*p(--n,i-1)+2*p(n,i)+1*p(n,i+1)]:i>=0&i<s?d[i]:r[(i%l+l)%l])=>[...Array(4*l+s+2*n)].map((_,i)=...
C (gcc), 64 62 bytes Saved 2 bytes thanks to m90 in the comments. i;f(char*s){i=atoi(s)>>4;return'A'+(i&8?i&4?i&2?i%2+3:2:1:0);} Try it online!
Ruby, 58 bytes Simple eval solution. Using ** for exponentiation. ->a,b,c{%w[+ - * / % **].select{eval("#{a}#{_1}#{b}")==c}} Attempt This Online!
Ruby, 27 bytes ->n{(2..n).all?{n%_1**2>0}} Try it online
Ruby, 25 bytes ->e{e.to_c*e[/ .*/].to_c} Try this online!
Ruby, 75 72 bytes f=->((e,*r),n){(i=n.index e)?f[r[0,i],n[...i]]+f[r[i..],n[i+1..]]+[e]:n} Attempt This Online!
Ruby, 50 bytes ->n{n.times{puts (' '*(n-_1)+'_/'+'##'*_1)[3..]}} Attempt This Online!
Scala 3, 553 bytes package a import E.Z as Z import E.* sealed trait T open class Y{export E.Z as Q} enum E[+A]derives CanEqual: case Z end E private abstract final class X extends AnyRef...
JS, 15 bytes !![]/(-~[]-~[]) Basically typed random characters into js console and it worked.
Javascript, 5 Distinct characters, 9 Bytes ~[]/~-~[]
Japt, 1 byte ½ Test it Jelly, Vyxal, 1 byte . Try it online! Yeah, golf langs still kind of kill this challenge.
Scala, 15 bytes '#'.toFloat/'F' Try it in Scastie! Characters in Scala kinda get casted to integers. This is usually annoying, but it's useful when golfing. # is 35 and F is 70, and the .toFlo...
Raku (also Perl polyglot), 5 unique, 13 bytes !()/(!()+!()) () or empty list coerces to its length which is 0 in math operations and !0 is True which coerces to 1
Ruby, 10 distinct, 35 chars t=/%/=~%/=%/ o=/%/=~%/==%/ t/o.to_f
Julia 1.0, 16 bytes (o=one(Int))/-~o Try it online!