Syntax und Datentypen in Java – Die Bausteine des Codes

© [email protected] – Shotshop.com

Willkommen zurück zu unserer Java-Tutorialreihe! In diesem Beitrag vertiefen wir uns in die Welt der Java-Syntax und Datentypen. Nachdem du im letzten Beitrag gelernt hast, wie du deine Java-Entwicklungsumgebung einrichtest, ist es nun an der Zeit, die grundlegenden Bausteine deines Java-Codes zu verstehen.

1. Java-Syntax

Java ist eine statisch typisierte Sprache, die der Syntax von C und C++ stark ähnelt. Das macht es für Entwickler, die bereits Erfahrung mit diesen Sprachen haben, leicht, sich zurechtzufinden. Aber selbst wenn das deine erste Programmiersprache ist, findest du die Syntax relativ einfach und logisch. Hier sind einige Schlüsselelemente:

Semikolon:
In Java ist das Semikolon (;) mehr als nur ein Satzzeichen. Es markiert das Ende einer Anweisung, quasi wie ein Punkt am Ende eines Satzes. Das ermöglicht der Java Virtual Machine (JVM) zu erkennen, wo die eine Anweisung aufhört und die nächste beginnt. Zum Beispiel schreibst du System.out.println("Hallo Welt"); und beendest die Anweisung mit einem Semikolon.

Klammern:
Klammern ({}) in Java haben eine spezielle Bedeutung; sie werden verwendet, um Codeblöcke zu definieren. Ein Codeblock könnte eine Klasse, eine Methode oder eine Schleife sein. Hier ein einfaches Beispiel:

Werbung/Advertising

<

Office 2019 Home and Student

Office 2019 Home and Student

Preis: 19,90 EUR

Zum Angebot

Office 2019 Professional

Office 2019 Professional

Preis: 39,90 EUR

Zum Angebot

Office 2016 Home and Student

Office 2016 Home and Student

Preis: 14,90 EUR

Zum Angebot

AVG TuneUp ESD

AVG TuneUp ESD

Preis: 25,00 EUR

Zum Angebot

Office 2024 Home

Office 2024 Home

Preis: 44,90 EUR

Zum Angebot

Windows 10/11 Pro

Windows 10/11 Pro

Preis: 19,90 EUR

Zum Angebot

Office 2019 Professional Plus

Office 2019 Professional Plus

Preis: 14,90 EUR

Zum Angebot

Trend Micro Antivirus + Security

Trend Micro Antivirus + Security

Preis: 20,00 EUR

Zum Angebot

Office 2021 Professional

Office 2021 Professional

Preis: 19,90 EUR

Zum Angebot

Windows Server 2022 RDS CALS

Windows Server 2022 RDS CALS

Preis: 109,90 EUR

Zum Angebot

ESET NOD32 Antivirus

ESET NOD32 Antivirus

Preis: 50,00 EUR

Zum Angebot

Windows 10 Pro

Windows 10 Pro

Preis: 14,90 EUR

Zum Angebot

McAfee VPN Safe Connect

McAfee VPN Safe Connect

Preis: 30,00 EUR

Zum Angebot

Microsoft Powerpoint 2019

Microsoft Powerpoint 2019

Preis: 29,90 EUR

Zum Angebot

Panda Dome Advanced MD

Panda Dome Advanced MD

Preis: 15,00 EUR

Zum Angebot

CorelCAD 2021 Upgrade Windows/Mac ESD

CorelCAD 2021 Upgrade Windows/Mac ESD

Preis: 180,00 EUR

Zum Angebot

Microsoft Windows Server 2022

Microsoft Windows Server 2022

Preis: 349,90 EUR

Zum Angebot

Microsoft Word 2021

Microsoft Word 2021

Preis: 14,90 EUR

Zum Angebot

AVG Internet Security

AVG Internet Security

Preis: 25,00 EUR

Zum Angebot

Adobe Photoshop Elements 2025

Adobe Photoshop Elements 2025

Preis: 190,00 EUR

Zum Angebot

Office 2019 Home and Business

Office 2019 Home and Business

Preis: 19,90 EUR

Zum Angebot

Microsoft Windows Server 2022 Datacenter

Microsoft Windows Server 2022 Datacenter

Preis: 219,00 EUR

Zum Angebot

Avast Premium Security

Avast Premium Security

Preis: 20,00 EUR

Zum Angebot

Office 2016 Home and Business

Office 2016 Home and Business

Preis: 14,90 EUR

Zum Angebot

Microsoft Word 2016

Microsoft Word 2016

Preis: 14,90 EUR

Zum Angebot

Bitdefender Premium VPN

Bitdefender Premium VPN

Preis: 90,00 EUR

Zum Angebot

Office 2016 Professional

Office 2016 Professional

Preis: 14,90 EUR

Zum Angebot

Office 2021 Professional Plus + Windows 11 Professional GRATIS

Office 2021 Professional Plus + Windows 11 Professional GRATIS

Preis: 24,90 EUR

Zum Angebot

Office 2024 Professional Plus

Office 2024 Professional Plus

Preis: 29,99 EUR

Zum Angebot

Office 2024 Professional

Office 2024 Professional

Preis: 39,90 EUR

Zum Angebot

McAfee Total Protection

McAfee Total Protection

Preis: 15,00 EUR

Zum Angebot

Bitdefender Antivirus Plus

Bitdefender Antivirus Plus

Preis: 50,00 EUR

Zum Angebot

Microsoft Excel 2019

Microsoft Excel 2019

Preis: 14,90 EUR

Zum Angebot

Windows 10/11 Home

Windows 10/11 Home

Preis: 14,90 EUR

Zum Angebot

Windows Server Standard 2022

Windows Server Standard 2022

Preis: 194,90 EUR

Zum Angebot

Office 2021 Home and Student

Office 2021 Home and Student

Preis: 19,90 EUR

Zum Angebot

Office 2021 Home and Business

Office 2021 Home and Business

Preis: 24,90 EUR

Zum Angebot

Windows Server 2019 RDS CALS

Windows Server 2019 RDS CALS

Preis: 93,90 EUR

Zum Angebot

Windows Server 2016 RDS CALS

Windows Server 2016 RDS CALS

Preis: 64,90 EUR

Zum Angebot

Windows Server 2019 CALS

Windows Server 2019 CALS

Preis: 87,90 EUR

Zum Angebot

Microsoft Word 2019

Microsoft Word 2019

Preis: 14,90 EUR

Zum Angebot

Microsoft Office 2019 Standard

Microsoft Office 2019 Standard

Preis: 19,90 EUR

Zum Angebot

Microsoft Excel 2021

Microsoft Excel 2021

Preis: 19,90 EUR

Zum Angebot

Windows 11 Home

Windows 11 Home

Preis: 19,90 EUR

Zum Angebot

Norton 360 Deluxe inkl. 25GB MD

Norton 360 Deluxe inkl. 25GB MD

Preis: 25,00 EUR

Zum Angebot

Visio 2016 Professional

Visio 2016 Professional

Preis: 14,90 EUR

Zum Angebot

Office 2021 Professional Plus

Office 2021 Professional Plus

Preis: 19,90 EUR

Zum Angebot

Kaspersky Mobile

Kaspersky Mobile

Preis: 25,00 EUR

Zum Angebot

Microsoft Office 2021 Standard

Microsoft Office 2021 Standard

Preis: 24,90 EUR

Zum Angebot

Adobe Acrobat Standard 2020 OEM

Adobe Acrobat Standard 2020 OEM

Preis: 900,00 EUR

Zum Angebot

Panda Dome Essential MD

Panda Dome Essential MD

Preis: 15,00 EUR

Zum Angebot

Windows 11 Pro

Windows 11 Pro

Preis: 14,90 EUR

Zum Angebot

Windows Server 2022 CALS

Windows Server 2022 CALS

Preis: 99,90 EUR

Zum Angebot

Windows Server 2016 CALS

Windows Server 2016 CALS

Preis: 64,90 EUR

Zum Angebot

Office 2021 Professional Plus + Windows 11 Professional Bundle

Office 2021 Professional Plus + Windows 11 Professional Bundle

Preis: 24,90 EUR

Zum Angebot

Watchdog Anti-Malware Vollversion

Watchdog Anti-Malware Vollversion

Preis: 25,00 EUR

Zum Angebot

Office 2021 Professional Plus f?r 3 Ger?te

Office 2021 Professional Plus f?r 3 Ger?te

Preis: 23,50 EUR

Zum Angebot

Windows 10 Home

Windows 10 Home

Preis: 14,90 EUR

Zum Angebot

AVG Ultimate

AVG Ultimate

Preis: 25,00 EUR

Zum Angebot

mySteganos Online Shield VPN

mySteganos Online Shield VPN

Preis: 55,00 EUR

Zum Angebot

Office 2019 Professional plus

Office 2019 Professional plus

Preis: 39,90 EUR

Zum Angebot

McAfee Internet Security

McAfee Internet Security

Preis: 25,00 EUR

Zum Angebot

Pinnacle Studio 26 (2023) STANDARD Windows / Deutsch

Pinnacle Studio 26 (2023) STANDARD Windows / Deutsch

Preis: 110,00 EUR

Zum Angebot

Panda Dome Complete MD

Panda Dome Complete MD

Preis: 25,00 EUR

Zum Angebot

Visio 2019 Professional

Visio 2019 Professional

Preis: 14,90 EUR

Zum Angebot

Windows Server 2016

Windows Server 2016

Preis: 99,90 EUR

Zum Angebot
>

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hallo Welt");
  }
}

Einrückung:
Obwohl Einrückung in Java nicht obligatorisch ist (im Gegensatz zu Sprachen wie Python), verbessert sie die Lesbarkeit des Codes erheblich. Es ist eine gängige Best Practice, Code innerhalb von Klammern einzurücken.

if (true) {
  System.out.println("Das ist wahr!");
}

2. Datentypen in Java

In Java gibt es zwei Hauptkategorien von Datentypen: primitive Datentypen und Referenzdatentypen.

Primitive Datentypen:
Dies sind die grundlegenden Datentypen in Java, die direkt Werte speichern. Sie umfassen int (für Ganzzahlen), double (für Fließkommazahlen), boolean (für Wahr/Falsch-Werte) und char (für einzelne Zeichen). Hier sind einige Beispiele:

int myNumber = 5;
double myDouble = 5.5;
boolean myBoolean = true;
char myChar = 'A';

Referenzdatentypen:
Diese Datentypen speichern nicht den eigentlichen Wert des Objekts, sondern eine Referenz darauf. Das erlaubt es uns, komplexe Strukturen wie Klassen, Schnittstellen und Arrays zu nutzen.

Warum sind Referenzdatentypen wichtig?
Im Gegensatz zu primitiven Datentypen, die nur einzelne Werte speichern können, erlauben uns Referenzdatentypen, komplexere Datenstrukturen zu bilden. Das macht sie extrem vielseitig.

Beispiel mit Arrays:
Arrays sind eine geordnete Liste von Elementen eines bestimmten Typs. Sie sind in Java ein typisches Beispiel für einen Referenzdatentyp.

int[] myArray = new int[5]; // Ein Array von Integern mit einer Länge von 5

3. Variablendeklaration und -initialisierung

Grundlagen:
Um eine Variable zu deklarieren, definierst du zuerst den Datentyp und dann den Variablennamen.

int myNumber; // Deklariert eine Variable vom Typ „int“ namens „myNumber“

Zuweisung eines Werts:
Um einer Variable einen Wert zuzuweisen, benutzt du das Gleichheitszeichen (=).

myNumber = 5; // Weist der Variable „myNumber“ den Wert 5 zu

Deklaration und Initialisierung zusammen:
Sie können auch beides in einer Zeile machen.

int myNumber = 5; // Deklariert „myNumber“ und weist ihr gleich den Wert 5 zu

4. Arbeiten mit Strings

Strings in Java:
In Java sind Strings Objekte, die eine Sequenz von Zeichen darstellen. Trotzdem sind sie einfacher zu verwenden als andere Objekte.

String myString = "Hallo, Welt!"; // Deklariert und initialisiert eine String-Variable

String-Konkatenation:
Sie können Strings mit dem Pluszeichen (+) zusammenfügen.

Werbung/Advertising

Aromatico
String greet = "Hallo";
String name = "Welt";
String greeting = greet + ", " + name + "!";
System.out.println(greeting); // Ausgabe: "Hallo, Welt!"

Abschluss

Das war’s für heute! Wir haben uns die Grundlagen der Java-Syntax angesehen und gelernt, wie man mit verschiedenen Datentypen arbeitet. Im nächsten Blogbeitrag werden wir tiefer in Java eintauchen und uns mit Kontrollstrukturen wie Bedingungen und Schleifen beschäftigen.

Bis dahin, happy coding!