1. variable

1.5. Practical Example in Java

Below is a Java program demonstrating variables and expressions, adapted from the Python example you provided. It includes basic data types and simple expressions.

public class VariablesAndExpressions { public static void main(String[] args) { // Variable declarations and initializations int age = 25; // Integer variable double height = 5.9; // Float (double) variable boolean isStudent = true; // Boolean variable String name = "Alice"; // String variable char initial = 'A'; // Character variable

    // Expressions

    int yearsUntilRetirement = 65 - age; // Arithmetic expression

    double heightInMeters = height * 0.3048; // Arithmetic expression

    boolean canEnroll = isStudent && age < 30; // Logical expression

    String greeting = "Hello, " + name; // String concatenation expression

 

    // Output results

    System.out.printf("Age: %d%n", age);

    System.out.printf("Height: %.1f feet (%.2f meters)%n", height, heightInMeters);

    System.out.printf("Is student? %b%n", isStudent);

    System.out.printf("Name: %s%n", name);

    System.out.printf("Initial: %c%n", initial);

    System.out.printf("Years until retirement: %d%n", yearsUntilRetirement);

    System.out.printf("Can enroll? %b%n", canEnroll);

    System.out.printf("Greeting: %s%n", greeting);

}

}