Java is a widely-used programming language known for its "write once, run anywhere" philosophy. This article will cover the basics of Java, including its history, features, and some essential concepts.

History

Java was developed by Sun Microsystems in the mid-1990s. It was designed to be platform-independent, allowing developers to write code once and run it on any device with a Java Virtual Machine (JVM).

Features

Here are some of the key features of Java:

  • Object-Oriented Programming (OOP): Java is an OOP language, which means it focuses on objects and classes.
  • Platform Independence: Java code runs on any device with a JVM, making it a versatile language.
  • Robust: Java is known for its robustness and reliability.
  • Secure: Java has built-in security features that make it suitable for developing secure applications.
  • Multithreading: Java supports multithreading, allowing developers to create applications that can perform multiple tasks simultaneously.

Essential Concepts

Variables

In Java, variables are used to store data. Here are some basic types of variables:

  • Primitive Types: int, double, char, boolean
  • Reference Types: String, Object, etc.

Classes and Objects

In Java, everything is an object. A class is a blueprint for creating objects. Here's an example of a simple class:

public class Car {
    private String brand;
    private int year;

    public Car(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }

    public String getBrand() {
        return brand;
    }

    public int getYear() {
        return year;
    }
}

Inheritance

Inheritance allows you to create a new class (subclass) based on an existing class (superclass). Here's an example:

public class ElectricCar extends Car {
    private int batteryCapacity;

    public ElectricCar(String brand, int year, int batteryCapacity) {
        super(brand, year);
        this.batteryCapacity = batteryCapacity;
    }

    public int getBatteryCapacity() {
        return batteryCapacity;
    }
}

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. Here's an example:

public class Animal {
    public void makeSound() {
        System.out.println("Some sound");
    }
}

public class Dog extends Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
}

public class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow!");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.makeSound(); // Output: Woof!
        myCat.makeSound(); // Output: Meow!
    }
}

Further Reading

For more information on Java, you can check out the following resources:

Java Logo