Skip to the content.

Unit 1 Notes

Unit 1: Primitive Types – AP Computer Science A

🔑 Key Concepts

  • Primitive Data Types: int, double, boolean, char
  • Variable Declaration and Initialization
  • Arithmetic Expressions
  • Casting (Type Conversion)
  • Constants (final keyword)

📌 Primitive Data Types

Type Example Description
int int x = 5; Whole numbers
double double y = 3.14; Decimal numbers (floating point)
boolean boolean done = true; True or false values
char char grade = 'A'; A single Unicode character

🧠 Arithmetic Expressions

Basic math:

int x = 5;
int y = 2;
System.out.println(x + y); // 7
System.out.println(x / y); // 2 (integer division)

With doubles:

double a = 5;
double b = 2;
System.out.println(a / b); // 2.5

Operator Precedence (same as math):

  1. () Parentheses
  2. *, /, %
  3. +, -

🔄 Type Casting

Widening (safe): int → double

int i = 3;
double d = i; // 3.0

Narrowing (manual):

double pi = 3.14;
int approx = (int) pi; // 3

✅ Constants

Declare a constant using final:

final double PI = 3.14159;
PI = 3.2; // ❌ Error: cannot assign a value to final variable

💡 Tips

  • Integer division truncates the decimal.
  • % is the modulus operator (remainder).
    10 % 3  1
    8 % 2  0
    
  • Use char for single characters only, " " for strings:
    char c = 'A';
    String s = "A";
    

🧪 Practice Prompt

What will the following code print?

int x = 10;
int y = 3;
System.out.println(x / y);
System.out.println((double) x / y);

Answer:

3
3.3333333333333335

🧪 Unit 1 Practice Multiple Choice (5 Questions)

1. What is the result of the expression 7 / 2 in Java?
(A) 3.5
(B) 3
(C) 4
(D) 3.0
Answer: (B) — Integer division truncates the decimal.


2. Which of the following is a valid declaration of a char?
(A) char c = "A";
(B) char c = 'A';
(C) char c = A;
(D) char c = 'ABC';
Answer: (B) — A char holds one character in single quotes.


3. What will be printed by the following code?

double x = 5;
int y = 2;
System.out.println(x / y);

(A) 2
(B) 2.0
(C) 2.5
(D) 2.00
Answer: (C) — A double divided by an int results in a double.


4. What keyword is used to define a constant in Java?
(A) const
(B) static
(C) final
(D) constant
Answer: (C) — The final keyword is used for constants.


5. What is the value of result in the following code?

int result = (int)(3.7 + 2.5);

(A) 6
(B) 6.2
(C) 7
(D) 5
Answer: (A) — 3.7 + 2.5 = 6.2 → cast to int gives 6.