JAVA Docs

New File (Base)

public class name_of_file {
  public static void main(String[] args){
      // Some Code
  }
}

Declaring (Creating) Variables

type variableName = value;

Data types

int myNum = 5;               // Integer (whole number)
float myFloatNum = 5.99f;    // Floating point number
char myLetter = 'D';         // Character
boolean myBool = true;       // Boolean
String myText = "Hello";     // String

Output / Print

System.out.println("Hello World!");     // Text
System.out.println(3);                  // Integer
System.out.println(myNum);              // Any Variable

Array

int[] myNum = {1, 2, 3};                    // Integer array
float[] myFloatNum = {1.1f, 1.2f, 1.3f};    // Float array
char[] myLetter = {'A', 'B', 'C'};          // Character array
boolean[] myBool = {true, false, true};     // Boolean array
String[] myText = {"Hello", "World"};       // String array

int[] emptyArray = new int[10]              
// Creates empty array with 10 slots

Doing stuff with arrays

System.out.println(myNum[2]);   // output: 3
System.out.println(myText[0]);  // output: "Hallo"
myNum[2] = 30;
System.out.println(myNum[2]);   // output: 30
System.out.println(myNum.length);   // output: 3

Loop Through an Array with for

for (int i = 0; i < myNum.length; i++) {
  System.out.println(myNum[i]);
}

OR for each

for (int i : myNum) {
  System.out.println(i);
}

// Output:

1
2
3

Math

Math.max(5, 10);    // Finds highest value of x and y output: 10
Math.min(5, 10);    // Finds lowest value of x and y output: 5
Math.sqrt(64);      // The square root of x output: 8
Math.abs(-4.7);     // The absolute (positive) value of x output: 4.7
Math.pow(2, ;2)     // Power of 2^2 output: 4
Math.round(1.5f);   // Rounds output: 2

Random

Random ran = new Random();
int x = ran.nextInt(max - min + 1) + min;   // for random int
int y = ran.nextdouble(max - min + 1) + min // for random double

If else

if (x > y || x < y && x == y){
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}

Switch

switch(expression) {
  case x:   // expression == x
    // code block
    break;
  case y:   // expression == y
    // code block
    break;
  default:  // else
    // code block
}

Loobs

For

for (int i = 0; i < 5; i++) {   // runs for 5 times
  // code block to be executed
}

While

while (condition) {     // checks condition runs code
  // code block to be executed
}

While do

do {                    // runs code checks condition
  // code block to be executed
}
while (condition);

Try catch

try {
    int[] myNumbers = {1, 2, 3};
    System.out.println(myNumbers[10]);
    // Some code
} catch (Exception e) {
    // Some code if error
    System.out.println("Something went wrong.");
}

Import

import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.*;   // evertying from swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
etc...

Methodes

public static String name_of_method(String var_name){
    // Some code
}

GUI Stuff

Creating a window

JFrame mainFrame = new JFrame(); // From swing
mainFrame.setTitle("Lotto");
mainFrame.setLayout(null);
mainFrame.setSize(1000, 600);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);

Creating a Button

JButton mainButton = new JButton();
mainButton.setText("myButton");
mainButton.setSize(100, 50);
mainButton.setLocation(0, 0);
mainButton.setVisible(true);

mainFrame.add(mainButton);

Creating a Events

mainButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // Some code
        // sysout 
        // easter egg
        // etc...
    }
});