4.2 Lab - Data Types in Java

Introduction

In this lab, you will explore the different data types in Java. Understanding data types is fundamental to programming because they define the kind of values that can be stored and manipulated within a program. Java provides various data types, including primitive data types (e.g., int, double, char, boolean) and reference types (e.g., objects and arrays). In this lab, you will declare variables of different data types, perform operations on them, and observe how Java handles different types of data.

Objectives

By the end of this lab, you will be able to:

  1. Understand and use the basic data types in Java.
  2. Declare and initialize variables of different data types.
  3. Perform operations with variables of different data types.
  4. Write a Java program that demonstrates the use of primitive data types and reference types.

Lab Steps

Step 1: Access the Linux Terminal
  1. Open the terminal on your Linux system.
  2. Ensure you are in your home directory by running the command:
    cd ~
    
Step 2: Create and Edit a Java File
  1. Create a new Java file using Nano called DataTypesExample.java:
    nano DataTypesExample.java
    
  2. Type the following Java code into the file. This program will demonstrate the use of different data types:
    public class DataTypesExample {
        public static void main(String[] args) {
            // Integer data type
            int myInt = 25;
            System.out.println("Integer: " + myInt);
    
            // Double (floating-point) data type
            double myDouble = 3.14159;
            System.out.println("Double: " + myDouble);
    
            // Character data type
            char myChar = 'A';
            System.out.println("Character: " + myChar);
    
            // Boolean data type
            boolean myBoolean = true;
            System.out.println("Boolean: " + myBoolean);
    
            // String (reference type)
            String myString = "Hello, World!";
            System.out.println("String: " + myString);
        }
    }
    
  3. Save the file by pressing CTRL + O, then hit Enter to confirm the filename.
  4. Exit Nano by pressing CTRL + X.
Step 3: Compile the Java Program
  1. In the terminal, compile the Java program using the javac command:
    javac DataTypesExample.java
    
  2. If there are no errors, this command will create a file named DataTypesExample.class. Verify this by running:
    ls
    
Step 4: Run the Java Program
  1. Run the compiled program using the java command:
    java DataTypesExample
    
  2. The program will output the values of the different data types you declared, similar to:
    Integer: 25
    Double: 3.14159
    Character: A
    Boolean: true
    String: Hello, World!
    
Step 5: Modify the Program
  1. Open the DataTypesExample.java file again using Nano:
    nano DataTypesExample.java
    
  2. Modify the program to perform some operations on the data types. For example, add the following lines:
    // Perform arithmetic operations with int and double
    int sum = myInt + 10;
    double product = myDouble * 2;
    
    // Output the results
    System.out.println("Sum of myInt + 10: " + sum);
    System.out.println("Product of myDouble * 2: " + product);
    
  3. Save, exit, recompile, and run the program to see the updated behavior:
    javac DataTypesExample.java
    java DataTypesExample
    
Step 6: Add More Data Types
  1. Modify the program to include more primitive data types, such as long, float, and byte:
    // Long data type
    long myLong = 100000L;
    System.out.println("Long: " + myLong);
    
    // Float data type
    float myFloat = 5.75f;
    System.out.println("Float: " + myFloat);
    
    // Byte data type
    byte myByte = 100;
    System.out.println("Byte: " + myByte);
    
  2. Save, exit, recompile, and run the program again to see the additional data types:
    javac DataTypesExample.java
    java DataTypesExample
    
Step 7: Experiment with Type Casting
  1. Modify the program to include an example of type casting between data types:
    // Casting double to int
    int castedInt = (int) myDouble;
    System.out.println("Casted double to int: " + castedInt);
    
    // Casting int to double
    double castedDouble = myInt;
    System.out.println("Casted int to double: " + castedDouble);
    
  2. Save, exit, recompile, and run the program again. This time the output will demonstrate how values are cast between types:
    javac DataTypesExample.java
    java DataTypesExample
    
Step 8: Cleanup (Optional)
  1. Once you're done, clean up the directory by removing the .class files:
    rm *.class
    

Summary

In this lab, you explored the basic data types in Java, including integers, floating-point numbers, characters, booleans, and strings. You learned how to declare and initialize variables, perform arithmetic operations, and work with type casting. Understanding data types is crucial for ensuring that your program works efficiently and handles data correctly.