Java is a widely-used, object-oriented programming language designed to be platform-independent, meaning that Java code can run on any device that has a Java Virtual Machine (JVM). It is known for its simplicity, portability, and robustness, making it a popular choice for developing a wide range of applications, from web and mobile apps to enterprise-level software.

1. Basic Syntax

  • Hello World Example:
    java

    public class Main {
    public static void main(String[] args) {
    System.out.println("Hello, World!");
    }
    }
  • Explanation:
    • public class Main: Defines a class named Main. Java is an object-oriented language, so everything is written inside classes.
    • public static void main(String[] args): The main method is the entry point of any Java application. It’s where the program starts execution.
    • System.out.println("Hello, World!");: This line prints the string “Hello, World!” to the console.

2. Data Types

Java has several built-in data types categorized into two types: primitive types and reference types.

  • Primitive Types:
    • int: Integer type (e.g., int x = 10;).
    • double: Floating-point type (e.g., double y = 5.5;).
    • char: Character type (e.g., char letter = 'A';).
    • boolean: Boolean type (true or false).
  • Reference Types:
    • These include objects, arrays, and strings. Reference types point to objects in memory rather than holding the data directly.

3. Variables and Operators

  • Variables: Containers for storing data values.
    java

    int number = 10;
    String name = "John";
  • Operators:
    • Arithmetic Operators: +, -, *, /, %
    • Comparison Operators: ==, !=, >, <, >=, <=
    • Logical Operators: && (AND), || (OR), ! (NOT)

4. Control Structures

Java provides several control structures for flow control:

  • If-Else Statement:
    java

    if (x > 0) {
    System.out.println("Positive number");
    } else {
    System.out.println("Non-positive number");
    }
  • For Loop:
    java

    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
  • While Loop:
    java

    int i = 0;
    while (i < 5) {
    System.out.println(i);
    i++;
    }

5. Object-Oriented Programming (OOP) Concepts

Java is an object-oriented language, which means it focuses on objects that encapsulate data and behavior.

  • Class and Object: A class is a blueprint for objects, and an object is an instance of a class.
    java

    class Dog {
    String breed;
    int age;

    void bark() {
    System.out.println("Woof!");
    }
    }

    public class Main {
    public static void main(String[] args) {
    Dog myDog = new Dog();
    myDog.bark();
    }
    }

  • Inheritance: Allows a new class to inherit properties and methods from an existing class.
  • Polymorphism: Allows methods to do different things based on the object it is acting upon.
  • Encapsulation: Bundling data (variables) and methods that operate on the data into a single unit or class.
  • Abstraction: Hiding complex implementation details and showing only the essential features of an object.

6. Exception Handling

Java provides a robust mechanism for handling runtime errors through exceptions.

  • Try-Catch Block:
    java

    try {
    int result = 10 / 0;
    } catch (ArithmeticException e) {
    System.out.println("Cannot divide by zero.");
    }
  • Finally Block: Executed after the try-catch block, regardless of whether an exception was caught.

7. Java Libraries and APIs

Java comes with a vast standard library (Java API) that provides pre-built classes and methods for tasks like data structures, I/O operations, networking, and more.

8. Integrated Development Environment (IDE)

  • Common Java IDEs include Eclipse, IntelliJ IDEA, and NetBeans. These tools provide features like code completion, debugging, and project management.

9. Java Development Kit (JDK)

  • The JDK is a software development kit that includes tools like the Java compiler and JVM, which are essential for developing and running Java applications.

10. Getting Started

  • Install the JDK: Download and install the JDK from Oracle’s website.
  • Set Up Your IDE: Choose an IDE, install it, and configure it to use the JDK.
  • Write Your First Program: Start with the “Hello, World!” example and gradually build more complex programs as you learn.

Java is a versatile and powerful language that is used in many domains, including web development, mobile app development, and large-scale enterprise applications.

Sign In

Sign Up