integer arithmetic, math methods, strings, tracing code, methods, writing code, arrays, arraylist, algorithms
first line
public class ProgramName
second line
public void main(String[] arg)
driver method
role of ‘main’ in second line
print line (code)
System.out.println(“”);
syntax error
case sensitive language; missing character (ex: ; ( { )
logic error
displays “hello” instead of “HELLO”; mathematical error by programmer
runtime error
occurs during execution; ex: divide by 0, out of bounds access of an array
identifiers
may include: letters, numbers, _, $; may not start w/ number; no spaces
source code
used in editor; MyName.java
byte code
used in compiler; MyName.class
machine code
used in interpret (JVM); code is translated + executed
compiler
checks for syntax errors; creates byte code, if there are no errors
print(“”)
displays argument
println(“”)
displays argument, then CRLF
CRLF
carriage return + line feed; new line
System.out
implies printing output
System.in
implies receiving user input
\n
escape sequence; new line
\\
escape sequence; prints one slash
\”
escape sequence; prints one double quote
integer
4 data types: byte, short, int, long; number with no decimals
real number
2 data types: float, double; number with decimals
boolean
1 data type; true or false
String
reference data type; saves reference to base16 memory address
concatenation
using ‘+’ to add strings
final
declare constant version of variable
+=
add by number after ‘=’
-=
subtract by number after ‘=’
/=
divide by number after ‘=’
*=
multiply by number after ‘=’
%=
divide by number after ‘=’; return remainder
integer division
division between two integers; returns no remainder or decimals
float division
division with at least one real number; returns decimals
modular division
division between two integers; returns remainder
binary shortcut
shortcut type; math sign then ‘=’ sign
unary shortcut
shortcut type; variable and two of math sign
x++ (or ++x)
adds one to x
x-- (or --x)
subtracts one from x
type casting
used when declaring variable; do not use twice
System.out.print(“hi” + 5 + 5);
hi55
System.out.print(“hi” + (5 + 5));
hi10
exponent
Math.pow(int x, int n);
square root
Math.sqrt(int x);
substring
.substring(int begIndex);
.substring(int begIndex, int endIndex);
# does not include end Index
o Worl
(“Hello World”).substring(4, 10);
.length()
returns string length; starts with 1
.indexOf(String str)
returns position of first occurrence of str; returns -1 if not found
.charAt(int index)
returns character at a specified string
o
(“Hello World”).charAt(4);
if/else
executed if condition (placed in parentheses) is true; no semicolon at end; expects one line of following code, use { } for more
hi!
if (5 == 5)
return(“hi!”);
<
less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
==
equal to, for logic
.equals(String str)
compares one String to another; returns boolean
!=
not equal to, for logic
for(int index = 0; condition; index++)
for loop; condition will return true while loop continues; index can be manipulated in various ways; standard form
for(int i = arrList.size() - 1; i >= 0; i --)
best for loop for iteration through arrayList which you will be removing items from
for(Object obj: arrList)
enhanced for loop; does not provide index or easily update item values
while loop
loop that iterates until condition is false; no provided index; condition must be in parentheses; expects one line of following code, use { } for more
Math.random()
provides a random double in [0.0, 1.0); to manipulate, add, divide, and Integer type casting
(int) (Math.random() * 11 + 4)
returns a number between 4 and 14 (inclusive)
method header
first line in method definition; includes visibility, return type, method name, and parameters
void
method header return type; no return
return type (method header)
specifies data type of returned values
parameters
data type needs to be specified (ex: (int age))
.length
returns length of an array
arr[i]
provides element at index i of array
int[] arr = new int[10];
initialize int array with default values
String[] arr = new String[] {“hello”, “world”};
initialize String array with pre-set values
null
default String value
0
default int value
false
default boolean value
.add(Object obj)
adds an element obj to the end of ArrayList
.add(int index, Object obj)
adds an element obj at index of ArrayList
.set(int index, Object obj)
changes element at index of ArrayList to obj
.remove(int index)
removes an element at index of ArrayList
.get(int index)
accesses element at index of ArrayList; used to print element
.size()
returns the size of ArrayList
ArrayList<Object> arrList = new ArrayList<Object>();
initialize empty ArrayList; can contain any object type
total algorithm
int x = 0;
for (int num : arrList)
x += num;
return x;
counting algorithm
int x = 0;
for (int num : arrList)
if (num == condition)
x+= 1;
return x;
search algorithm
for (int i = 0; i < arrList.size(); i++)
if (arrList.get(i) == condition)
return i;
return -1;
remove algorithm
for (int i = arrList.size() - 1; i >= 0; i--)
if (arrList.get(i) == condition)
arrList.remove(i);
smallest int algorithm
int x = nums.get(0);
for (int num : nums)
if (num < x)
x = num;
return x;
largest int algorithm
int x = nums.get(0);
for (int num : nums)
if (num > x)
x = num;
return x;