Skip to main content

Command Palette

Search for a command to run...

Introduction to Java

Published
5 min read
Introduction to Java

1. What is Java?

  • History and Evolution:

    • Java was created by James Gosling and his team at Sun Microsystems and was officially released in 1995.

    • The language was initially called "Oak," but it was later renamed "Java" after the island of Java, which was known for its coffee.

  • Features of Java:

    • Platform Independence: Java code is compiled into bytecode, which can be run on any platform with a Java Virtual Machine (JVM), making Java platform-independent (write once, run anywhere).

    • Object-Oriented: Java is an object-oriented programming language, which means it uses objects to model real-world entities, making the code more modular, flexible, and reusable.

    • Simple and Familiar: Java was designed to be easy to use, with a syntax that is clear and simple to understand, especially for developers who have experience with C or C++.

    • Secure: Java includes features like bytecode verification and a robust security manager to create secure applications. It has built-in safety mechanisms to prevent common vulnerabilities like memory leaks and buffer overflows.

    • Robust: Java has strong memory management, automatic garbage collection, and exception handling to ensure reliability.

    • Multithreaded: Java supports multithreading, allowing multiple tasks to run simultaneously, which is particularly useful in applications like games, multimedia, and web servers.

    • High Performance: Although Java is an interpreted language, modern JVMs use Just-In-Time (JIT) compilers to optimize performance close to that of native code.

    • Distributed: Java is designed for the distributed environment of the internet. It provides built-in support for web services and networking.

    • Dynamic: Java programs can dynamically load classes, libraries, and other components at runtime, allowing for greater flexibility.

  • Java vs. Other Programming Languages:

    • C++ vs. Java: Java simplifies memory management with automatic garbage collection, eliminating the need for manual memory allocation and deallocation (e.g., malloc and free in C++).

    • Python vs. Java: While Python is highly readable and easy to learn, Java offers better performance and a more extensive ecosystem for enterprise applications.

    • JavaScript vs. Java: Despite the similarity in names, Java and JavaScript serve different purposes. Java is a general-purpose programming language, while JavaScript is mainly used for web development.

    • Why Choose Java: Java is widely used in enterprise environments, Android app development, and large-scale systems due to its robustness, security features, and extensive libraries and frameworks.

2. Setting Up the Development Environment

  • Installing the Java Development Kit (JDK):

    • JDK vs. JRE: The Java Development Kit (JDK) is necessary for developing Java applications, as it contains the compiler (javac), libraries, and other tools required for writing, compiling, and running Java programs. The Java Runtime Environment (JRE) is used to run Java applications and is included within the JDK.

    • Downloading the JDK:

      • Visit the Oracle JDK download page or use OpenJDK (an open-source alternative). Select the appropriate version for your operating system.
    • Installing the JDK:

      • Windows: Run the installer and follow the instructions. Ensure to add the JDK to the system path during installation.

      • macOS: Use the .dmg file to install and follow the on-screen instructions.

      • Linux: Use the package manager (e.g., sudo apt install openjdk-11-jdk) or download the tarball and set it up manually.

    • Setting up the JAVA_HOME Environment Variable:

      • Windows: Go to System Properties -> Environment Variables, and add a new system variable JAVA_HOME pointing to the JDK installation directory. Add %JAVA_HOME%\bin to the Path variable.

      • macOS/Linux: Open .bash_profile or .bashrc (or .zshrc for Zsh users) and add:

          export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
          export PATH=$JAVA_HOME/bin:$PATH
        
      • Verify Installation: Open the terminal (or Command Prompt on Windows) and type java -version and javac -version to check if Java is installed correctly.

  • Choosing an Integrated Development Environment (IDE):

    • IntelliJ IDEA: A powerful IDE with advanced code completion, refactoring tools, and built-in support for frameworks like Spring. It's particularly popular among enterprise Java developers.

    • Eclipse: An open-source IDE that's been a mainstay in the Java community for years. It offers extensive plugin support and is highly customizable.

    • VSCode: A lightweight editor that, with the right extensions, can be an effective environment for Java development. It's especially useful if you're working with multiple languages in a single project.

    • Setting Up Your IDE:

      • Installing IntelliJ IDEA: Download from JetBrains, install it, and select the JDK during setup.

      • Installing Eclipse: Download from Eclipse.org, install, and configure the JDK.

      • Configuring VSCode for Java: Install the Java Extension Pack from the Extensions marketplace. Set up the JDK path in VSCode settings.

  • Writing and Running Your First Java Program:

    • Hello World Program:

      • Create a new Java file named HelloWorld.java and write the following code:

          public class HelloWorld {
              public static void main(String[] args) {
                  System.out.println("Hello, World!");
              }
          }
        
      • Explanation:

        • public class HelloWorld: Declares a public class named HelloWorld. The class name should match the filename.

        • public static void main(String[] args): The main method is the entry point for any Java application. The String[] args parameter allows the program to accept command-line arguments.

        • System.out.println("Hello, World!");: Prints "Hello, World!" to the console. System.out is a standard output stream.

      • Compiling and Running the Program:

        • Command Line:

          • Open a terminal or command prompt.

          • Navigate to the directory where HelloWorld.java is saved.

          • Compile the code using javac HelloWorld.java. This generates a HelloWorld.class file.

          • Run the compiled program with java HelloWorld.

        • Using an IDE:

          • Create a new Java project and class in your IDE.

          • Copy the above code into the class file.

          • Click on the run button or right-click on the file and select "Run" to execute the program.

By the end of Day 1, we will have a solid understanding of what Java is, why it's widely used, and how to set up a development environment to start coding in Java. Stay tuned! for further topics .

More from this blog

Mohit's blog

51 posts