Show Menu
Cheatography

Java - AP Comp Sci Cheat Sheet (DRAFT) by

Java - AP Comp Sci 2020

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Variables

Type
Descri­ption
Example Value
byte
8 bits
0
int
Integer
0
double
Integers w/ decimals
0.00
boolean
Boolean value (True or False)
False
char
Single character
'a'
String
One or more characters
"­a1:­&b­!"

Instan­tiating

Primitives
int myInt = 12;
double myDouble = 2.11;
char myChar = 'c';

Strings
String myString = "­hel­lo";

Classes
MyClass classOne = new MyClas­s(p­ara­met­ers);
MyClass classTwo = new MyClass();

Getters & Setters

Getter
public int getMyInt()
{
 ­ ­ ­ ­return myInt;
}

Setter
public void setMyI­nt(int newInt)
{
 ­ ­ ­ ­myInt = newInt;
}
 

Size and Length

.length()
Returns the number of characters in a String object
.length()
Returns the number of elements in a Array
.size()
Returns the number of elements in a ArrayList

Operators

Operator
Descri­ption
+
addition of numbers, concen­tration of Strings
-
subtra­ction
*
multip­lic­ation
/
division
%
modulus; find remainder
+=
add and assign numbers, conven­trate and assign Strings
-=
subtract and assign
*=
multiply and assign
/=
divide and assign
%=
modulus and assign
++
add one
--
subtract one
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
!
not
!=
not equal to
&&
and
||
or
==
comparison
=
assign
 

Loops

for (int i = 0; i < x; i++) { }
for (int i : someArray) { }
while (something is true) { }

Classes

public class MyClass
{
 ­ ­ ­ ­private int myInt;
 ­ ­ ­ ­private String myString;

 ­ ­ ­ ­public MyClass()
 ­ ­ ­ {
 ­ ­ ­ ­ ­ ­ ­ ­myInt = 0;
 ­ ­ ­ ­ ­ ­ ­ ­myS­tring = "­hello world";
 ­ ­ ­ }
 ­ ­ ­ ­public MyClas­s(int i, String s)
 ­ ­ ­ {
 ­ ­ ­ ­ ­ ­ ­ ­myInt = i;
 ­ ­ ­ ­ ­ ­ ­ ­myS­tring = s;
 ­ ­ ­ }
}

Arrays

int[] myArrayTwo = {1, 2};
------­---­---­---­------ or ------­---­---­---­------
int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;

ArrayList

.add(obj)
Appends obj to end of list
.add(i­ndex, obj)
Inserts obj at position index, moving elements at position index and higher to the right
.set(i­ndex)
Replaces the element at position index with obj; returns the element formerly at position index
.get(i­ndex)
Returns the element at position index in the list

ArrayList

.add(o­bj)­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­Appends obj to end of list.
.add(i­ndex, obj)     In­serts obj at position index, moving  ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ele­ments at position index and higher  ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ to the right.
.set(i­ndex, obj)     Re­places the element at position  ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­index with obj.
.get(i­nde­x)            R­eturns the element at position index  ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ in the list.
.remov­e(i­ndex) ­ ­ ­ ­ Removes element from position
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ index, moving elements at position
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ index + 1 and higher to the left and
 ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ subtracts 1 from size.