for 2023+ test takers 💛
source code license
❗ this resource assumes you already know somethings about Java
it
also does not cover things you don't need to know (take with a grain salt)
System.out.println("With a new line"); System.out.print("Without new line");
ln
signifies that a new line will be printed after
the supplied content is printed
System.out.print() System.out.println()
both can be
called without parameters, but println
is usually
called when we just want a new line
System.out.print('\n')
is the same as
System.out.println()
// this is a one line comment /* * this is a multi line comment */
/** * my description here! * @param my parameter for my method * @return what this method returns */
java.lang.Integer
,java.lang.Character
,java.lang.Boolean
,java.lang.Double
,java.lang.Float
,java.lang.Short
,java.lang.Long
Object
representation of a regular
primitive type (int, double, float, etc.).
Object
type
java.util.ArrayList
or identified when the angle
brackets are used (<, >)
Type myVar = (AnotherType) value;
double -> float -> long -> int -> short -> byte
double
and
long
are in the same expression, since
double
is more "wide" or higher up in the
ordering, the compiler chooses to cast the original
long
value to a double
int
,java.lang.Integer
int myInt;
Integer.MAX_VALUE
-> 2147483647
Integer.MIN_VALUE
-> -2147483648
int primitiveInt = 69; Integer boxedVersion = new Integer(69);
double
,java.lang.Double
0.0
->
double myUnInitializedDouble;
Integer
, you can use
Double.MAX_VALUE
and Double.MIN_VALUE
to
get the appropriate representable range and can suffer from
overflow and underflow
[1.7976931348623157*10^308, 4.9406564584124654*10^-324]
double primitiveInt = 0.392; Double boxedVersion = new Double(31.3231);
char
,java.lang.Character
+ -
signs -> only
positive or 0) integer in the range of
[0, 65535]
'
single quote
on both side of the character
char primitiveCharacter ='c'; Character boxedVersion = new Character('1');
boolean
,java.lang.Boolean
true
or false
valuefalse
boolean primitiveBoolean = true; Boolean boxedVersion = new Boolean(false);
String
,java.lang.String
char
values
char
, String
use
"
(double quotes) to encase its value(s)
String
s are IMMUTABLE
String str1 = "A"; String str2 = "B"; str1 = str1 + "C";
str2
would not be the same as
str1
because Java creates a new object everytime the
original object is modified, meaning Strings are immutable, where
the modified object is discarded
\n
which is used to represent the
new line control character, you can use the backslash
\
to print special characters or to override ones
that are quoted
new
keyword, we create an object and the
Java runtime does not perform such check and cause unnecessary
memory usage (where Objects are located on the heap)
"Hi There!".toLowerCase()
toLowerCase()
- makes all possible "A" through
"Z" be their lowercase counterparts
toUpperCase()
- makes all possible "a" through
"z" be their uppercase counterparts
length()
- how many characters make up this
String
substring(int from, int to)
- returns a part of a
String (aka returns a String) starting from index
from
and ending with to - 1
substring(int from)
- similar to calling
substring(from, length())
indexOf(String str)
- returns the index of the
first occurence of the parameter, returns -1 if not found
equals(String str)
- returns a boolean on whether
the two strings are equal based on content
DO NOT USE ==
to check if two are the same
based on content and not memory location
String literalString = "hi there"; String objectString = new String(":)");
[]
like:
Type[] myArray = new Type[size]
where
size
is how many elements the array can hold. the
Type
attribute can be both Objects, primitives, or
other arrays of the same type (2D arrays! d ND arrays!) -> 2D
array:
Type[][] my2DArray = new Type[row_size][column_size]
Type[] myArray = new Type[]{ element_0, element_1, ...}
notice how the size parameter is not needed. For a 2D array we can
follow the following format (for an N-Dimension Array, just append
more bracket pairs):
Type[][] myArray = new Type[][]{ {element_0_0, element_0_1,
...}, { element_1_0, element_1_1, ...}, ...}
myArray[index]
. note that
indexing starts at 0, meaning if you want to access element 2, it
would be myArray[1]
(i.e. you do "canonical_index-1")
and goes up to myArray.length-1
. For an n-dimension
array, like a 2D array, you have to additional bracket pairs
depending on the dimension. For example, a 2D array uses
myArray[row_index][column_index]
ArrayIndexOutOfBoundsException
will be
thrown
myArray.length
.
note that is it not a method and is a field builtin into the
type itself
int[]
array:
int tempVariable = myArr[0]; myArr[0] = myArr[myArr.length - 1]; myArr[myArr.length - 1] = tempVariable;
java.lang.Object
new
keyword to create a new object like so:
MyObj variable = new MyObj()
java.lang.Object
null
. if the
user tries using the object in any way besides comparison of the
raw object, a NullPointerException
will be thrown. to
prevent this, you can achieve a check:
if(myObj == null) { // handle } else { System.out.println("wow it isn't null!"); }
public class MyClass { int myGlobalVariable; }
public class MyClass { ... public void myMethod() { int localVariable = 69; } }
public
,private
static
static
keyword
MyClass.method()
or
MyClass.field
public class ClassNameHere { private int fieldsArePrivate; public ClassNameHere(int val) { // constructor this.fieldsArePrivate = val; } public void setField(int x) { this.fieldsArePrivate = x; } public int getField() { return this.fieldsArePrivate; }
}
getFieldName
, should take no arguments and return the respective type
setFieldName
, should take as many arguments as necessary and return void
private
(and should also not final
unless stated)
extends
keyword:
public class ChildClass extends ParentClass { ... }
super
keyword and behaves similarly to the this
keyword
super(...)
similar to calling another constructor in the same class using this(...)
public
B
extends class A
:
A e = new B()
every overriden method in B will be called instead of those in A (unless they are not overriden). However, for this edge case, we can access explicit child methods by casting upwards: ((Child)variable).method()
Parent e = new Parent(); Parent e1 = new Child(); Child e2 = new Child(); Child e3 = new Parent();
copyright (c) jack meng 2023