Unit 1 - Primitive Types

Classes and Methods

  • Every Java program must have at least one class.
  • Classes are declared using public class ClassName, where ClassName follows Pascal case (e.g., MyFirstProgram).
  • Class names must match the file name exactly, with a .java extension (e.g., MyFirstProgram.java).
  • Curly brackets {} define the beginning and end of a class.
  • Indentation increases after an opening curly bracket and decreases after a closing curly bracket for readability.
  • Every program must have at least one main method, which is the entry point.
  • The main method signature is public static void main(String[] args). Note the capital "S" in String.
  • The System.out.println() command outputs data to the console.
  • A string literal is a specific text or number enclosed in double quotes (e.g., "Hello, world").
  • Certain lines of code, like System.out.println(), must end with a semicolon ;.
  • Use "Build" then "Compile" to check the program for errors and convert it to byte code.
  • Use "Build" then "Run" to execute the program.

Variables

  • Variables store and manipulate data.
  • Variables declared inside a method are local variables.
  • Declare a variable with DataType variableName; (e.g., int x;).
  • Initialize a variable by assigning it a value (e.g., x = 10;).
  • Variables can be declared and initialized on the same line (e.g., int myFinalScore = 10;).
  • The single equal sign = acts as a left-pointing arrow, assigning the value on the right to the variable on the left.
  • Variable names should follow lower camel case (e.g., myFinalScore).
  • To output the value of a variable, use System.out.println(variableName); without quotation marks.

Primitive Data Types

  • Java has eight primitive data types: byte, short, int, long, float, double, boolean, and char.
  • int, double, and boolean are the most commonly used, especially for the AP Computer Science A exam.
  • int stores whole numbers (e.g., int a = 3;).
  • double stores decimal numbers (e.g., double b = 3.0;).
  • A double can be assigned an integer value; this is called widening.
  • A double can be manually cast to an int. The decimal portion is truncated, not rounded (e.g., int a = (int)3.999; results in a being 3).
  • boolean stores either true or false (e.g., boolean c = true;).

Operators

  • The simple assignment operator = assigns the value on the right to the variable on the left.
  • Arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), % (mod/remainder).
  • The mod operator % returns the remainder of a division (e.g., 4 % 3 equals 1).
  • Unary operators are shortcuts:
    • x++ is equivalent to x = x + 1.
    • x-- is equivalent to x = x - 1.
    • x *= 3 is equivalent to x = x * 3.
  • The not operator ! with a boolean negates the value (e.g., if z is true, !z is false).

Division with int and double

  • int division truncates the decimal portion (e.g., 5 / 3 equals 1).
  • double division results in a decimal value if one or both operands are doubles (e.g., 5.0 / 3 is a double division, resulting in 1.666...).
  • Dividing an int by zero results in an ArithmeticException.
  • Dividing a double by zero results in:
    • Positive infinity if the numerator is positive.
    • Negative infinity if the numerator is negative.
    • NaN (Not a Number) if both the numerator and denominator are zero.

Operator Precedence

  • Operator precedence determines the order of execution.
  • Use parentheses to clarify the order of execution.
  • Simplified precedence table (highest to lowest):
    1. Parentheses ()
    2. Increment/Decrement ++, --, and Not !
    3. Casting (int), (double)
    4. Multiplication *, Division /, Mod %
    5. Addition +, Subtraction -
    6. Comparative Operators (e.g., >, <)
  • Operators with the same precedence are generally processed from left to right.

Casting and Overflow

  • Casting converts a piece of data from one primitive type to another.
  • Widening: Converting to a data type with more precision or space (e.g., int to double). This can happen automatically.
  • Narrowing: Converting to a data type with less precision or space (e.g., double to int). This must be done manually.
  • When casting a double to an int, the decimal portion is truncated (not rounded).
  • int values range from approximately -2,000,000,000 to 2,000,000,000.
  • byte values range from -128 to 127.
  • Overflow: Occurs when a number is larger than the data type can hold.
    • byte, short, int, and long will wrap around to the other side (e.g., adding 1 to a byte with a value of 127 results in -128).
    • double and float will go to positive or negative infinity.

Swapping Variable Values

  • The most universal way to swap the values of two variables is with a temporary (temp) variable.
  • Example:
    java int x = 3; int y = 9; int temp = x; // Store the value of x in temp x = y; // Assign the value of y to x y = temp; // Assign the value of temp (original x) to y