📜 Introduction to Java
Java is one of the most widely used, high-level, and object-oriented programming languages. It was developed by James Gosling and Mike Sheridan at Sun Microsystems in 1991 and was officially released in 1995. Java is known for its platform independence, meaning that Java programs can run on any device or operating system that supports Java without modification, thanks to the "Write Once, Run Anywhere" (WORA) principle.
🔸 Features of Java
- Simple: Java is easy to learn and simple to use.
- Object-Oriented: Everything in Java is treated as an object, which makes it more modular, flexible, and adaptable.
- Platform Independent: Java follows the WORA principle, allowing applications to run on any platform with Java support.
- Secure: Java provides a secure environment for developing web applications and is less vulnerable to security risks.
- Distributed Computing: Java simplifies the development of distributed applications, such as those for client-server models.
- Multithreaded: Java supports multithreading, allowing multiple threads to run simultaneously for improved performance.
- Rich API: Java provides a rich set of libraries (APIs) for building robust applications.
- Automatic Memory Management: Java includes automatic garbage collection to manage memory efficiently.
🔸 Java Syntax
Java syntax is similar to C and C++, which makes it easy for developers who are familiar with those languages to pick up Java quickly. Java programs are written in files with a .java
extension, which are then compiled into bytecode by the Java Compiler (javac
) and executed by the Java Virtual Machine (JVM
).
🔸 Structure of a Java Program
Every Java program consists of at least one class, and the class contains a main()
method, which is the entry point for execution. The class name must match the file name. The following is a simple "Hello, World!" Java program:
// Simple Java Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In the above example:
- public class HelloWorld: This defines the class named
HelloWorld
.
- public static void main(String[] args): This is the entry point of the program, where execution begins.
- System.out.println(): This is a built-in Java method that prints the specified message to the console.
🔸 Java Compilation and Execution Process
Java follows a two-step process for compiling and running a program:
- Compilation: The Java source code is compiled by the Java compiler (
javac
) into an intermediate bytecode file with a .class
extension.
- Execution: The bytecode is executed by the Java Virtual Machine (JVM), which translates the bytecode into machine code for the host system.
🔸 Java Development Environment
To write and run Java programs, you will need:
- Java Development Kit (JDK): The JDK contains the Java compiler, libraries, and tools required for developing Java programs.
- Integrated Development Environment (IDE): While optional, an IDE such as IntelliJ IDEA, NetBeans, or Eclipse can help streamline development with features like code completion and debugging.
- Java Runtime Environment (JRE): The JRE is required to run Java applications and includes the JVM.
💡 Tip: Java is platform-independent due to the use of the Java Virtual Machine (JVM). You can write a program once, compile it into bytecode, and run it on any machine that has a JVM installed.