Category: Java

brew: install Java on macOS

The following steps will guide you through the installation of Java on macOS.

First, check the available Java related formulas:

% brew search java

==> Formulae
app-engine-java              java                         javacc                       jslint4java                  pdftk-java
google-java-format           java11                       javarepl                     libreadline-java

Currently, there are two different version of Java: java and java11. To check the version of both, you can use the following commands:

% brew info java

openjdk: stable 16.0.1 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk.rb
License: GPL-2.0-only with Classpath-exception-2.0
% brew info java11

openjdk@11: stable 11.0.10 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk@11.rb
License: GPL-2.0-only

Depending on your requirements, you can install one of the above. For me, some of the libraries I use in Dart are currently not compatible with the latest Java version (16.0.1), so I decided to install Java 11 with LTS (long term support).

% brew install java11

This will install Java version 11.0.10 as listed in the output above. The output also shows the following hints:

For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

For me it was necessary to called the specified command, so that the system finds the java binary:

sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

To see if Java was installed correctly, you can check the version of Java:

 % java --version

openjdk 11.0.10 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9)
OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)

That’s it!

Photo by Adam Wilson on Unsplash

 

Java: Zufallszahlen in einem bestimmten Bereich erstellen

Angenommen, man möchte mit Java eine Zufallszahl innerhalbe eines bestimmten Zahlenbereiches definieren. In den folgenden Beispielen soll eine Zufallszahl zwischen 5 (inklusive) und 15 (inklusive) erstellt werden.

Hierfür definiere ich zunächst zwei Variablen, die in den nachfolgenden Code-Zeilen die Werte für Maximum und Minimum halten:

int min = 5;
int max = 15;

Variante 1: Math.random()

int x = (int)(Math.random() * ((max - min) + 1)) + min

Die Zahl x ist eine Zufallszahl im Bereich [min, max].

Im Detail

Die Java Math Funktion Math.random() gibt einen double Werte im Bereich [0, 1) zurück, wobei die 1 nicht mit eingeschlossen ist. Man sollte den Zufallswert also zunächst mit ‘max-min’ multiplizieren, um den Bereich abzudecken.

Math.random() * ( max - min )

Dies liefert einen Wert im Bereich [0, max – min), wobei der Endwert ‘max – min’ nicht enthalten ist. Nun sollte der Bereich noch in den gewünschten Bereich verschoben werden, indem man ‘min’ dazu addiert.

(Math.random() * (max - min)) + min

Die Zufallszahl enthält allerdings immer noch nicht den Wert ‘max’, d.h. man muss 1 zum Wert ‘max – min’ hinzuaddieren. Abschließend müssen noch alle Nachkommastellen durch ein Casten zu (int) entfernt werden.

(int)(Math.random() * ((max - min) + 1)) + min

Variante 2: Random()

Random ran = new Random();
int x = ran.nextInt(max - min + 1) + min;

Die Zahl x ist eine Zufallszahl im Bereich [min, max]. Neben nextInt() gibt es noch andere Methoden wie nextDouble(), nextFloat() etc. die genau wie nextInt() verwendet werden können, um Zufallszahlen zu generieren. Der type des Rückgabewertes ist dann entsprechend ein double, float oder eben integer.