Author Archives: austin

Boolean Expressions

Sure, boolean variables are nice. Boolean expressions are even better. Below are a couple of examples.

Is one greater than two?
1 > 2
Of course not, so the answer is false.

Is two greater than one?
2 > 1
It is, so it must be true!

Is X greater than two?
x > 2
We can’t answer this until we know the value of x!

int x = 0;
boolean isXGTRThan = x > 2; // what's the value of isXGTRThan?

x = 2013;
isXGTRThan = x > 2// what's the value of isXGTRThan?

There are a number of operators that can be used in Boolean expression:

  • greater than: >
  • less than: <
  • equal to: ==
  • greater than or equal to: >=
  • less than or equal to: <=
int x = 5;

boolean expression = x > 2;
x = 3;

expression = x < 4; // what's the value of expression?
x = x; // what's the value of x?
expression = 5 >= x; // what's the value of expression?
expression = x == x; // what's the value of x and expression?

We can build even larger Boolean expressions with logical conjunctions and disjunctions.

  • The conjunction operator is: &&. This denotes a logical ‘and’.
  • The disjunction/disjoint operator is: ||. This denotes a logical ‘or’.
boolean one = true;
boolean two = true;
boolean three = false;

boolean expression = one && two; // what is the value of expression?

expression = one || three; // what is the value of expression?

expression = one && three; // what is the value of expression?

Let’s see how well you know this stuff. 🙂

boolean expression = ((true && false) || (3 < 2)); // what's the value of expression?

-> Branching Statements

Boolean

What is a boolean?

  • It’s a primitive data type. (So, it’s simpler than an object but it’s often used as one or more instance variables in a class.)
  • It can only have one of two values: true or false.
boolean austinsTeaching = true;

boolean mercerIslandRocks = true;

boolean theSeahawksWillLoseTheirNextGame = false;

-> Boolean expressions

Invoke a binary inside of .jar

I’m working on a Java GUI for an OCaml executable. I need to execute a binary that resides in a .jar. It turns out that binaries cannot be executed while in the .jar. The binaries have to be copied from the .jar and placed on the host machine. I chose to place the binaries in a temporary file. Then the .jar can use system commands to interact with binaries it needs to execute.

The following thread led me in this direction. Thank you, stackoverflow.com!

http://stackoverflow.com/questions/4764347/extract-and-load-dll-from-jar

Here is some example code to copy a file from your .jar to the host machine.

   

try {
    // Get a handle (as a stream) on the binary you want to move from the .jar to a folder on the system
    InputStream in = Toolkit.getDefaultToolkit().getClass().getResourceAsStream("/path/to/binary/in/jar");

    // Get a handle on where you want the binary to be saved.
    File fileOut = new File("/path/you/want/to/place/your/lib/binaryname");

    // make sure the path to the file exists
    fileOut.mkdirs();

    // Create an output stream to put data into.
    OutputStream out = new FileOutputStream(fileOut);

    // Copy the bytes from our input stream to our output stream.
    byte[] buf = new byte[1024];

    int len;

    while((len = in.read(buf)) &gt; 0) {

        out.write(buf, 0, len);

    }

    in.close();
    out.close();

} catch (Exception e) {
    //handle
}

After a successful copy you can then use the Runtime to execute your command.

  

try {
    String command = "path/to/binary -args";

    Process p = Runtime.getRuntime().exec(command);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    String s = null;

    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);

    }

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):n");

    while ((s = stdError.readLine()) != null) {
        System.err.println(s);
    }

}

catch(Exception e){
    //handle
}