bembry.org
Home / Technology / Java

Java Lesson 2: Variables and Numbers

  • Variables and Comments
    • Variables are statically typed.
    • A variable can start with any letter, an underscore _ or a dollar sign $ .
    • Declare a variable by giving the type, then the variable name:
      int aNumber
    • Line comments begin with //
    • Block comments are enclosed between /* and */
  • Numerical Data Types
    • The range of values stored in each type is independent of your computer platform and architecture.
    • If you put a zero at the beginning of a number, Java will interepret it as being an octal value instead of an integer. Don't do that.
    • byte : A 1-byte number (+- 127)
    • short : A 2-byte number (+- 32,767)
    • int : A 4-byte number (+- 2 billion)
    • long : An 8-byte number (+- 9 quintillion). Requires "L" at end of number, ie long bigNum = 123465789L
    • float : A 4-byte decimal number (+- 3.4E38). Requires "F" at end of number, ie float aNum = 1234.56F
    • double : An 8-byte decimal number (+- 1.7E308)
  • Working with Numerical Types
    • You can declare multiple variables of the same type on a single line by separating the declarations with commas.
      int numDogs = 5, numCats = 10, numSnakes = 12
    • Numercial calculations use the following operators:
      • + Add
      • - Subtract
      • * Multiply
      • / Divide
      • % Modulus (remainder after division)
      • ++num : Increment num before calculating the expression
      • --num : Decrement num before calculating the expression
      • num++ : Increment num after calculating the expression
      • num-- : Decrement num after caluculating the expression
    • Calculations using short or byte numbers will result in int numbers.
    • Calculations involving a long will result in a long.
    • Calculations involving a float or double will result in float or double.
    • In order for division to return a decimal value, you must use at least one float or double in the calculation.
  • Sample Code
                public class FruitCalc
                {
                    public static void main(String[] args)
                    {
                        // Setting up the numbers
                        int apples = 12; // we have 12 apples
                        float costApples = 1.25F; // each apple is $1.25
                        int oranges = 25; // we have 25 oranges
                        float costOranges = .49F; // each orange is $.49
    
                        // Display inventory
                        System.out.println("Inventory: ");
                        System.out.println("- Apples" + apples);
                        System.out.println("- Oranges" + oranges);
                        System.out.println("- Total Fruit: " + (apples+oranges));
                        
                        System.out.println("Total Prices: ");
                        System.out.println("-Apples: $" + (apples*costApples));
                        System.out.println("-Oranges: $" + (oranges*costOranges));
                    }
                }
            
Restricted access