Exploring Java Primitive Data Types

Beginner

Introduction

In this lab, you will learn about the eight primitive data types in Java. You will learn about their sizes, ranges, and default values. You will also learn about autoboxing.

Create a new Java file

Create a new Java file and name it PrimitivesLab.java using the following command:

touch PrimitivesLab.java

Declare int and byte data types

Declare an int data type named i and a byte data type named b. Assign them the values 15 and 10, respectively.

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);
    }
}

Declare short and long data types

Declare a short data type named s and assign it a value of 1000. Declare a long data type named l and assign it a value of 9999999L.

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);
    }
}

Declare float and double data types

Declare a float data type named f and assign it a value of 3.14f. Declare a double data type named d and assign it a value of 2.71828.

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);
    }
}

Declare boolean and char data types

Declare a boolean data type named bool and assign it the value true. Declare a char data type named c and assign it the value 'A'.

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);
    }
}

Demonstrate autoboxing

Demonstrate autoboxing by declaring an Integer data type named x and assigning it the value 25.

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);
    }
}

Demonstrate integer underflow

Declare an integer data type named n and assign it the value 2147483647. Increment n by 1 and note the output.

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);
    }
}

Demonstrate integer overflow

Declare an integer data type named m and assign it the value -2147483648. Decrement m by 1 and note the output.

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);
    }
}

Compile and run the program

Compile the program using the following command on your terminal:

javac PrimitivesLab.java

Run the program using the following command:

java PrimitivesLab

Summary

In this lab, you learned about the eight primitive data types in Java. You created a Java program and declared variables of each of the primitive data types. You learned about autoboxing and how Java automatically converts a primitive data type to its corresponding wrapper class. You also learned about overflow and underflow in the integer data types. Finally, you compiled and ran the program to see the output. You can use these data types in your programming to store and manipulate data efficiently.

Other Tutorials you may like