Variables and Data Types

This exercise will help you master variables and data types in Java. You'll learn how to declare and work with different types such as int, double, String, and boolean, which are essential for any programmer. You'll also practice displaying information stored in variables using System.out.println(), which is essential for debugging and displaying output to the console.

Topic

Java Fundamentals

Java Exercise

In this exercise, you will create a Java program that uses different data types and variables to store information. The goal is to familiarize yourself with the most common data types in Java, such as:

  • int (integers)
  • double (decimal numbers)
  • String (text)
  • boolean (true/false values)

You'll need to declare variables of each type, assign them appropriate values, and then display those values ​​to the console using System.out.println().

Instructions:

  1. Declare a variable of type int and assign an integer value.
  2. Declare a variable of type double and assign a decimal value.
  3. Declare a variable of type String and assign a text value.
  4. Declare a variable of type boolean and assign a value of true or false.
  5. Display each variable to the console using System.out.println().

This exercise will help you understand how to work with variables and how to display information to the console in Java.


public class DataTypes {
    public static void main(String[] args) {
        // Variable declaration
        int integerNumber = 25;
        double decimalNumber = 3.14;
        String text = "Hello, Java!";
        boolean trueValue = true;
        
        // Display the values in the console
        System.out.println("Integer Number: " + integerNumber);
        System.out.println("Decimal Number: " + decimalNumber);
        System.out.println("Text: " + text);
        System.out.println("True Value: " + trueValue);
    }
}

 Output:

Integer: 25
Decimal: 3.14
Text: Hello, Java!
True Value: true

This code declares a variable of each data type mentioned and then prints their values ​​to the console.


 Share this JAVA exercise