Java is a pure object - oriented language. The key concepts include classes, objects, inheritance, polymorphism, and encapsulation.
class Dog {
String name;
int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public void bark() {
System.out.println(name + " is barking!");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", 3);
myDog.bark();
}
}
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat is meowing");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Cat myCat = new Cat();
myCat.eat();
myCat.meow();
}
}
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape {
@Override
void draw() {
System.out.println("Drawing a rectangle");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape[] shapes = {new Circle(), new Rectangle()};
for (Shape shape : shapes) {
shape.draw();
}
}
}
class BankAccount {
private double balance;
public BankAccount(double initialBalance) {
this.balance = initialBalance;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}
public class EncapsulationExample {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000);
account.deposit(500);
account.withdraw(200);
System.out.println("Balance: " + account.getBalance());
}
}
Java uses automatic garbage collection to manage memory. The Java Virtual Machine (JVM) reclaims memory occupied by objects that are no longer reachable.
Java provides a mechanism to handle exceptions. The try - catch - finally
block is used to catch and handle exceptions.
public class ExceptionExample {
public static void main(String[] args) {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds: " + e.getMessage());
} finally {
System.out.println("This block always executes");
}
}
}
JAVA_HOME
environment variable and add the bin
directory of the JDK to the PATH
environment variable.Popular Java IDEs include IntelliJ IDEA, Eclipse, and NetBeans.
.java
extension, e.g., HelloWorld.java
.javac
command: javac HelloWorld.java
.java
command: java HelloWorld
.Group related classes and interfaces into packages. For example, in a web application, you might have packages for models, controllers, and services.
com.example.myapp
├── model
│ ├── User.java
│ ├── Product.java
├── controller
│ ├── UserController.java
│ ├── ProductController.java
├── service
│ ├── UserService.java
│ ├── ProductService.java
Use testing frameworks like JUnit to write unit tests for your Java code.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class Calculator {
public int add(int a, int b) {
return a + b;
}
}
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
}
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle");
}
}
class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
return null;
}
}
public class FactoryPatternExample {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
Shape circle = shapeFactory.getShape("CIRCLE");
circle.draw();
}
}
Java provides a rich set of classes and interfaces for concurrency management, such as Thread
, Runnable
, and the java.util.concurrent
package.
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Thread is running");
}
}
public class ConcurrencyExample {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
ArrayList
for dynamic arrays and HashMap
for key - value pairs.In modern Java development, following best practices is essential for creating high - quality, maintainable, and efficient applications. By understanding fundamental concepts, using proper usage methods, adopting common practices, and applying best practices like design patterns and concurrency management, developers can write better Java code. Continuous learning and staying updated with the latest Java features and trends will further enhance the development process.