Skip to content

Tag 01 Programmierung # 1 Java

CodingRobIT edited this page Mar 14, 2023 · 1 revision

Java

Basics

Java was created in 1995 by Oracle (started by Sun) on the basis of C++ in order to create a platform-agnostic programming language which would run similarly on different devices.

Variables

Think of variables as boxes to contain a value of a defined type

camelCase is conventional for variable naming in Java

Declaration

int number;

Initialisation

int number = 7;

= sets the value of the variable

Data type

Data type defines the content type, which the value of the variable may be of

Primitive datatypes can be in particular: integral/whole numbers e.g.:

  • byte
  • short
  • int -
  • long

fractional numbers (floating-point literals) e.g.:

  • float - always set f after a number while initialsing
  • double

conditions e.g.:

  • boolean - true or false

texts e.g.:

  • String - string needs ""; strings may be added with +
  • char - character needs ''

Logic operators

boolean booleanTrue = true;
boolean booleanFalse = false;
System.out.println(booleanTrue && booleanFalse) // is false

Logical And

  • true && false = false
  • true && true = true
  • false && true = false
  • == equal to
  • != not equal to
  • > greater than
  • >= greater than or equal to
  • < less than
  • <= less than or equal to

Array

An array is a data structure of multiple variables of the same type

int[] array = new int[3];
// 'new' declaration of a new instance of an array with exactly three integers
array[0] = 1
array[1] = 2
array[2] = 3

Hello World in Java

To set up a new java project in IntelliJ IDEA follow these steps:

  • File - Empty Project
  • Enter name

Newly started project contains a precoded method main

public class Main {

    // main method is regularly run on the start
    public static void main(String[] args) { 

        // here is the playground
        
        System.out.println("Hello world!");
    }
}

run Main.java to execute

Clone this wiki locally