[memo] Java FizzBuzz Code Golf

Java FizzBuzz Code Golf

Java1.4.2 (105char)

A.java


class B{static{for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i+"":"":"Buzz"));}}
$ java -version
java version "1.4.2_19"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_19-b04)
Java HotSpot(TM) Client VM (build 1.4.2_19-b04, mixed mode)
$ javac -version
javac: -version is an invalid flag.
How to use: javac <options> <source files>
The available options are:
  -g Generate all debug information
  -g:none Do not generate debug information
  -g:{lines,vars,source}Generate only some debug information
  -nowarn do not raise a warning
  -Print a message about the behavior of the verbose compiler
  -deprecation Prints the location of sources where deprecated APIs are used
  -classpath <path>Specify the location to search for user class files
  -sourcepath <path>Specify the location to search the input source file
  -bootclasspath <path>Replace the location of bootstrap class files
  -extdirs <dirs>Replace the location of installed extensions
  -d <directory>Specify the location to store the generated class file
  -encoding <encoding>Specifies the character encoding used by the source file
  -source <release>Maintain source compatibility with the specified release
  -target <release>Generate class files for a particular VM version
  -help Print a summary of standard options

$ echo -n 'class B{static{for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}' > A.java
$ javac A.java
A.java:1: ?Incompatible type for:Neither is a subtype of the other.
Second operand: int
Third operand: java.lang.String
class B{static{for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
                                                                                     ^
A.java:1: ?Incompatible type for:Neither is a subtype of the other.
Second operand: int
Third operand: java.lang.String
class B{static{for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
                                                                               ^
2 errors
$ echo -n 'class A{static{for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i+"":"":"Buzz"));}}' > A.java
$ wc -c
105 A.java
$ javac A.java
$ java A
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz
Exception in thread "main" java.lang.NoSuchMethodError: main

Java5~Java6 (96char)

A.java


enum A{B;int i;{for(;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
$ java -version
java version "1.5.0_22"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_22-b03)
Java HotSpot(TM) Client VM (build 1.5.0_22-b03, mixed mode, sharing)
$ javac -version
javac 1.5.0_22
javac:There is no source file.
How to use: javac <options> <source files>
The available options are:
  -g Generate all debug information
  -g:none Do not generate debug information
  -g:{lines,vars,source}Generate only some debug information
  -nowarn do not raise a warning
  -Print a message about the behavior of the verbose compiler
  -deprecation Prints the location of sources where deprecated APIs are used
  -classpath <path>Specify the location to search for user class files
  -cp <path>Specify the location to search for user class files
  -sourcepath <path>Specify the location to search the input source file
  -bootclasspath <path>Replace the location of bootstrap class files
  -extdirs <dirs>Replace the location of installed extensions
  -endorseddirs <dirs>Replace recommended standard path location
  -d <directory>Specify the location to store the generated class file
  -encoding <encoding>Specifies the character encoding used by the source file
  -source <release>Maintain source compatibility with the specified release
  -target <release>Generate class files for a particular VM version
  -version Version information
  -help Print a summary of standard options
  -X Print a summary of non-standard options
  -J<flag>                   <flag>Pass directly to the execution system

$ echo -n 'enum A{B;int i;{for(;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}' > A.java
$ wc -c A.java
96 A.java
$ javac A.java
$ java A
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz
Exception in thread "main" java.lang.NoSuchMethodError: main

Java7 (130char)

A.java


class A{public static void main(String[]a){for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) Client VM (build 24.80-b11, mixed mode, sharing)
$ javac -version
javac 1.7.0_80
$ echo -n 'enum A{B;int i;{for(;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}' > A.java
$ javac A.java
$ java A
error:The main method cannot be found in class A. Define the main method as follows:
   public static void main(String[] args)
$ echo -n 'class A{public static void main(String[]a){for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}' > A.java
$ wc -c A.java
130 A.java
$ javac A.java
$ java A
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz

Java8 or later (127char)

A.java


interface A{static void main(String[]a){for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}
$ java -version
java version "1.8.0_212"
Java(TM) SE Runtime Environment (build 1.8.0_212-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)
$ javac -version
javac 1.8.0_212
$ echo -n 'interface A{static void main(String[]a){for(int i=0;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"));}}' > A.java
$ wc -c A.java
127 A.java
$ javac A.java
$ java A
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz

Jshell (83char)

int i;for(;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"))
$ jshell --version
jshell 11.0.3
$ echo -n 'int i;for(;i++<100;)System.out.println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"))' | jshell -
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz

Jshell PRINTING version (72char)

int i;for(;i++<100;)println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"))
$ echo -n 'int i;for(;i++<100;)println((i%3>0?"":"Fizz")+(i%5>0?i%3>0?i:"":"Buzz"))' | jshell PRINTING -
1
2
Fizz
4
Buzz
(Omitted)
97
98
Fizz
Buzz

Recommended Posts

[memo] Java FizzBuzz Code Golf
[Memo] Test code summary
Python code memo for yourself
Difference between java and python (memo)