Java is a popular programming language used to develop desktop applications, web applications, enterprise software, and backend systems. Java provides several features that make it useful for building reliable and maintainable applications.
Java was designed with a structured syntax and removes several complex features associated with languages such as C++, including direct pointer manipulation and manual memory deallocation.
Java still has important concepts to learn, but its consistent structure helps beginners understand programming.
public class Main {
public static void main(String[] args) {
System.out.println("Java is simple to start learning.");
}
}
Java supports object-oriented programming (OOP). OOP organizes programs around classes and objects and helps developers structure large applications.
Important OOP concepts include:
class Student {
String name = "Rahul";
void display() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.display();
}
}
Here, Student is a class and s is an object created from that class.
Java is commonly described as platform-independent because its compiler generates bytecode that can run on different systems with compatible JVMs.
The basic process is:
Java Source Code
|
v
Java Compiler (javac)
|
v
Bytecode (.class)
|
v
Compatible JVM
|
v
Program Output
Java provides security mechanisms that help applications run in a controlled environment. These include bytecode verification, runtime checks, access controls, and security-related APIs.
class Account {
private double balance = 1000;
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
Account a = new Account();
System.out.println(a.getBalance());
}
}
The private modifier prevents other classes from directly accessing the balance field. This is an example of access control, not a complete security solution by itself.
Java is considered robust because it provides features that help developers detect errors and manage many common programming problems.
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
}
}
}
The catch block handles the arithmetic exception instead of allowing it to terminate the program at that point.
Java supports multithreading, which allows multiple threads of execution within a program. Threads can help applications perform different tasks concurrently.
public class Main {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("Thread is running");
});
t.start();
}
}
The start() method starts a new thread. The output shown is the expected message from this example.
Java bytecode is designed to be portable across compatible JVM implementations. This helps developers run the same compiled program on multiple systems.
public class Main {
public static void main(String[] args) {
System.out.println("Java supports portability.");
}
}
Java uses garbage collection to reclaim memory occupied by objects that are no longer reachable by the program. Developers generally do not manually free object memory.
class Student {
String name = "Amit";
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.name);
s = null;
}
}
After s = null, the object may become eligible for garbage collection if no other references point to it. Garbage collection timing is not guaranteed.
Java can provide high performance through Just-In-Time (JIT) compilation. The JVM can compile frequently executed bytecode into native machine code at runtime.
Java provides APIs for network communication and distributed applications. Developers can use these tools to build programs that communicate across computers and services.
import java.net.URI;
public class Main {
public static void main(String[] args) {
URI address = URI.create("https://example.com");
System.out.println(address.getHost());
}
}
This example uses Java's URI API to read the host from a URL. It does not make a network connection.
Java supports dynamic behavior through features such as runtime class loading, reflection, and dynamic linking of classes when required.
public class Main {
public static void main(String[] args) {
Class<?> c = String.class;
System.out.println(c.getName());
}
}
The example obtains runtime class information for Java's String class.
| Feature | Meaning |
|---|---|
| Simple | Structured syntax and fewer low-level tasks. |
| Object-oriented | Supports classes, objects, inheritance, and polymorphism. |
| Platform-independent | Bytecode runs on compatible JVMs. |
| Secure | Provides runtime checks and security mechanisms. |
| Robust | Offers type checking, exceptions, and memory management. |
| Multithreaded | Supports multiple threads of execution. |
| Portable | Bytecode can work across supported platforms. |
| High performance | JIT compilation and runtime optimization can improve speed. |
1. Which feature allows Java bytecode to run on compatible JVMs?
2. Which feature helps Java reclaim memory from unreachable objects?
3. What does JIT stand for?