java – Implementation of Scytale cipher

I have some suggestions for your 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 optimize it by itself while translating your code into bytecode; The compiler will not use the java.lang.StringBuilder in complex loops and your method will take more time and more memory to execute, since the String Object is immutable (a new instance will be created each iteration).

StringBuilder encrypted = new StringBuilder();
for (int i = 0; i < kth; i++) {
   for (int j = 0; i + j < textLength; j += kth) {
      encrypted.append(text.charAt(i + j));
return encrypted.toString();

When concatenating a primitive into a string, you don’t need to convert it.

You can add the primitive (int, double, float, char, ect) directly into the string by using the assignment operators (+, +=).


encrypted += String.valueOf(text.charAt(i+j));


encrypted += text.charAt(i+j);