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

Variables

Type

Description

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!”

Instantiating

Primitives
int myInt = 12;
double myDouble = 2.11;
char myChar = ‘c’;
Strings
String myString = “hello”;
Classes
MyClass classOne = new MyClass(parameters);
MyClass classTwo = new MyClass();

Getters & Setters

Getter
public int getMyInt()
{
return myInt;
}
Setter
public void setMyInt(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

Description

+

addition of numbers, concentration of Strings

subtraction

*

multiplication

/

division

%

modulus; find remainder

+=

add and assign numbers, conventrate 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;
myString = “hello world”;
}
public MyClass(int i, String s)
{
myInt = i;
myString = 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(index, obj)

Inserts obj at position index, moving elements at position index and higher to the right

.set(index)

Replaces the element at position index with obj; returns the element formerly at position index

.get(index)

Returns the element at position index in the list

ArrayList

.add(obj) Appends obj to end of list.
.add(index, obj) Inserts obj at position index, moving elements at position index and higher to the right.
.set(index, obj) Replaces the element at position index with obj.
.get(index) Returns the element at position index in the list.
.remove(index) Removes element from position
index, moving elements at position
index + 1 and higher to the left and
subtracts 1 from size.

You are watching: Java – AP Comp Sci Cheat Sheet. Info created by GBee English Center selection and synthesis along with other related topics.