Java Data Types: Real-Life Example
Understanding how data types are used in real-world applications can enhance comprehension. Below is an example of a simple banking application using various data types.
1. Bank Account Example
public class BankAccount {
public static void main(String[] args) {
String accountHolder = "John Doe";
int accountNumber = 123456789;
double accountBalance = 1000.50;
boolean isActive = true;
char accountType = 'S'; // S for Savings, C for Checking
System.out.println("Account Holder: " + accountHolder);
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Balance: $" + accountBalance);
System.out.println("Account Active: " + isActive);
System.out.println("Account Type: " + accountType);
}
}
Output:
Account Holder: John Doe
Account Number: 123456789
Account Balance: $1000.5
Account Active: true
Account Type: S
Account Number: 123456789
Account Balance: $1000.5
Account Active: true
Account Type: S
2. Explanation
In this example:
String
is used for the account holder's name.int
is used for the account number.double
is used for the account balance.boolean
indicates if the account is active.char
denotes the account type.
Key Takeaways
- Real-life applications use various data types to represent different kinds of data.
- Choosing the appropriate data type is crucial for data accuracy and program efficiency.