Numbers and Strings


Number and String Operators

/**
 * Numbers and Strings Example #4
 *
 * @author Alyce Brady, Pamela Cutter
 */
public class NumbersAndStringsEx4
{
    /** The main function initiates execution of this program. */
    public static void main()
    {
        System.out.println (2 + 3);
        System.out.println (2 + 3 + 4);  // What value do you expect?
        System.out.println (2 + 3 * 6);  // What value do you expect?
    }
}
Output:
    5
    9
    20 

/**
 * Numbers and Strings Example #5
 *
 * @author Alyce Brady, Pamela Cutter
 */
public class NumbersAndStringsEx5
{
    /** The main function initiates execution of this program. */
    public static void main()
    {
        System.out.println (2 + 3 * 6);  // What value do you expect?
        System.out.println ((2 + 3) * 6);  // What value do you expect?
        System.out.println (2 + (3 * 6));  // What value do you expect?
    }
}
Output:
    20
    30
    20 

Basic Operators on Numbers

    +   -   *   /

    %     (integers only)

Basic Operator on Strings

    +     (string concatenation)

/**
 * Numbers and Strings Example #6
 *
 * @author Alyce Brady, Pamela Cutter
 */
public class NumbersAndStringsEx6
{
    /** The main function initiates execution of this program. */
    public static void main()
    {
        System.out.println ("Hello, " + "world!");  // What value do you expect?
        System.out.println ("Hello" + "world!");  // What value do you expect?
    }
}
Output:
    Hello, world!
    Helloworld!

Alyce Brady, Kalamazoo College