Printing Gray code through modifying Samuel Beckett's stage instructions
I have some suggestions for your code.
Always add curly braces to loop &amp; if
In my opinion, it's a bad practice to have a block of code not surrounded by curly braces; I saw so many bugs in my career related to that, if you forget to add the braces when adding code, you break the logic / semantic of the code.
Use java.lang.StringBuilder to concatenate String in a loop.
It's generally more efficient to use the builder in a loop, since the compiler is unable to make it efficient in a loop; since it creates a new String each iteration. There are lots of good explanations with more details on the subject.
before
String s = &quot;&quot;;
for (int i = 0; i &lt; n; i++) {
   s += &quot;0&quot;;
}

after
StringBuilder s = new StringBuilder();
for (int i = 0; i &lt; n; i++) {
   s.append(&quot;0&quot;);
}

Don’t use a loop when you want to repeat a string of x elements
The API that java give you offer a method to repeat the character x times; you can use java.lang.String#repeat (Java 11+)
before
StringBuilder s = new StringBuilder();
for (int i = 0; i &lt; n; i++) {
   s.append(&quot;0&quot;);
}

after
String s = &quot;0&quot;.repeat(n);

Extract the expression to variables when used multiple times.
In your code, you can extract the similar expressions into variables; this will make the code shorter and easier to read.
before
beckett(s, n - 1);
System.out.println(swap(s, &quot;1&quot;, n));
s = swap(s, &quot;1&quot;, n);
beckett(s, n - 1);

after
int previousNumber = n - 1;
beckett(s, previousNumber);
System.out.println(swap(s, &quot;1&quot;, n));
s = swap(s, &quot;1&quot;, n);
beckett(s, previousNumber);

Uses the increment and decrement operators when possible
Try to use the x++ / ++x, x-- or --x instead of x = x + 1 since they are more widely used and make the code easier to read.
before
n = n - 1;

after
n--;