Java Cheat Sheet

3 billion universities run Java

#Basics

Hello World in Java

public class main {
    public static void main(String args[]) {
        System.out.println("Hello World");
    }
}
Styled Components is a library that can be installed normally via NPM. It uses JavaScript to generate CSS code at runtime to style components in React.js.

Building and running Java

javac main.java
java main
// Hello World
Make sure you have the JDk installed, then it should work fine.

#Methods in Java

Rules for methods

A method must be defined within a Class. With the static keyword we define that the method belongs to the class it is wrapped by. Void means, it does not return something. We need to define the datatype of the parameters and the return-value.

Basic method

static int timesTwo(int x) {
    return x * 2;
}

public static void main(String args[]) {
    System.out.println(timesTwo(2)); // 4
}

Order of methods

public static void main(String args[]) {
    System.out.println(timesTwo(2)); // 4
}

static int timesTwo(int x) {
    return x * 2;
}
We can executed a method first, before defining it in the code - this is no problem.

Multiple Parameters

public static void main(String args[]) {
    System.out.println(add(2, 3));
}

static int add(int x, int y) {
    return x + y;
}
Of course, we can also pass multiple parameters into a method in Java. But there is no shorthand-syntax for it, so we need to write the datatype of each parameter.

Method Overloading

static int timesTwo(int x) {
    return x * 2;
}
static double timesTwo(double x) {
    return x * 2;
}
System.out.println(timesTwo(2.5)); // 5.0 (double)
System.out.println(timesTwo(2));    // 4   (int)
Yes, both methods have the exact same name - but they expect and return different datatypes. When calling the method, Java will automatically chose the method with the fitting datatype.

#Datatypes in Java

Whole-Value-Datatypes

byte: 1 byte size
short: 2 bytes size
int: 4 bytes size
long: 8 bytes size

Decimal Values

float: 4 bytes size.
double: 8 bytes size
A float value always has the f-suffix. For example: 15.5f

Other types of data

char: a single character. 2 bytes size. boolean: true or false. 1 byte. String: a String.

Using a datatype

String name = "Max";
int age = 24;

Casting a datatype

double value = 5.9;
System.out.println((int) value);  // 5
With casting, we can transform data from one type to another type. When transforming a decimal value to an integer, Java will always round off.

Operators

System.out.println(2 * 2);  // 4
System.out.println(2 + 2);  // 4
System.out.println(2 - 2);  // 0
System.out.println(2 / 2);  // 1
System.out.println(3 % 2);  // 1
Operators actually work like in all other programming languages too.

Operator Shorthands

int age = 10;
age = 10 + 1;
System.out.println(age);  // 11
int age = 10;
age += 1;
System.out.println(age);  // 11
+= is just a shorter syntax for adding a value on a variable.

Increment shorthand x++ vs. ++x

int age = 10;
System.out.println(age++); // 10
System.out.println(age);   // 11
int age = 10;
System.out.println(++age); // 11
System.out.println(age);   // 11
++x increments x directly in the context - x++ increments the value, but we only can get the updated value when accessing the second time.

#Arrays in Java

The definition of Arrays

Arrays have a fixed size. We cannot append or remove anything.
The only can hold one type of data.
Every element has an index, starting with 0.
The elements itself can be changed.
They can hold nearly every type of data.

Creating an empty Array

String people[] = new String[3];
people[0] = "Max";
people[1] = "Anna";
people[2] = "Tom";
We created an empty array, with the size of 3 - 3 elements can be stored in it. Then we place the elemente into it by specifying the value and the index.

Creating an Array - 3 ways

String people[] = new String[3];
String[] people = new String[3];
String people[] = { "Max", "Anna", "Tom" };
Both are working ways to declare a new, empty Array in Java. In the second box, this is the way to create an Array which holds data by default.

Getting elements from an Array

String[] people = { "Max", "Anna" };
System.out.println(people[0]); // Max
System.out.println(people[1]); // Anna
Just specify the index, starting from 0.

Printing the Array

import java.util.Arrays;

String[] people = { "Max", "Anna" };
System.out.println(Arrays.toString(people)); 
// [Max, Anna]
String[] people = { "Max", "Anna", "Tom" };

for (int i = 0; i < people.length; i++) { 
  System.out.println(people[i]);
}
The first way prints the whole Array, with it's structure. The second way prints every single element of the Array.

Changing element values

String[] people = { "Max", "Anna", "Tom" };

people[0] = "Maxi";
System.out.println(people[0]); // Maxi
Just like when we filled an empty Array, this way we can change the values of it's elements.

Multi-dimensional Arrays

int[][] coordinates = new int[3][3];
coordinates[0][0] = 15;

System.out.println(Arrays.toString(coordinates[0]));
System.out.println(Arrays.toString(coordinates[1]));
System.out.println(Arrays.toString(coordinates[2]));

/* [15, 0, 0]
   [0, 0, 0]
   [0, 0, 0] */

#ArrayList in Java

The definition of an ArrayList

The Type ArrayList is an alternative to Arrays.
ArrayLists have an initial size, but we can add and remove elements.
They have own functions for managing them.
.

Creating an ArrayList

import java.util.ArrayList;

ArrayList<String> people = new ArrayList<String>();

people.add("Max");
people.add("Anna");
Make sure to import ArrayList first. ArrayLists have own functions mutate the Array.

Getting Values from an ArrayList

ArrayList<String> people = new ArrayList<String>();

people.add("Max");
people.add("Anna");

System.out.println(people.get(0)) // Max;
System.out.println(people.get(1)) // Anna;
As I said - we need to use the special functions for ArrayLists.

Looping through an ArrayList

for (int i = 0; i < people.size(); i++) {
    System.out.println(people.get(i));
}
for (String i : people) {
    System.out.println(i);
}
We can use for or forEach - same result. Keep in mind that we need use people.size, not people.length for ArrayLists.

Mutating Elements

// updating / overwritting:
people.set(0, "Tom");

// removing elements:
people.remove(2);
remove() removes the whole element, so the size gets reduced by 1.