Java Fundamentals

Lesson 01: Java Fundamentals

Java Fundamentals

Welcome to your first lesson in Java programming! In this lesson, we will cover the fundamental concepts of Java that every beginner should know. By the end of this lesson, you'll have a solid understanding of the basics and be ready to write your first Java program.

{getToc} $title={Table of Contents} $count={true}

1. Characteristics of Java

Java is one of the most popular programming languages because of its simplicity, versatility, and robustness. Here are some key characteristics:

  • Simple: Java is easy to learn and has a syntax similar to C++ but with fewer complexities.
  • Object-Oriented: Java is purely object-oriented, meaning everything in Java revolves around objects and classes.
  • Platform-Independent: Java programs can run on any device or operating system that has a Java Virtual Machine (JVM). This is often summarized as "Write Once, Run Anywhere" (WORA).
  • Robust: Java has strong memory management and error-checking features, making it less prone to crashes.
  • Secure: Java provides built-in security features, such as bytecode verification and sandboxing, which make it suitable for web applications.
  • Multithreaded: Java supports multithreading, allowing multiple tasks to run concurrently.

2. JDK vs JRE

Before you start coding in Java, it's important to understand the tools you'll need:

  • JDK (Java Development Kit): The JDK is a software development kit that includes everything you need to develop, compile, and run Java programs. It contains:
    • The Java Compiler (javac): Converts your .java files into .class files (bytecode).
    • The Java Runtime Environment (JRE): Allows you to run Java programs.
    • Debugging tools and libraries.
  • JRE (Java Runtime Environment): The JRE is a subset of the JDK. It only includes the runtime environment needed to execute Java programs. If you're just running Java programs (and not developing them), you only need the JRE.

3. J2SE, J2EE, J2ME

Java has different editions tailored for different purposes:

  • J2SE (Java 2 Standard Edition): This is the core Java platform, used for developing desktop and standalone applications. It includes basic libraries and APIs for general-purpose programming.
  • J2EE (Java 2 Enterprise Edition): This edition is designed for enterprise-level applications, such as web services, distributed systems, and large-scale applications. It includes additional APIs for database connectivity, web services, and more.
  • J2ME (Java 2 Micro Edition): This edition is used for developing applications for small devices like mobile phones, embedded systems, and other resource-constrained environments.

4. JVM & Java Memory

The Java Virtual Machine (JVM) is the engine that runs Java programs. It converts Java bytecode (.class files) into machine code that your computer can understand. The JVM also manages memory allocation and garbage collection.

Java Memory Areas:

  • Heap: Where objects are stored. This is where memory is allocated for objects during runtime.
  • Stack: Stores local variables and method calls. Each thread has its own stack.
  • Method Area: Stores class-level data, such as static variables and method code.
  • PC Register: Holds the address of the next instruction to be executed.
  • Native Method Stack: Used for native methods (methods written in languages other than Java).

5. Java Running Process

Here’s how a Java program runs step-by-step:

  1. Write the Code: You write your Java code in a .java file.
  2. Compile the Code: Use the Java compiler (javac) to compile your .java file into bytecode (.class file).
  3. Run the Bytecode: The JVM interprets the .class file and executes the program.
// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compile: javac HelloWorld.java
Run: java HelloWorld

Output:

Hello, World!

6. Installing IntelliJ IDEA

IntelliJ IDEA is one of the most popular Integrated Development Environments (IDEs) for Java development. Here's how to install it:

  1. Download IntelliJ IDEA:
  2. Install IntelliJ:
    • Follow the installation wizard for your operating system (Windows, macOS, or Linux).
  3. Create a New Project:
    • Open IntelliJ and select "New Project".
    • Choose "Java" as the project type and configure the JDK path.
  4. Write Your First Program:
    • Create a new Java class and start coding!

7. First Java Program

Let's write a simple "Hello, World!" program.

// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  • public class HelloWorld: Defines a class named HelloWorld.
  • public static void main(String[] args): The entry point of the program. Every Java program starts from the main method.
  • System.out.println("Hello, World!");: Prints "Hello, World!" to the console.

8. Variable Declaration

In Java, variables must be declared before they can be used. A variable declaration specifies the type of data the variable will hold.

Syntax:

type variableName = value;

Example:

int age = 25;         // Integer variable
double salary = 50000.50;  // Double variable
char grade = 'A';     // Character variable
boolean isJavaFun = true;  // Boolean variable
String name = "John"; // String variable

9. Java Naming Rules

Java has specific rules for naming variables, classes, and methods:

  • Variables and Methods: Start with a lowercase letter and use camelCase (e.g., myVariable, calculateSalary).
  • Classes: Start with an uppercase letter and use PascalCase (e.g., MyClass, StudentRecord).
  • Constants: Use all uppercase letters with underscores (e.g., MAX_VALUE, PI).

Example:

int studentAge = 20;          // Variable
class StudentRecord { }       // Class
final double PI = 3.14159;    // Constant

10. Comments

Comments are used to explain your code and make it more readable. Java supports three types of comments:

  • Single-line Comment:
    // This is a single-line comment
  • Multi-line Comment:
    /*
     * This is a multi-line comment.
     * It can span multiple lines.
     */
  • Documentation Comment:
    /**
     * This is a documentation comment.
     * It is used to generate API documentation.
     */

Example:

public class HelloWorld {
    public static void main(String[] args) {
        // Print a greeting message
        System.out.println("Hello, World!");  // Output: Hello, World!
    }
}

Conclusion

In this lesson, you've learned the basics of Java, including its characteristics, the difference between JDK and JRE, the various Java editions, how the JVM works, and how to write your first Java program. You've also covered variable declarations, naming conventions, and comments.

Now that you've completed this lesson, try writing a few simple Java programs on your own using IntelliJ IDEA. Practice makes perfect!

Happy coding! 🚀



Previous Post Next Post