Introduction to Java Programming


Java programming illustration

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.

Here are the 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,has automatic memory management,Readable syntax,and Rich standard library.
  • Platform Independent: Java supports the "Write Once, Run Anywhere" feature. Java programs can run on any device or OS that supports the Java Virtual Machine (JVM), without modification.
  • Object-Oriented: Java follows the object-oriented paradigm, promoting clean, modular, and reusable code.
  • Secure: Java does not support pointers and includes built-in security features to prevent common vulnerabilities like memory leaks.
  • Multithreading: Java supports multithreading, allowing programs to perform multiple tasks concurrently—ideal for complex and high-performance applications.
  • Robust: Java is designed to be extremely dependable and error-resistant. Without the need for manual pointer arithmetic, which is frequently the cause of errors in languages like C and C++, it offers robust memory management. Java checks for errors both during compilation and during execution. Its try, catch, and finally blocks for handling exceptions provide error recovery that is controlled and seamless. Strict type checking and automated trash collection further support the general stability of the software. Java applications, like Android apps or banking systems, for instance, are made to function continually while gracefully managing faults.
  • Distributed: Java is ideally suited for creating network-based applications that allow programs running on separate computers to communicate and share resources. It facilitates the smooth calling of methods on distant objects by supporting Remote Method Invocation (RMI). Through the java.net package, Java further offers built-in networking classes, which makes it perfect for cloud services, enterprise apps, and the Internet of Things (IoT). A Java-based e-commerce platform, for instance, can effectively communicate with distant databases, APIs, and payment gateways.
  • The architecture is neutral: The bytecode that Java compiles is not affected by the hardware architecture that underlies it. This bytecode is converted into host system-specific native machine instructions by the Java Virtual Machine (JVM). This method reduces incompatibilities caused by variations in operating systems, memory configurations, or processor kinds. For instance, Java bytecode can run unaltered on any device with a compatible JVM, in contrast to platform-specific C or C++ binaries.

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): An abstract machine that enables computers to run Java programs by interpreting bytecode.
  • Java Development Kit (JDK): Used to develop Java applications. It includes the JRE, compiler, and debugging tools.
  • Java Runtime Environment (JRE): Allows the running of Java applications. It includes the JVM and standard libraries.

Java Program Execution Flow:

Java source code → Bytecode (.class file using compiler) → Machine code (via JVM) → Load into memory → Bytecode verification → Execution


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

Java source code is compiled using javac into bytecode (.class), which is then interpreted into machine code by the JVM and executed by the CPU.

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

  1. First,install Java Development Kit: Download the JDK from oracle.com.
  2. Install an Integrated Development Environment: Use editors like Eclipse, NetBeans, or IntelliJ IDEA for Java development.
  3. Set System Variables:
    • Right-click on 'This PC' → click on Properties → click on Advanced System Settings → click on 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.
    • Check Installation: Open Command Prompt and type:
      java -version ← to check JRE
      javac -version ← to check compiler

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 (or command-line option) 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 encounter the error Error: Could not find or load main class JavaPractice, try removing the package declaration or ensure that your PATH and CLASSPATH are set correctly.

  • To compile multiple Java files: javac *.java
  • To run the program: java ClassName (replace ClassName with the one that contains the main method)
Yilma Goshime

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

LetUsLearn