Watch our 3-minute video to find out how you can learn Java with a live instructor.
Additional Resources

Comparisons And Flow Control Structures

In this lesson of the Java tutorial, you will learn...
  1. Understand boolean values and expressions
  2. Create complex boolean expressions using AND and OR logic
  3. Write flow control statements using if and if ... else structures
  4. Compare objects for equality and identity
  5. Write loops using while, do ... while, and for loop structures

Controlling Program Flow

Boolean-Valued Expressions

Java has a data type called boolean

  • possible values are true or false
  • can be operated on with logical or bitwise operators, but cannot be treated as a 0 or 1 mathematically
  • can be specified as a parameter type or return value for a method
  • a boolean value will print as true or false

The result of a conditional expression (such as a comparison operation) is a boolean value

  • simple comparisons, such as greater than, equal to, etc.
  • values returned from functions
  • complex combinations of simpler conditions

Condition expressions are used for program control

  • branching to another section of the program under specified conditions
  • repeating loops of the same section of code

Comparison Operators

This chart shows the comparison operators and the types of data they can be applied to
Operator Purpose (Operation Performed) Types of Data
boolean numeric primitives and char objects
== is equal to (note the two equals signs!) X X X
!= is not equal to X X X
> is greater than X
< is less than X
>= is greater than or equal to X
<= is less than or equal to X
  • note that it is unwise to use == or != with floating-point data types, as they can be imprecise

Comparing Objects

The operators == and != test if two references point to exactly the same object in memory - they test that the numeric values of the two references are the same

The equals(Object o) method compares the contents of two objects to see if they are the same (you can override this method for your classes to perform any test you want)

Conditional Expression Examples

a = 3; b = 4;

a == b will evaluate to false

a != b will evaluate to true

a > b will evaluate to false

a < b will evaluate to true

a >= b will evaluate to false

a <= b will evaluate to true

String s = "Hello";
String r = "Hel";
String t = r + "lo";

s == t will evaluate to false

s != t will evaluate to true (they are not the same object - they are two different objects that have the same contents)

s.equals(t) will evaluate to true

String s = "Hello";
String t = s;

s == t will evaluate to true

s != t will evaluate to false (they are the same object)

s.equals(t) will evaluate to true

Note: Java will intern a literal String that is used more than once in your code; that is, it will use the same location for all occurrences of that String

String s = "Hello";
String t = "Hello";

s == t will evaluate to true, because the String object storing "Hello" is stored only once, and both s and t reference that location

s != t will evaluate to false (they are the same object)

s.equals(t) will evaluate to true

Complex boolean Expressions

Java has operators for combining boolean values with AND and OR logic, and for negating a value (a NOT operation)

  • there are also bitwise operators for AND, OR and bitwise inversion (changing all 1 bits to 0, and vice versa)
  • since a boolean is stored as one bit, the bitwise AND and OR operators will give the same logical result, but will be applied differently (see below)
  • for some reason, the bitwise NOT cannot be applied to a boolean value
Logical Bitwise
&& logical AND & bitwise AND
|| logical OR | bitwise OR
! logical NOT ~ bitwise NOT (inversion)

Examples:

Code Effect
Testing if a value falls within a range

( ( a >= 0 ) && (a <= 10 ) )

is true if a is falls between 0 and 10; it must satisfy both conditions

Testing if a value falls outside a range

( ( a < 0 ) || (a > 10 ) )

is true if a falls outside the range 0 to 10; it may be either below or above that range

( !( ( a >= 0 ) && (a <= 10) ) )

inverts the test for a between 0 and 10; so a must be outside the range instead of inside it

The && and || operations are called short-circuiting, because if the first condition determines the final outcome, the second condition is not evaluated

  • to force both conditions to be evaluated, use & and | for AND and OR, respectively

Example: to test if a reference is null before calling a boolean valued method on it

String s = request.getParameter("shipovernite");
if (s != null && s.equalsIgnoreCase("YES")) { . . . }

equalsIgnoreCase will only be called if the reference is not null

Simple Branching

The result of a conditional expression can be used to control program flow

if Statement

Syntax
if (condition) statement;

or

Syntax
if (condition) {
 zero to many statements;
}
  • causes "one thing" to occur when a specified condition is met
  • the one thing may be a single statement or a block of statements in curly braces
  • the remainder of the code (following the if and the statement or block it owns) is then executed regardless of the result of the condition.

The conditional expression is placed within parentheses

The following flowchart shows the path of execution:

If Statement Flowchart

if Statement Examples

Absolute Value

If we needed the absolute value of a number (a number that would always be positive):

Code Sample: Java-Control/Demos/If1.java

public class If1 {
 public static void main(String[] args) {
  int a = 5;
  System.out.println("original number is: " + a);
  if ( a < 0 ) { a = -a; }
  System.out.println("absolute value is: " + a);
  a = -10;
  System.out.println("original number is: " + a);
  if ( a < 0 ) { a = -a; }
  System.out.println("absolute value is: " + a);
 }
}

Random Selection

Task: write a brief program which generates a random number between 0 and 1. Print out that the value is in the low, middle, or high third of that range (Math.random() will produce a double value from 0.0 up to but not including 1.0).

  • although it is a bit inefficient, just do three separate tests: (note that Math.random() will produce a number greater than or equal to 0.0 and less than 1.0)

Code Sample: Java-Control/Demos/If2.java

public class If2 {
 public static void main(String[] args) {
  double d = Math.random();
  System.out.print("The number " + d);
  if (d < 1.0/3.0) 
   System.out.println(" is low");
  if (d >= 1.0/3.0 && d < 2.0/3.0) 
   System.out.println(" is middle");
  if (d >= 2.0/3.0) 
   System.out.println(" is high");
 }
}

Exercise: Game01: A Guessing Game

Duration: 15 to 20 minutes.

Write a program called Game that will ask the user to guess a number, and compare their guess to a stored integer value between 1 and 100.

  1. Use a property called answer to store the expected answer
  2. For now, just hard-code the stored value, we will create a random value later (your code will be easier to debug if you know the correct answer)
  3. Create a method called play() that holds the logic for the guessing
  4. Create a main method, have it create a new instance of Game and call play()
  5. Use the KeyboardReader class to ask for a number between 1 and 100, read the result, and tell the user if they are too low, correct, or too high

Exercise: Payroll-Control01: Modified Payroll

Duration: 15 to 20 minutes.
  1. Modify your payroll program to check that the pay rate and department values the user enters is greater than or equal to 0
  2. The Employee class should protect itself from bad data, so modify setPayRate and setDept to check the incoming data (and ignore it if it is less than 0)
  3. Test your program to see what happens with negative input values
  4. The code using Employee should avoiding sending it bad data, so also change main to check for pay rate and department values (print an error message for any negative entry, ane replace it with a value of 0 - later we will find a way to ask the user for a new value instead)

Two Mutually Exclusive Branches

if ... else statement

Syntax
if (condition) statement; or block
else statement; or block
  • does "one thing" if a condition is true, and a different thing if it is false
  • it is never the case that both things are done
  • the "one thing" may be a single statement or a block of statements in curly braces
  • a statement executed in a branch may be any statement, including another if or if ... else statement.

If ... Else Statement Flowchart

This program tells you that you are a winner on average once out of every four tries:

Code Sample: Java-Control/Demos/IfElse1.java

public class IfElse1 {
 public static void main(String[] args) {
  double d = Math.random();
  if (d < .25) System.out.println("You are a winner!");
  else System.out.println("Sorry, try again!");
 }
}

Nested if ... else Statements - Comparing a Number of Mutually Exclusive Options

Nested if ... else statements

  • the else clause can contain any type of statement, including another if or if ... else
  • you can test individual values or ranges of values
  • once an if condition is true, the rest of the branches will be skipped
  • you could also use a sequence of if statements without the else clauses (but this doesn't by itself force the branches to be mutually exclusive)

Here is the low/middle/high example rewritten using if ... else

Code Sample: Java-Control/Demos/IfElse2.java

public class IfElse2 {
 public static void main(String[] args) {
  double d = Math.random();
  System.out.print("The number " + d);
  if (d < 1.0/3.0) 
   System.out.println(" is low");
  else if (d < 2.0/3.0) 
   System.out.println(" is middle");
  else 
   System.out.println(" is high");
 }
}
  • note that we only need one test for the middle value - if we reach that point in the code, d must be greater than 1.0/3.0
  • similarly, we do not any test at all for the high third
  • the original version worked because there was no chance that more than one message would print - that approach is slightly less efficient because all three tests will always be made, where in the if ... else version comparing stops once a match has been made.

Exercise: Game02: A Revised Guessing Game

Duration: 15 to 20 minutes.
  1. Revise your number guessing program to use if . . . else logic (you can test for too low and too high, and put the message for correct in the final else branch)
  2. Once you have done that, here is a way to generate a random answer between 1 and 100:
    1. at the top:
      import java.util.*;
    2. then, in a constructor, use:
      Random r = new Random();
      answer = r.nextInt(100) + 1;
    3. the nextInt(int n) method generates a number greater than or equal to 0 and less than n, so r.nextInt(100) would range from 0 through 99; we need to add 1 to raise both ends of the range
    4. you might want to print the expected correct answer to aid debugging

Note that until we cover looping, there will be no way to truly "play" the game, since we have no way to preserve the value between runs

Comparing a Number of Mutually Exclusive Options - The switch Statement

switch statement

  • a switch expression (usually a variable) is compared against a number of possible values
  • used when the options are each a single, constant, value that is exactly comparable (called a case)
  • the switch expression must be a byte, char, short, or int
  • cases may only be byte, char, short, or int values, in addition, their magnitude must be within the range of the switch expression data type
  • cannot be used with floating-point datatypes or long
  • cannot compare an option that is a range of values, unless it can be stated as a list of possible values, each treated as a separate case
  • cases are listed under the switch control statement, within curly braces, using the case keyword
  • once a match is found, all executable statements below that point are executed, including those belonging to later cases - this allows stacking of multiple cases that use the same code
  • the break; statement is used to jump out of the switch block, thus skipping executable steps that are not desired
  • the default case keyword catches all cases not matched above
  • note that, technically speaking, the cases are labeled lines; the switch jumps to the first label whose value matches the switch expression

Usage

Syntax
switch ( expression ) {
 case constant1:  
  statements;
  break;
 case constant2:
   statements;
  break;
 . . .
 case constant3:  
 case constant4MeansTheSameAs3:
   statements;
  break;
 . . .
 [default:
  statements;]
}

switch Statement Examples

Example - code that tests a character variable against several possible cases

Code Sample: Java-Control/Demos/Switch1.java

import util.KeyboardReader;

public class Switch1 {
 public static void main(String[] args) {
  int c = 'X';
  c = KeyboardReader.getPromptedChar("Enter a letter a - d: ");
  switch (c) {
   case 'a':
   case 'A': System.out.println("A is for Aardvark");
        break;
   case 'b':
   case 'B': System.out.println("B is for Baboon");
        break;
   case 'c':
   case 'C': System.out.println("C is for Cat");
        break;
   case 'd':
   case 'D': System.out.println("D is for Dog");
        break;
   default:  System.out.println("Not a valid choice");
  }
 }
}

Points to note:

  • stacking of cases for uppercase and lowercase letters allows both possibilities
  • break; statements used to separate code for different cases
  • default: clause used to catch all other cases not explicitly handled

Another example - taking advantage of the "fall-though" behavior without a break statement

Code Sample: Java-Control/Demos/Christmas.java

import util.KeyboardReader;

public class Christmas {
 public static void main(String[] args) {
  int day = KeyboardReader.getPromptedInt("What day of Christmas? ");
  System.out.println(
    "On the " + day + " day of Christmas, my true love gave to me:");
  switch (day) {
   case 12: System.out.println("Twelve drummers drumming,"); 
   case 11: System.out.println("Eleven pipers piping,"); 
   case 10: System.out.println("Ten lords a-leaping,"); 
   case 9: System.out.println("Nine ladies dancing,"); 
   case 8: System.out.println("Eight maids a-milking,"); 
   case 7: System.out.println("Seven swans a-swimming,"); 
   case 6: System.out.println("Six geese a-laying,"); 
   case 5: System.out.println("Five golden rings,"); 
   case 4: System.out.println("Four calling birds,"); 
   case 3: System.out.println("Three French hens,"); 
   case 2: System.out.println("Two turtle doves, and a "); 
   case 1: System.out.println("Partridge in a pear tree!");
  }
 }
}

Exercise: Game03: Multiple Levels

Duration: 15 to 30 minutes.

What if we want to offer gamers multiple levels of difficulty in our game? We could make the range multiplier a property of the Game class, and set a value into it with a constructor, after asking the user what level they'd like to play.

  1. Add a range property to the Game class (an int)
  2. Add a new constuctor that accepts a level parameter; use a char
  3. In the new constructor, create another new KeyboardReader (we will come up with a better solution later, where both main and play can share one KeyboardReader)
  4. use a switch to process the incoming level:
    • uppercase or lowercase B means Beginner, set the range to 10
    • I means Intermediate, set the range to 100
    • A means Advanced, set the range to 1000
    • any other value results in beginner - after all, if they can't answer a simple question correctly, how could we expect them to handle a higher level game?
    • you could put the default option stacked with the 'B' cases to accomplish this
    • better yet, make it the first of that group, so that you can print an error message and then fall through to the 'B' logic
  5. Modify the default constructor to call this new constructor, passing 'I' for intermediate
  6. In the main method, ask the user for the level and call the new constructor with their response
  7. Use range as the parameter when you call the Random object's nextInt method

Comparing Objects

When comparing two objects, the == operator compares the references, to see if they are the same object (i.e., occupying the same spot in memory)

  • and != tests to see if they are two different objects (even if they happen to have the same internal values)

The Object class defines an equals(Object) method intended to compare the contents of the objects

  • the code written in the Object class simply compares the references using ==
  • this method is overridden for most API classes to do an appropriate comparison
  • for example, with String objects, the method compares the actual characters up to the end of the string
  • for classes you create, you would override this method to do whatever comparisons you deem appropriate

Code Sample: Java-Control/Demos/Rectangle.java

public class Rectangle {
 private int height;
 private int width;
 
 public Rectangle(int height, int width) {
  this.height = height;
  this.width = width;
 }
 
 public boolean equals(Object other) {
  if (other instanceof Rectangle) {
   Rectangle otherRect = (Rectangle) other;
   return this.height == otherRect.height && this.width == otherRect.width;
  }
  else return false;
 }
}
Code Explanation

The equals method compares another object to this object.

This example is necessarily somewhat complicated. It involves a few concepts we haven't covered yet, including inheritance. Because the equals method inherited from Object takes a parameter which is an Object, we need to keep that that type for the parameter (technically, we don't need to do it by the rules of Java inheritance, but because other tools in the API are coded to pass an Object to this method). But, that means that someone could call this method and pass in something else, like a String.

So, the first thing we need to do is test that the parameter was actually a Rectangle. If so, we can work with it - if not, there is no way it could be equal, so we return false.

We will cover instanceof later, but, for now, we can assume it does what it implies - test that the object we received was an instance of Rectangle. Even after that test, in order to treat it as a Rectangle, we need to do a typecast to explicitly store it into a Rectangle variable (and again, we will cover object typecasting later). Once we have it in a Rectangle variable, we can check its height and width properties.

As an aside, note that private data in one instance of a class is visible to other instances of the same class - it is only other classes that cannot see the private elements.

Code Sample: Java-Control/Demos/ObjectEquivalenceIdentity.java

class ObjectEquivalenceIdentity {
 
 public static void main(String[] args) {
  Rectangle r1 = new Rectangle(10, 20);
  Rectangle r2 = new Rectangle(10, 20);
  if (r1 == r2)
   System.out.println("Same object");
  else
   System.out.println("Different objects");
  if (r1.equals(r2))
   System.out.println("Equal");
  else
   System.out.println("Not equal"); 
 }
}
Code Explanation

Since all the important work is done in Rectangle, all we need to do here is instantiate two and compare them using both == and the equals method to see the differing results.

The output should be:

Different objects
Equal

Testing Strings for Equivalence

Example - write a brief program which has a word stored as a string of text. Print a message asking for a word, read it, then test if they are the same or not.

  • you can preset the message assuming that they are incorrect, then change it if they are correct

Code Sample: Java-Control/Demos/StringEquals.java

import util.KeyboardReader;

public class StringEquals {
 public static void main(String[] args) {
  String s = "Hello";
  String t = KeyboardReader.getPromptedString("Enter a string: ");
  String message = "That is incorrect";
  if (s.equals(t)) message = "That is correct";
  System.out.println(message);
 }
}
  • try this, then change equals(t) to equalsIgnoreCase(t)

Conditional Expression

Java uses the same conditional expression as C and C++

Syntax
condition ? expressionIfTrue : expressionIfFalse

This performs a conditional test in an expression, resulting in the first value if the condition is true, the second if the condition is false

Note: due to operator precedence issues, it is often best to enclose the entire expression in parentheses

Example

Code Sample: Java-Control/Demos/Conditional.java

public class Conditional {
 public static void main(String[] args) {
  int i = -6;
  String s;
  s = "i is " + ((i >= 0) ? "positive" : "negative");
  System.out.println(s);
 }
}

Note that the parentheses around the test are not necessary by the operator precedence rules, but may help make the code clearer

The parentheses around the entire conditional expression are necessary; without them, precedence rules would concatenate the boolean result onto the initial string, and then the ? operator would be flagged as an error, since the value to its left would not be a boolean.

while and do . . . while Loops

Again, the loops in Java are pretty much the same as in C - with the exception that the conditional expressions must evaluate to boolean values

while loops

Syntax
while (condition) statement;

or

Syntax
while (condition){
 block of statements 
}
  • While Loop Flowchart the loop continues as long as the expression evaluates to true
  • the condition is evaluated before the start of each pass
  • it is possible that the body of the loop never executes at all (if the condition is false the first time)

do ... while loops

Syntax
do statement; while (condition);

or

Syntax
do {
 block of statements
} while (condition);
  • Do ... While Loop Flowchart the condition is evaluated after the end of each pass
  • the body of the loop will always execute at least once, even if the condition is false the first time

for Loops

A for loop uses a counter to progress through a series of values

  • a value is initialized, tested each time through, and then modified (usually incremented) at the end of each pass, before it is tested again
  • the for loop does not do anything that cannot be done with a while loop, but it puts everything that controls the looping at the top of the block

For Loop Flowchart

Syntax
for (initialize; condition; change)
 statement;

or

Syntax
for (initialize; condition; change) {
 block of statements
}

for loops often use a variable with block-level scope (existing only for the duration of the loop), created in the control portion of the loop

int j;
for (j = 0; j < 12; j++ )
 System.out.print("j = " + j);
for (int j = 0; j < 12; j++ )
 System.out.print("j = " + j);

A for loop may use multiple control variables by using the sequence operator, the comma ( , )

for (int j = 0, k = 12; j <= 12; j++, k-- )
 System.out.println(j + " " + k);

Note: if you use a block-scope variable (such as above) for the first counter, the additional ones will be block scope as well, and also will be the same type of data - i.e., the variable k above also exists only for the duration of the block. There is no way to declare two counters of different types at block-level scope..

ForEach Loops

Java 5 introduced a new type of loop, the for-each loop. When you have an array or collection class instance, you can loop through it using a simplified syntax

Syntax
for (type variable : arrayOrCollection) {
 body of loop
}

The looping variable is not a counter - it will contain each element of the array or collection in turn (the actual value, not an index to it, so its type should be the same as the type of items in the array or collection). You can read the : character as if it was the word "from". We will cover this type of loop in more depth in the Arrays section.

For some reason, the looping variable must be declared within the parentheses controlling the loop - you cannot use a preexisting variable.

Code Sample: Java-Control/Demos/Loops1.java

public class Loops1 {
 public static void main(String[] args) {
  int i = 1;
  
  System.out.println("While loop:");
  while (i <= 512) {
   System.out.println("i is " + i);
   i *= 2;
  }
  System.out.println("i is now " + i);
  
  System.out.println("Do while loop:");
  do {
   i = i - 300;
   System.out.println("i is now " + i);
  }
  while (i > 0);
  
  System.out.println("For loop:");
  for (i = 0; i < 12; i++) 
   System.out.print(" " + i);
  System.out.println();
  
  System.out.println("For loop that declares a counter:");
  for (int j = 0; j < 12; j++) 
   System.out.print(" " + j);
  System.out.println();
  
  System.out.println("ForEach loop:");
  String[] names = { "Jane", "John", "Bill" };
  for (String oneName : names) 
   System.out.println(oneName.toUpperCase());
 }
}

Exercise: Payroll-Control02: Payroll With a Loop

Duration: 20 to 40 minutes.
  1. Revise your payroll program to use only one Employee variable
  2. Use a loop to repeatedly create a new instance to store in it, populate it from the keyboard and display it on the screen (you can ask after each one if the user wants to enter another, or just use a loop that has a fixed number of iterations)
  3. Also, change the logic for reading the salary and department values from the keyboard to use do . . . while loops that will continue to loop as long as the user enters invalid values

Exercise: Game04: Guessing Game with a Loop

Duration: 10 to 20 minutes.

(Optional - if time permits)

  1. Revise the number guessing program to force the user to guess until they are correct (loop-wise, to keep guessing as long as they are incorrect).
  2. Then add more logic to ask if they want to play again, read a Y or N as their answer, and loop as long as they enter a Y

Additional Loop Control: break and continue

Breaking Out of a Loop

The break statement will end a loop early

  • execution jumps to the first statement following the loop
  • the following example prints random digits until a random value is less than 0.1

Code Sample: Java-Control/Demos/Break.java

public class Break {
 public static void main(String[] args) {
  for ( ; ; ) { // creates an infinite loop
          // no initialization, no test, no increment
   double x = Math.random();
   if (x < 0.1) break;
   System.out.print((int) (x * 10) + " ");
  }
  System.out.println("Done!");
 }
}
Code Explanation

This code loops, generating and printing a random number for each iteration. If the number is less than 0.1, we break out before printing it.

Code Sample: Java-Control/Demos/BreakNot.java

Code Explanation

This code avoids the break, by creating and testing the random number in the control part of the loop. As part of the iteration condition, if the number is less than 0.1, the loop simply ends "normally".

Continuing a Loop

If you need to stop the current iteration of the loop, but continue the looping process, you can use the continue statement

  • normally based on a condition of some sort
  • execution skips to the end of this pass, but continues looping
  • it is usually a better practice to reverse the logic of the condition, and place the remainder of the loop under control of the if statement

Example - a program to enter 10 non-negative numbers

Code Sample: Java-Control/Demos/Continuer.java

import util.*;

public class Continuer {
 public static void main(String[] args) throws IOException {
  int count = 0;
  do {
   int num = KeyboardReader.getPromptedInt("Enter an integer: ");
   if (num < 0) continue;
   count++;
   System.out.println("Number " + count + " is " + num);
  } while (count < 10);
  System.out.println("Thank you");
 }

/*
 // a better way

 public static void main(String[] args) throws IOException {
  int count = 0;
  do {
   int num = KeyboardReader.getPromptedInt("Enter an integer: ");
   if (num >= 0) {
    count++;
    System.out.println("Number " + count + " is " + num);
   }
  } while (count < 10);
  System.out.println("Thank you");
 }
*/

}

A better way to handle the loop is shown in the commented out version of main - try removing the comment marks and commenting out the original method

But, continue is easier to use in nested loops, because you can label the level that will be continued

break and continue in Nested Loops

In normal usage, break and continue only affect the current loop; a break in a nested loop would break out of the inner loop, not the outer one

But, you can label a loop, and break or continue at that level

  • a label is a unique identifier followed by a colon character

Try the following example as is, then reverse the commenting on the break lines

Code Sample: Java-Control/Demos/BreakOuter.java

public class BreakOuter {
 public static void main(String[] args) {

  outer: for (int r = 0; r < 10; r++) {
   for (int c = 0; c < 20; c++) {
    double x = Math.random();

    //if (x < 0.02) break;
    if (x < 0.02) break outer;
    System.out.print((int) (x * 10) + " ");
   }
   System.out.println();
  }
  System.out.println("Done!");
 }
}

Classpath, Code Libraries, and Jar files

By now, you may have noticed that every demo and solution folder contains its own copy of util and KeyboardReader. Not only is it inefficient, but it means that we would have to locate and update each copy if we wanted to change KeyboardReader.

A better solution would be to have one master copy somewhere that all of our exercises could access.

Using CLASSPATH

Java has a CLASSPATH concept that enables you to specify multiple locations for .class files, at both compile-time and runtime.

  • by default, Java uses rt.jar in the current Java installation's lib directory, and assumes that the classpath is the current directory (the working directory in an IDE)
  • if you create a classpath, the default one disappears, so any classpath that you create must include the current directory using a period (.) character

To use an external library, you would need to create a classpath in one of two ways:

  1. you can create a system or user environment variable, with multiple directories and/or .jar files, separated by the same delimiter used by your PATH (a semicolon in Windows)
  2. you could use the -classpath option in java and javac (-cp is a shorthand version of that)

Here is an example of a pair of commands to compile and run using an external library stored in a jar file. Note that we need the jar file at both compile-time and runtime. The -cp option in both commands replaces the system classpath with one specifically for that command.

javac -cp c:\Webucator\Java102\ClassFiles\utilities.jar;. *.java
java -cp c:\Webucator\Java102\ClassFiles\utilities.jar;. Payroll

Many Integrated Developments (IDEs) have a means to specify project properties; usually an item like "Build Path" will have options to specify external jar files (as well as choose from various libraries supplied with the environment).

Creating a jar File (a Library)

If you wish to create your own library of useful classes, you can bundle one or more classes and/or directories of classes in a jar file

  • you can also add other resources such as image files

A jar file contains files in a zipped directory structure

  • the root level of the file structure should match the root of the package structure, for example, to put KeyboardReader in a jar file, we would want to start at the directory where util is visible, and jar that

The following command will create a jar file called utilities.jar for all files in the util package (just KeyboardReader, in this case)

jar -cvf utilities.jar util\*.class
  • the options are create, verbose, and use a list of files supplied at the end of the command

Exercise: Creating and Using an External Library

Duration: 5 to 10 minutes.
  1. Run the command shown above to create utilities.jar
  2. Move utilities.jar to the ClassFiles directory in your filesystem
  3. If you remove the util directory from Payroll-Control01, you will not be able to compile or run Payroll as we have been doing
  4. But, you should be able to compile and run by specifiying a classpath option as in the commands shown above
  5. You can also temporarily modify the classpath for the duration of your command prompt window as follows (note that you may need to modify the line below to match your setup):
    set CLASSPATH=%CLASSPATH%;c:\Webucator\Java102\ClassFiles\utilities.jar

Compiling to a Different Directory

You can supply a destination directory to the Java compiler, in order to keep your source and compiled files separate:

Syntax
javac -d destination FileList

The destination directory can be a relative path from the current directory, or an absolute path. It must already exist, but an subdirectories necessary for package structures will be created.

Comparisons And Flow Control Structures Conclusion

In this section we have learned about boolean-valued expressions, and used them to control branches and loops.

We also learned how to create and use jar file libraries.

To continue to learn Java go to the top of this page and click on the next lesson in this Java Tutorial's Table of Contents.
Last updated on 2009-02-26

Use of http://www.learn-java-tutorial.com (Website) implies agreement to the following:

Copyright Information

All pages and graphics on Website are the property of Webucator, Inc. unless otherwise specified.

None of the content on Website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of pages or content on Website

This content may not be printed or saved. It is for online use only.


Linking to Website

You may link to any of the pages on Website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

Website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).


For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm