JavaFar Academy - Learn to Code with Java & PythonJavaFar Academy - Learn to Code with Java & Python

Java Unveiled: Exploring the Essence with a Unique Code Example

Java: Your Key to Building Code that Works Anywhere

Imagine a programming language where you write your code once and it magically runs on any device – desktop computer, smartphone, giant server… that’s the power of Java! Since its creation in 1995, Java has become a favorite thanks to its focus on readability, structured design, and the ability to work across different systems.

How Java Achieves ‘Write Once, Run Anywhere’

  • Bytecode: When you write Java, it’s turned into a special in-between form called bytecode. This isn’t tied to any one computer’s hardware.
  • Java Virtual Machine (JVM): Think of the JVM as a translator. It lives on each device and knows how to read and run that bytecode.

What Makes Java Special

class BankAccount {
    private String accountHolder;
    private double balance;

    // Constructor
    public BankAccount(String accountHolder, double initialBalance) {
        this.accountHolder = accountHolder;
        this.balance = initialBalance;
    }

    // Method to deposit funds
    public void deposit(double amount) {
        if(amount > 0) {
            balance += amount;
            System.out.println(amount + " deposited. New balance is " + balance);
        }
    }

    // Method to withdraw funds
    public void withdraw(double amount) {
        if(amount > 0 && balance >= amount) {
            balance -= amount;
            System.out.println(amount + " withdrawn. Remaining balance is " + balance);
        } else {
            System.out.println("Insufficient balance to withdraw " + amount);
        }
    }

    // Method to check balance
    public void checkBalance() {
        System.out.println("Balance is " + balance);
    }

    // Main method to run some operations
    public static void main(String[] args) {
        BankAccount account = new BankAccount("John Doe", 1000.00);
        account.checkBalance();  // Check initial balance
        account.deposit(500.00);  // Deposit money
        account.withdraw(200.00); // Withdraw money
        account.checkBalance();  // Check final balance
    }
}

In this example, the BankAccount class encapsulates the properties and methods related to a bank account. These include the account holder’s name and balance. It demonstrates vital OOP principles, including encapsulation (using private variables and public methods), and provides a simple but concrete example of how a real-world problem can be modeled in Java.

Key Features of Java Demonstrated:

  • Object-Oriented: The use of classes and objects to model real-world entities.

  • Encapsulation: Private access modifiers restrict access to class internal state to protect its integrity.

  • Methods: Objects can perform operations, such as depositing or withdrawing funds.

Java’s architecture-neutral nature, rich standard library, and extensive use in the industry make it an enduring choice for developers across diverse domains.

Leave a Reply

Your email address will not be published. Required fields are marked *