Unit 1 - Primitive Types
Unit 1 - Primitive Types
AP CS A
Lesson 1 - Java Basics
Unit 1 - Primitive Types
;
; ;
;
;
;
; Semicolons.
Everywhere. ;
;
;
;
Comments
Use them!
// for single-line
/* starts a multiline
end with */
print v println
System.out.println();
\
Escape character!
\n, \t, \”, \’, \\
public static void main(String[] args)
The green run button executes this method.
Lesson 2 - Data Types
Unit 1 - Primitive Types
Bit - binary digit
An electrical switch can be either on or off.
1101 0101
Byte - 8 bits
You can make a lot of numbers with 8 bits!
1 All the way from 0000 0000
0
00
11 To 1111 1111
00
0 1
01
10
11
In fact, you can make 256 numbers total.
Primitive Data Types
No Objects/classes necessary
Whole numbers: Numbers w/ decimal Other:
● byte (1 byte) ● float (4 bytes) ● boolean (1 bit!)
● short (2 bytes) ● double (8 bytes) ○ true/false
● int (4 bytes) ● char (2 bytes)
● long (8 bytes) ○ A single character
Lesson 3 - Expressions and
Assignment Statements
Unit 1 - Primitive Types
Close your scanners!
This is not part of the Collegeboard curriculum, but
it’s something you should do. No more warnings!
Literals
A “literal” is a source code representation of a fixed value. For instance:
+-*/%
% (Remainder)
x % y gives the remainder of x divided by y
● 5 % 2 -> 1
● 2 % 5 -> 2
Can also think of it as what time it is after x hours have passed
Remainder v modulus
Technically, Java has no “modulus”. The modulo operator calculates the
remainder.
Remainder v modulus
● Modulus - result has the same sign as the divisor (bottom number)
● Remainder - result has the same sign as the dividend (top number)
Arithmetic on data types
Combine two ints, you get an int.
(this counts for division, too)
5 / 2 -> 2
Combine two doubles, you get a double.
Arithmetic on data types
Combine an int and a double, you get a double
Order of Operations
PEMDAS
Exact same as math.
x += 2
add 2 to x and save the new value to x
Increment & Decrement
operators
++, –
x ++
add 1 to x and save the new value to x
Lesson 5 - Casting and
Ranges of Variables
Unit 1 - Primitive Types
Casting
(data type) thing_you_you_want_to_cast