KEMBAR78
Unit 1 - Primitive Types | PDF | Bit | Data Type
0% found this document useful (0 votes)
8 views30 pages

Unit 1 - Primitive Types

This document covers the basics of Java programming, focusing on primitive data types, literals, and arithmetic operations. It explains various data types such as byte, short, int, long, float, double, boolean, and char, along with their sizes and usage. Additionally, it discusses assignment operators, compound assignment, and casting in Java.

Uploaded by

veddwivedi39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views30 pages

Unit 1 - Primitive Types

This document covers the basics of Java programming, focusing on primitive data types, literals, and arithmetic operations. It explains various data types such as byte, short, int, long, float, double, boolean, and char, along with their sizes and usage. Additionally, it discusses assignment operators, compound assignment, and casting in Java.

Uploaded by

veddwivedi39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

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.

Two possible values - 1 or 0.


1001 0001
1101 0101
0000 0001

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:

● int x = 3; // 3 is a literal, but x is not


● String msg = “hello”; // “hello” is a String literal

The following are not literals:

● int y = sc.nextInt() // where sc is a scanner object


● int timesTwo(int z){ // z is not a literal

Basically, is there a “literal” value in the code?


Basic Arithmetic
You know these.

+-*/%
% (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.

% has the same precedence as multiplication/division


The assignment operator
=
Eg: int x = 4;

Yes, this is an actual Collegeboard topic in 1.3.


The assignment operator
Here is a screenshot from the AP Classroom daily video on the topic:

If you can do this, you have mastered this topic.


Lesson 4 - Compound
Assignment Operators
Unit 1 - Primitive Types
Compound assignment
operators
+=, -=, *=, /=, %=

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

(double) 2 -> 2.0


(int) 2.5 -> 2
Remember data type sizes!
This is the correct answer.
Remember data type sizes!
This is not—it is the maximum value of an integer

You might also like