Java primitive Datentypen erkunden

JavaJavaBeginner
Jetzt üben

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

In diesem Lab werden Sie die acht primitiven Datentypen in Java kennenlernen. Sie werden erfahren, wie groß sie sind, welche Wertebereiche sie haben und was ihre Standardwerte sind. Sie werden auch über Autoboxing lernen.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/FileandIOManagementGroup(["File and I/O Management"]) java(("Java")) -.-> java/ConcurrentandNetworkProgrammingGroup(["Concurrent and Network Programming"]) java(("Java")) -.-> java/BasicSyntaxGroup(["Basic Syntax"]) java(("Java")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["Object-Oriented and Advanced Concepts"]) java/BasicSyntaxGroup -.-> java/data_types("Data Types") java/BasicSyntaxGroup -.-> java/operators("Operators") java/BasicSyntaxGroup -.-> java/booleans("Booleans") java/BasicSyntaxGroup -.-> java/variables("Variables") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("Wrapper Classes") java/FileandIOManagementGroup -.-> java/create_write_files("Create/Write Files") java/ConcurrentandNetworkProgrammingGroup -.-> java/working("Working") subgraph Lab Skills java/data_types -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/operators -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/booleans -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/variables -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/wrapper_classes -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/create_write_files -.-> lab-117948{{"Java primitive Datentypen erkunden"}} java/working -.-> lab-117948{{"Java primitive Datentypen erkunden"}} end

Erstellen einer neuen Java-Datei

Erstellen Sie eine neue Java-Datei und benennen Sie sie PrimitivesLab.java mit dem folgenden Befehl:

touch PrimitivesLab.java

Deklarieren von int- und byte-Datentypen

Deklarieren Sie einen int-Datentypen namens i und einen byte-Datentypen namens b. Weisen Sie ihnen die Werte 15 und 10 zu.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
    }
}

Deklarieren von short- und long-Datentypen

Deklarieren Sie einen short-Datentypen namens s und weisen Sie ihm einen Wert von 1000 zu. Deklarieren Sie einen long-Datentypen namens l und weisen Sie ihm einen Wert von 9999999L zu.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
    }
}

Deklarieren von float- und double-Datentypen

Deklarieren Sie einen float-Datentypen namens f und weisen Sie ihm einen Wert von 3.14f zu. Deklarieren Sie einen double-Datentypen namens d und weisen Sie ihm einen Wert von 2.71828 zu.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
    }
}

Deklarieren von boolean- und char-Datentypen

Deklarieren Sie einen boolean-Datentypen namens bool und weisen Sie ihm den Wert true zu. Deklarieren Sie einen char-Datentypen namens c und weisen Sie ihm den Wert 'A' zu.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
    }
}

Zeige das Autoboxing

Zeige das Autoboxing, indem du einen Integer-Datentypen namens x deklarierst und ihm den Wert 25 zuweist.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
        System.out.println("Value of x: " + x);
    }
}

Zeige die Ganzzahlunterläufe (Integer Underflow)

Deklariere einen Ganzzahl-Datentypen namens n und weise ihm den Wert 2147483647 zu. Inkrementiere n um 1 und notiere die Ausgabe.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
    }
}

Zeige die Ganzzahlüberläufe (Integer Overflow)

Deklariere einen Ganzzahl-Datentypen namens m und weise ihm den Wert -2147483648 zu. Dekrementiere m um 1 und notiere die Ausgabe.

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
        int m = -2147483648;
        m = m - 1;
        System.out.println("Value is: " + m);
    }
}

Kompilieren und Ausführen des Programms

Kompiliere das Programm mit dem folgenden Befehl in Ihrem Terminal:

javac PrimitivesLab.java

Führe das Programm mit dem folgenden Befehl aus:

java PrimitivesLab

Zusammenfassung

In diesem Lab haben Sie die acht primitiven Datentypen in Java kennengelernt. Sie haben ein Java-Programm erstellt und Variablen für jeden der primitiven Datentypen deklariert. Sie haben das Autoboxing kennengelernt und erfahren, wie Java automatisch einen primitiven Datentyp in seine entsprechende Wrapper-Klasse umwandelt. Sie haben auch über Überläufe und Unterläufe in den ganzzahligen Datentypen gelernt. Schließlich haben Sie das Programm kompiliert und ausgeführt, um die Ausgabe zu sehen. Sie können diese Datentypen in Ihrem Programm verwenden, um Daten effizient zu speichern und zu manipulieren.