Printing all binary strings of length n I have completed my homework with directions as follows:


  Declare and implement a class named Binary. This class will have a
  method named printB(int n) that prints all binary strings of length n.
  For n = 3, it will print


000
001
010
011
100
101
110
111



  in this order.


Here is my code:

import java.util.Scanner;
class Binary
{

    String B;
    int temp;

    void printB(int n)
    {
        for(int i = 0; i &lt; Math.pow(2,n); i++)
        {
            B = "";
            int temp = i;
            for (int j = 0; j &lt; n; j++)
            {
                if (temp%2 == 1)
                    B = '1'+B;
                else
                    B = '0'+B;
                    temp = temp/2;
            }
            System.out.println(B);
         }
    } 
}

class Runner
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter n:");
        int n = in.nextInt();

        Binary myB = new Binary();

        myB.printB(n);
    }
}


My question is...is there anyway to make this shorter or more efficient?
Speaking from a straight Java perspective, one thing you can do is change your loop header. You evaluate the power every iteration and could do something like:

for (int i = 0, end = Math.pow(2, n); i &lt; end; i++)


You also use standard String concatenation which is slow because once compiled, when you concatenate two strings a StringBuffer is created, and both strings are added to it, and then resulting string is returned. You save a step by using a StringBuffer from the get go which would you replace B = ""; with B = new StringBuffer(); (of course changing it's type accordingly). Instead of adding strings to you call insert (which can be chained) like B.insert(0, "1")

You're appending a char ('0' or '1') which you evaluate with modular division, you can just prepend it like B.insert(0, (temp % 2)) and save that entire if.

I probably said more than I should have for homework, but you appear to have already solved your problem and so I was just giving advice in addition to that.

EDIT

My solution would be as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number &gt;&gt; ");
        int value = in.nextInt();
        in.close();
        Binary binary = new Binary(value);
        System.out.println(binary.getResult());
    }
}

class Binary {
    private int value = 0;
    private StringBuilder binaryValues;

    public Binary(int value) {
        this.value = value;
        this.binaryValues = new StringBuilder();
        this.process();
    }

    private void process() {
        for (int i = 0, end = (1 &lt;&lt; this.value); i &lt; end; i++) {
            StringBuilder binary = new StringBuilder(Integer.toString(i, 2));
            while (binary.length() &lt; this.value)
                binary.insert(0, 0);
            this.binaryValues.append(binary).append("\n");
        }
    }

    public String getResult() {
        return this.binaryValues.toString();
    }
}


Maybe not shorter but no need to rewrite binary number generation when it's built in.