Introduction to Java Programming


What is Java?


Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995, and later acquired by Oracle.It has been a popular choice among developers for over two decades, with millions of Java applications in use today.

Key Features of Java

Simplicity

Java is designed to be easy to learn and use, especially for developers familiar with C or C++. Because in Java there is no pointer, it has automatic memory management, readable syntax, and a rich standard library.

Platform Independent

A Java program written and compiled on one system can run on any other system without requiring any modifications, provided that the other system has a Java Virtual Machine (JVM) installed. This is because Java was created with the "Write Once, Run Anywhere" (WORA) principle in mind.
Cross-platform compatibility is made possible via the JVM, which serves as an interpreter between the compiled Java bytecode and the underlying operating system. Because Java ensures consistency and eliminates the need to rewrite or recompile code for each environment, it is the perfect choice for developers working with many platforms, such as Windows, macOS, or Linux.

Object-Oriented

The foundation of Java is object-oriented programming, which helps to structure software design around objects rather than logic and functions. Java completely supports the fundamental OOP ideas of encapsulation, inheritance, polymorphism, and abstraction.
These characteristics improve scalability, facilitate testing and debugging, and allow the reuse of existing code. Java applications become more intuitive and easier to grow over time by representing real-world items as objects.

Secure

Security is a key component of Java's design. Its inability to provide direct memory access via pointers is a key feature that helps reduce the risk of buffer overflows and unauthorized memory access.
Furthermore, Java's Security Manager and Bytecode Verifier, which verify the integrity of code before execution, offer a strong security mechanism. Java also enables sandboxing to keep potentially dangerous code—from the internet—from damaging the host system. These built-in safeguards make Java ideal for distributed and networked applications such as web and mobile apps.

Multithreading

Java’s multithreading capability allows a program to run several threads at once. This is especially useful in high-performance and real-time applications—for example, processing user input while downloading a file.
Java offers the Thread class, the Runnable interface, and tools from the java.util.concurrent package to manage threads. This results in responsive, scalable, and efficient applications such as server software, games, and GUI-based apps.

Robust

Java is designed to be reliable and error-resistant. It eliminates manual pointer arithmetic and offers strong memory management. Java performs error checking at compile-time and runtime.
Features like try, catch, and finally support controlled exception handling. Strict type checking and automatic garbage collection further enhance reliability. This makes Java ideal for mission-critical systems like Android apps or banking platforms.

Distributed

Java supports distributed computing through technologies like RMI (Remote Method Invocation), allowing methods to run on remote machines.
The java.net package provides built-in networking classes, making Java perfect for cloud services, enterprise systems, and IoT. For instance, a Java-based e-commerce app can communicate with remote databases, APIs, and payment gateways.

Architecture Neutral

Java’s bytecode is platform-independent. It is executed by the JVM, which translates it into native machine code for the host system.
This removes issues caused by variations in hardware architecture. Unlike platform-specific C/C++ binaries, Java bytecode can run on any system with a JVM, providing excellent portability and consistency.

Java is widely used to build desktop applications, web applications, Android apps, and enterprise systems. Every Java program has a class defined using the class keyword, and includes attributes, constructors, and methods.

During execution:

Java Virtual Machine (JVM)

The JVM is a virtual engine responsible for running Java applications. When Java code is compiled, it is transformed into platform-independent bytecode. At runtime, the JVM either interprets or compiles this bytecode into machine-specific code that can be executed by the underlying hardware.
This approach allows Java programs to be run on any operating system that supports the JVM, following the principle of “write once, run anywhere.” In addition to execution, the JVM also handles important tasks such as memory management, garbage collection, and runtime optimization, all of which contribute to the reliability and performance of Java applications.

Java Development Kit (JDK)

The JDK is a complete toolkit used by developers to build Java applications and applets. It includes the Java Runtime Environment (JRE) for running Java programs, as well as the Java compiler (javac) that converts source code into bytecode.
It also comes with various development tools like the jar utility for packaging, javadoc for generating documentation, and debugging tools. The JDK provides all the essential components needed to write, compile, and test Java code, making it a fundamental resource for Java developers.

Java Runtime Environment (JRE)

The JRE is intended for users who need to run Java applications but not develop them. It includes the JVM for executing bytecode and a collection of core libraries and APIs (like java.lang and java.util) that are commonly used in Java programs.
The JRE also contains necessary support files and tools to ensure smooth and efficient program execution. Since it does not include development tools, the JRE is ideal for end users rather than programmers.

Java Program Execution Flow

  • The Java compiler compiles Java source code into .class bytecode files.
  • This bytecode is platform-independent and can run on any system with a JVM.
  • The bytecode is passed to the Java Virtual Machine (JVM).
  • The JVM translates the bytecode into machine-level instructions specific to the host system.
  • Before execution, the bytecode undergoes a verification phase to check for memory and security violations.
  • Once verified, the JVM loads the code into memory.
  • The program is then executed by the JVM.
Example →Basic Java Program

public class Student {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

          

Java source code is first compiled by the javac compiler into bytecode files (.class). This bytecode is then processed by the Java Virtual Machine (JVM), which translates it into machine-specific instructions that the CPU executes.

To install and configure Java,we have to follow those procedures:

Steps to Install Java

  1. Install Java Development Kit: Download the JDK from oracle.com .
  2. Install an Integrated Development Environment (IDE): Use editors like Eclipse, NetBeans, or IntelliJ IDEA for Java development.
  3. Set System Variables:
    • Right-click on 'This PC' → Properties → Advanced System Settings → Environment Variables.
    • Under 'System Variables', click on edit Path.
    • Add C:\Program Files\Java\jdk-18\bin and C:\Program Files\Java\jre1.8.0_291\bin
    • Add the same path for CLASSPATH.
  4. Check Installation:
    Open Command Prompt and type:
    java -version ← checks the installed Java Runtime Environment (JRE)
    javac -version ← checks the installed Java compiler version

PATH is an environment variable used to locate Java tools like java (Java runtime) and javac (Java compiler) from the command line, regardless of the current working directory.

CLASSPATH is an environment variable used by the Java ClassLoader to locate user-defined classes and packages needed for Java application execution, including .class and .jar files.

You can run Java code using either a Command-Line Interface (CLI) or a Graphical User Interface (GUI). When using the CLI:

If you see the error like,"Could not find or load main class JavaPractice", try deleting the package declaration or make sure your PATH and CLASSPATH environment variables are properly configured.

  • To compile multiple Java files: javac *.java
  • To run the program: java ClassName (replace ClassName with the one that contains the main method)
← Previous Post Exception Handling in Java
Next Post → Java GUI Tutorial
Yilma Goshime

I’m committed to providing tailored solutions and always ready to assist if any issue arises.

LetUsLearn
Back to Top