Show Menu
Cheatography

Java Fundamentals Cheat Sheet (DRAFT) by

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

Datatypes

public class Opdracht1 {
    public static void main(String [] args) {
        long a = 196587543L;
        float b = 1.0065f;
        boolean c = false;
        boolean e = false;
        double d = 0.0d;
        double f  = 0.0d;
    }
}

For-loop

public class Opdracht3 {
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            System.out.println(i);
        }
    }
}

While-loop

public class Opdracht4 {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 20) {
            System.out.println(i);
            i++;
        }
    }
}

Ternary

public class Opdracht6 {
    public static void main(String[] args) {
        for (int x = 0; x <= 20; x++) {
            String result = (x % 3) == 0 ? "het getal is deelbaar door 3!" : "";
            System.out.println(x + " " + result);
        }
    }
}

String omzetten naar...

String -> integer
Intege­r.p­ars­eIn­t(str)
Integer -> String
String.va­lue­Of(var)
String -> Double
Double.pa­rse­Dou­ble­(str)
Double -> String
String.va­lue­Of(­dnum)

String methoden

str1==str2 //compares address;
String newStr = str1.equals(str2); //compares the values
String newStr = str1.equalsIgnoreCase() //compares the values ignoring the case
newStr = str1.length() //calculates length
newStr = str1.charAt(i) //extract i'th character
newStr = str1.toUpperCase() //returns string in ALL CAPS
newStr = str1.toLowerCase() //returns string in ALL LOWERCASE
newStr = str1.replace(oldVal, newVal) //search and replace
newStr = str1.trim() //trims surrounding whitespace
newStr = str1.contains("value"); //check for the values
newStr = str1.toCharArray(); // convert String to character type array
newStr = str1.IsEmpty(); //Check for empty String
newStr = str1.endsWith(); //Checks if string ends with the given suffix
 

Input gebruiker

import java.util.Scanner;

public class Opdracht2 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String invoer = input.next();
        System.out.println("Je hebt de volgende input gegeven:  " + invoer);
    }
}
naam.n­ext­Int() -> Integer invoer

naam.n­ext­Dou­ble() -> Double invoer

naam.n­ext() -> String invoer, zonder spaties

naam.n­ext­Line() -> Alles voor de 'enter'

Getal deelbaar door (3)

public class Opdracht5 {
    public static void main(String[] args) {
        for (int x = 1; x <= 10; x++) {
            if ((x % 3) == 0) {
                System.out.println(x+ " " + "het getal is deelbaar door 3!" );
            } else {
                System.out.println(x);
            }
        }
    }
}

Boolean String

equals
naam.e­qua­ls(­" ")
equals­ign­oreCase
naam.e­qua­lsI­gno­reC­ase­(" ")
startWith
naam.s­tar­tsW­ith­(" ")
endsWith
naam.e­nds­Wit­h(" ")
contains
naam.c­ont­ain­s(" ")

Spaties aan eind van string weghalen

int len = str.length();
    for( ; len > 0; len--)
    {
      if( ! Character.isWhitespace( str.charAt( len - 1)))
         break;
    }
    return str.substring( 0, len);