Java


Types

int

32 bit signed integer

64

float

32 bit float (number with decimal point)

64.0

double

64 bit float (number with decimal point)

64.0

short

16 bit signed integer

64

long

64 bit signed integer

64L

char

Character

"A"

boolean

Boolean value (either true or false)

true

Arithmetic operators

+

Addition

Unary positive, makes number positive

-

Subtraction

Unary negative, makes number negative

*

Multiplication

/

Division

%

Modulo (remainder of division)

++

Increment then return

int x = 1;
System.out.println(x++);
//Prints 2

Return then increment

int x = 1;
System.out.println(++x);
//Prints 1

--

Decrement

Assignment operators

=

Equals

+=

a+=b is a=a+b

-=

a-=b is a=a-b

*=

a*=b is a=a*b

/=

a/=b is a=a/b

%=

a%=b is a=a%b

&=

a&=b is a=a&b

|=

a|=b is a=a|b

^=

a^=b is a=a^b

>>=

a>>>=b is a=a>>>b

<<=

a><<=b is a=a><<b

Comparison operators

==

Is equal to

!=

Is not equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

instanceof

Checks if an object is a certain type

Bitwise operators

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

~

Bitwise NOT

Logical operators

&&

AND operator

||

OR operator

!

NOT operator

Miscellaneous operators

.

Applies method to object

Applies a state to an enum type

,

Used to separate parameters

[]

Denotes array data type

References a array's index

()

Defines a list

Denotes parameters of a function

{}

Defines a block

void foo() {
}

Initialises an array

String[] array = {"A", "B"}

:

Used for switch cases

Used for for loops on objects within objects

->

Denotes a lambda expression

int x = a -> a+1;

<>

Denotes wrapper type; a generic type defined for a class or function.

ArrayList<int> l;

public static <T> void f(T genericObject) {

""

Declares a string

''

Declares a string

Declares a char

;

Declares end of a line of code

::

Reference operator; Shorthand form for lambda expressions that automatically takes in an argument to a class or instance function

...

Varargs; Declares option to either parse multiple parameters or parsing an array

@

Annotation operator; denotes a method annotation or decorator

+

Concatenates strings

'These are '+'two strings!'

Key word

extends

Used for including a class in another class, creating polymorphism

super

Used for referencing the class' superclass

implements

Used for including an interface in another interface or in a class, creating polymorphism

enum

Declares an enum; a datatype that denotes a "state" of something

public enum enumName {
 code;
}

interface

Used for declaring an interface; like a class but has no variables and methods can be empty

interface

Declares an interface; like a class but has no variables and methods are not defined

public interface interface {
 code;
}

abstract

Used for declaring an abstract class or method, which is a class or method that is only accessible through its subclasses and is defined in its subclass

switch

Switches logic flow based on an output

switch (expression) {
case x:
  code;
break;
case y:
  code;
break;
}

double

Used for declaring doubles (also known as floats, or numbers with decimal place)

boolean

Used for declaring booleans

int

Used for declaring integers

String

Used for declaring strings

static

Used for declaring methods that do not depend on the state of the class

do

Used for declaring loop in a while loop

throw

Throws an exception object

throw new exception();

throws

Used for denoting methods that throw specific errors

char

Used for declaring characters

void

Used for declaring that a method does not return a specific value

this

Used for referring to the original constructor of a class on which a new object is being created

this()

Used for referring to the class object

this.classMethod()

this.classVariable

When used within a constructor method, used for referring to the instance of the class

this.instanceVariable

new

Used for declaring new objects from a class

private

Used for declaring private classes or methods, that cannot be interacted with by other classes

public

Used for declaring public classes or methods, that can be interacted with by other classes

if

Executes code if condition is true

if (condition) {
 code;
}

while

Loops code while a condition is true

while (condition) {
 code;
}

do {
 code;
}
while (condition);

for

Loops code for a set amount iterations or for every object in list

for (initialization; condition; update) {
 code;
}

for (type variable: array) {
 code;
}

try

Attempts to execute code and jumps to the subsequent except on an error

try {
 code;
}

try (declaration) {
 code;
}

catch

Executes code on failed try statement

catch (Exception exception) {
 code
}

else

Executes code when if or else if isn't executed

else {
 code;
}

import

Used for importing foreign classes for use in code

return

Used to give a final value at the end of a function or method

break

Used to tell compiler to end the loop

final

Used to denote a class with no subclasses or a function that can't be overwritten in a subclass

class

Declares a class

public class class {
 code;
}

interface

Declares an interface

default

Default case in a switch statement

finally

Executes code after try and catch blocks

finally {
 code;
}

Constructor

Constructor of a class (method to make objects of a class)

public class() {
 code
}

Methods

equals

Returns Boolean on whether two objects have the same value

boolean object1.equals(object2);

endsWith

Declares if an object ends in a certain string

boolean object.endsWith("string");

indexOf

Declares position of string in object

int object.indexOf("string");

main

Declares the main function in a class

public static void main(String[] args){
 code;
}

import

Includes foreign file's module

import module;

Commenting

//

Used to tell compiler the rest of this line is not to be compiled

//This isn't compiled

/*

Opens a multi line comment

/*This isn't compiled
neither is this line*/

*/

Closes a multi line comment

/*This isn't compiled
neither is this line*/

Decorators

A method that takes another method as parameter and returns a new method

@Overrides

Used to tell compiler to replace the old instance of the function with this one

@override