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:
- Declare a variable of type
int and assign an integer value.
- Declare a variable of type
double and assign a decimal value.
- Declare a variable of type
String and assign a text value.
- Declare a variable of type
boolean and assign a value of true or false.
- 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.
View Example Code
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.