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:
Stringis used for the account holder's name.intis used for the account number.doubleis used for the account balance.booleanindicates if the account is active.chardenotes 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.