Primitive Types and Basic Java Concepts

AP Computer Science A Study Guide

Primitive Types

  • Key Exam Details:

    • The AP Computer Science A course is equivalent to a first-semester, college-level course.

    • The exam is 3 hours long and consists of 44 questions.

    • 40 multiple-choice questions (50% of the exam).

    • 4 free-response questions (50% of the exam).

    • Exam Content Categories and Weights:

      • Primitive Types: 2.5%–5%

      • Using Objects: 5%–7.5%

      • Boolean Expressions and if Statements: 15%–17.5%

      • Iteration: 17.5%–22.5%

      • Writing Classes: 5%–7.5%

      • Array: 10%–15%

      • ArrayList: 2.5%–7.5%

      • 2D Array: 7.5%–10%

      • Inheritance: 5%–10%

      • Recursion: 5%–7.5%

  • Printing and Comments:

    • System.out.print and System.out.println are used for console output.

    • println moves the cursor to a new line after displaying data, while print does not.

    • Comments are ignored by the computer.

    • Single-line comments are denoted by //.

    • Multiline comments are demarcated by /* and */.

Data Types

  • Every value in a program has a specific data type.

  • Types are categorized as primitive or reference types.

  • AP Computer Science A focuses on three primitive types:

    • int: Integer numbers (e.g., 3, -14, 21860).

    • double: Floating-point numbers (e.g., 3.14, -1.0, 48.7662).

    • boolean: true and false.

  • Literals:

    • Represent exact values in code.

Arithmetic Expressions

  • int and double types can be used in arithmetic expressions.

  • Arithmetic operators:

    • +: Addition

    • -: Subtraction

    • *: Multiplication

    • /: Division

    • %: Modulus

  • Precedence Rules:

    1. *, /, %

    2. +, -

  • Operators in the same group are evaluated from left to right.

  • Parentheses can override precedence.

    • Example: 4 + 3 * 2 evaluates to 10, while (4 + 3) * 2 evaluates to 14.

  • Integer Division:

    • When both operands are int, the result is an int, truncating any non-integer part.

    • Example: 7 / 4 evaluates to 1.

  • If at least one operand is a double, the result is a double.

Variable Declaration and Assignment

  • Variables are names associated with memory locations that store values.

  • Every variable has a type.

  • Declaration: type name; (e.g., int age;)

  • Assignment: variable = expression; (e.g., age = 18;)

  • Declaration and assignment can be combined: int age = 18;

  • Variable values can be changed: double fin = 3.2; followed by fin = 4.5 – 5.1;

  • final Keyword:

    • Used to declare constants (values that cannot be changed after assignment).

    • Example:
      java final int x = 5; x = x - 2; // this line will cause a compiler error

Compound Operators

  • Shorthand for updating a variable with an arithmetic operation.

  • Examples:

    • +=: x += 3 is equivalent to x = x + 3

    • -=: x -= 1 is equivalent to x = x – 1

    • *=: x *= 2 is equivalent to x = x * 2

    • /=: x /= 10 is equivalent to x = x / 10

    • %=: x %= 10 is equivalent to x = x % 10

  • Increment and Decrement Operators:

    • x++, x += 1, and x = x + 1 are equivalent increment operations.

    • x--, x -= 1, and x = x - 1 are equivalent decrement operations.

Casting

  • Values of a specific type can only be assigned to variables of the same type.

  • Example that causes a compiler error: int myVariable = 6.3;

  • (int) and (double) Operators:

    • Used to create temporary values converted to another type (casting).

    • Casting a double to an int results in truncation (the decimal part is discarded).

    • Example: int x = (int)points; where points is a double with value 12.812.8, x will be assigned the value 1212.

  • Automatic Casting (Widening Conversion):

    • int values can be automatically cast to double values.

    • Example: double y = 2 * x + 3; where x is an integer, yy will store a double.

    • Similarly, an integer value will automatically be cast to double when calling a method that declares a double parameter.

Free Response Tip

  • When storing accumulated values in an int variable, be cautious, especially when calculating averages.

  • Use a double variable to store the accumulated value, or cast to a double before dividing to find the average.

  • Otherwise, the calculated average will be truncated, even if the result is stored in a double variable.

Sample Primitive Types Questions

  • Question 1:

    • Code:
      java int x = 9; int y = 2; int z = 1; System.out.println(x / y * 1.5 - z);

    • Output: 5.0

    • Explanation: Division and multiplication have the same precedence and are evaluated left-to-right. 9/2 -> 4 (integer division). 4 * 1.5 -> 6.0 (double). 6.0 – 1 -> 5.0.

  • Question 2:

    • Code:
      ```java
      double a = 3.6;
      int b = (int)a + 2;
      double c = b;
      System.out.print(a +