Primitve Types on CSA Exam

  • int - which store integers (numbers like 3, -76, 20393)
  • double - which store floating point numbers (decimal numbers like 6.3 -0.9, and 60293.93032)
  • boolean - which store Boolean values (either true or false).

Declaring Variables in Java

  • To create(declare) a variable, you must tell Java its data type and its name.
    • have to tell Java the type of the variable because Java needs to know how many bits to use and how to represent the value
    • integer - 32 bits of memory
    • double - 64 bits
    • boolean - one bit

Example: int score; score = 4;

(initialize) int score = 4;

Assignment Statements

  • Assignment statements initialize or change the value stored in a variable using the assignment operator =.
  • Instead of saying equals for the = in an assignment statement, say “gets” or “is assigned” to remember that the variable gets or is assigned the value on the right.
  • Example: score = (10 * points) + 5
    • score - variable
    • = is assigned to
    • (10 * points) + 5 - expression

Operators

  • Java uses the operator == to test if the value on the left is equal to the value on the right and != to test if two items are not equal.
  • Use == and != only with int values and not doubles because double values are an approximation and 3.3333 will not equal 3.3334 even though they are very close.
  • Don’t get one equal sign = confused with two equal signs ==.

    • = - assign a value to a variable
    • == - test a variable to see if it is a certain value and that returns true or false as you’ll see below
  • An arithmetic operation that uses two int values will evaluate to an int value. With integer division, any decimal part in the result will be thrown away.

  • An arithmetic operation that uses at least one double value will evaluate to a double value.
  • During evaluation, operands are associated with operators according to operator precedence to determine how they are grouped. (*, /, % have precedence over + and -, unless parentheses are used to group those.)

Compound Assignment Operators

  • Compound assignment operators are shortcuts that do a math operation and assignment in one step
  • The most common shortcut operator ++, the plus-plus or increment operator, is used to add 1 to the current value; x++ is the same as x += 1 and the same as x = x + 1

Casting and Ranges of Variables

  • type casting is used to convert variable values from one type to another
  • Situation to use casting: If you have integers and you want a double result from some mathematical operation cast one of the integers to a double using (double) as shown above.

int nearestInt = (int)(number + 0.5);

int nearestNegInt = (int)(negNumber – 0.5);

Common Mistakes

  • forgetting that Java is case sensitive - myScore is not the same as myscore.
  • forgetting to specify the type when declaring a variable (using name = value; instead of type name = value;)
  • using a variable name, but never declaring the variable.
  • using the wrong name for the variable. For example calling it studentTotal when you declare it, but later calling it total.
  • using the wrong type for a variable. Don’t forget that using integer types in calculations will give an integer result. So either cast one integer value to double or use a double variable if you want the fractional part (the part after the decimal point).
  • using == to compare double values. Remember that double values are often an approximation. You might want to test if the absolute value of the difference between the two values is less than some amount instead.
  • assuming that some value like 0 will be smaller than other int values. Remember that int values can be negative as well. If you want to set a value to the smallest possible int values use Integer.MIN_VALUE.