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.printandSystem.out.printlnare used for console output.printlnmoves the cursor to a new line after displaying data, whileprintdoes 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:trueandfalse.
Literals:
Represent exact values in code.
Arithmetic Expressions
intanddoubletypes can be used in arithmetic expressions.Arithmetic operators:
+: Addition-: Subtraction*: Multiplication/: Division%: Modulus
Precedence Rules:
*,/,%+,-
Operators in the same group are evaluated from left to right.
Parentheses can override precedence.
Example:
4 + 3 * 2evaluates to 10, while(4 + 3) * 2evaluates to 14.
Integer Division:
When both operands are
int, the result is anint, truncating any non-integer part.Example:
7 / 4evaluates to 1.
If at least one operand is a
double, the result is adouble.
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 byfin = 4.5 – 5.1;finalKeyword: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 += 3is equivalent tox = x + 3-=:x -= 1is equivalent tox = x – 1*=:x *= 2is equivalent tox = x * 2/=:x /= 10is equivalent tox = x / 10%=:x %= 10is equivalent tox = x % 10
Increment and Decrement Operators:
x++,x += 1, andx = x + 1are equivalent increment operations.x--,x -= 1, andx = x - 1are 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
doubleto anintresults in truncation (the decimal part is discarded).Example:
int x = (int)points;wherepointsis a double with value ,xwill be assigned the value .
Automatic Casting (Widening Conversion):
intvalues can be automatically cast todoublevalues.Example:
double y = 2 * x + 3;where x is an integer, 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
intvariable, be cautious, especially when calculating averages.Use a
doublevariable to store the accumulated value, or cast to adoublebefore dividing to find the average.Otherwise, the calculated average will be truncated, even if the result is stored in a
doublevariable.
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 +